Java – Calling any AccountManager getAccounts stops my application

Calling any AccountManager getAccounts stops my application… here is a solution to the problem.

Calling any AccountManager getAccounts stops my application

This code reproduces the issue and that’s it. Just take an activity and create an AccountManger instance, then call getAccounts(), it never reaches the for(... line.

public class MainActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        AccountManager manager = AccountManager.get(this);
        Account[] accounts = manager.getAccounts();
        for (int index = 0 ; index < accounts.length ; ++index)
            Log.i("RumbAPK", accounts[index].name);
    }
}

All I want to do is be able to list the current account and check if it is authenticated to use it as an authentication system for my application.

Edit:

According to @ CommonsWave is of course required because it has to be useful.

FATAL EXCEPTION: main
java.lang.SecurityException: caller uid 10085 lacks any of android.permission.GET_ACCOUNTS
    at android.os.Parcel.readException(Parcel.java:1428)
    at android.os.Parcel.readException(Parcel.java:1382)
    at android.accounts.IAccountManager$Stub$Proxy.getAccountsAsUser(IAccountManager.java:541)
    at android.accounts.AccountManager.getAccountsByTypeAsUser(AccountManager.java:414)
    at android.accounts.AccountManager.getAccountsByType(AccountManager.java:408)
    at com.har.innovation.and.technology.rumbapk.GoogleLoginManager.authenticate(GoogleLoginManager.java:31)
    at com.har.innovation.and.technology.rumbapk.MainActivity.onClick(MainActivity.java:132)
    at android.view.View.performClick(View.java:4220)
    at android.view.View$PerformClick.run(View.java:17510)
    at android.os.Handler.handleCallback(Handler.java:800)
    at android.os.Handler.dispatchMessage(Handler.java:100)
    at android.os.Looper.loop(Looper.java:194)
    at android.app.ActivityThread.main(ActivityThread.java:5455)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:525)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:966)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:733)
    at dalvik.system.NativeStart.main(Native Method)

Solution

Based on the stack trace, I’m pretty sure you’re missing GET_ACCOUNTS permissions in AndroidManifest.xml

Please add this line:

<pre class=”lang-none prettyprint-override”><uses-permission android:name="android.permission.GET_ACCOUNTS"/>

.xml to AndroidManifest

Read moreInformations about the permission system on Android.

Related Problems and Solutions