pratik03
pratik03

Reputation: 661

GoogleAuthUtil.getToken() method throws IllegalStateException

I've used GoogleApiClient object, inherited ConnectionCallbacks interface and calling GoogleAuthUtil.getToken(Context context, String accountName, String scope) from two different methods.

1. overriden onConnected() method of ConnectionCallbacks interface.

  1. onClick() method of button.

In both of these I am getting different response of getToken() method.

  1. while calling from onConnected() method: it throws error like: java.lang.IllegalStateException: Calling this from your main thread can lead to deadlock.

  2. In case while calling from onClick() method, it works fine!!!

What is difference between these 2 calls? onClick() executes in main thread. Right?

onClick() method:

@Override
public void onClick(View v) {
    getGoogleToken();
}

onConnected() method:

@Override
public void onConnected(Bundle arg0) {
     getGoogleToken();
 }

getGoogleToken() method:

private void getGoogleToken() {
    try {
        AccountManager am = AccountManager.get(MainActivity.this);
        Account[] accounts = am.getAccountsByType(GoogleAuthUtil.GOOGLE_ACCOUNT_TYPE);

        //Retrieve the google token
        String token = GoogleAuthUtil.getToken(getApplicationContext(), accounts[0].name, PRIVATE_KEY); // This line generates error.

        // ...

    } catch (Exception ex) {
        ex.printStackTrace();
    }
}

Here is stackTrace:

01-25 17:53:02.700 23235-23235/num.app.gpluslogin W/System.err: java.lang.IllegalStateException: Calling this from your main thread can lead to deadlock
01-25 17:53:02.700 23235-23235/num.app.gpluslogin W/System.err:     at com.google.android.gms.common.internal.zzx.zzcy(Unknown Source)
01-25 17:53:02.700 23235-23235/num.app.gpluslogin W/System.err:     at com.google.android.gms.auth.GoogleAuthUtil.zza(Unknown Source)
01-25 17:53:02.700 23235-23235/num.app.gpluslogin W/System.err:     at com.google.android.gms.auth.GoogleAuthUtil.getToken(Unknown Source)
01-25 17:53:02.700 23235-23235/num.app.gpluslogin W/System.err:     at com.google.android.gms.auth.GoogleAuthUtil.getToken(Unknown Source)
01-25 17:53:02.700 23235-23235/num.app.gpluslogin W/System.err:     at com.google.android.gms.auth.GoogleAuthUtil.getToken(Unknown Source)
01-25 17:53:02.700 23235-23235/num.app.gpluslogin W/System.err:     at num.app.gpluslogin.MainActivity.getGoogleToken(MainActivity.java:274)
01-25 17:53:02.700 23235-23235/num.app.gpluslogin W/System.err:     at num.app.gpluslogin.MainActivity.onConnected(MainActivity.java:207)
01-25 17:53:02.700 23235-23235/num.app.gpluslogin W/System.err:     at com.google.android.gms.common.internal.zzk.zzk(Unknown Source)
01-25 17:53:02.700 23235-23235/num.app.gpluslogin W/System.err:     at com.google.android.gms.internal.zzmg.zzi(Unknown Source)
01-25 17:53:02.700 23235-23235/num.app.gpluslogin W/System.err:     at com.google.android.gms.internal.zzme.zzpi(Unknown Source)
01-25 17:53:02.701 23235-23235/num.app.gpluslogin W/System.err:     at com.google.android.gms.internal.zzme.onConnected(Unknown Source)
01-25 17:53:02.701 23235-23235/num.app.gpluslogin W/System.err:     at com.google.android.gms.internal.zzmi.onConnected(Unknown Source)
01-25 17:53:02.701 23235-23235/num.app.gpluslogin W/System.err:     at com.google.android.gms.internal.zzlz.onConnected(Unknown Source)
01-25 17:53:02.702 23235-23235/num.app.gpluslogin W/System.err:     at com.google.android.gms.common.internal.zzj$zzg.zzqv(Unknown Source)
01-25 17:53:02.702 23235-23235/num.app.gpluslogin W/System.err:     at com.google.android.gms.common.internal.zzj$zza.zzc(Unknown Source)
01-25 17:53:02.702 23235-23235/num.app.gpluslogin W/System.err:     at com.google.android.gms.common.internal.zzj$zza.zzv(Unknown Source)
01-25 17:53:02.702 23235-23235/num.app.gpluslogin W/System.err:     at com.google.android.gms.common.internal.zzj$zzc.zzqx(Unknown Source)
01-25 17:53:02.702 23235-23235/num.app.gpluslogin W/System.err:     at com.google.android.gms.common.internal.zzj$zzb.handleMessage(Unknown Source)
01-25 17:53:02.702 23235-23235/num.app.gpluslogin W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:102)
01-25 17:53:02.702 23235-23235/num.app.gpluslogin W/System.err:     at android.os.Looper.loop(Looper.java:148)
01-25 17:53:02.702 23235-23235/num.app.gpluslogin W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:5417)
01-25 17:53:02.702 23235-23235/num.app.gpluslogin W/System.err:     at java.lang.reflect.Method.invoke(Native Method)
01-25 17:53:02.702 23235-23235/num.app.gpluslogin W/System.err:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
01-25 17:53:02.702 23235-23235/num.app.gpluslogin W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
01-25 17:53:09.358 1587-1605/system_process W/ActivityManager: Launch timeout has expired, giving up wake lock!

Upvotes: 1

Views: 1096

Answers (1)

Anurag Singh
Anurag Singh

Reputation: 6190

Try executing the below code block in a background thread.

new Thread(new Runnable() {
    public void run() {

     android.os.Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);

        //Your code
try {

        AccountManager am = AccountManager.get(MainActivity.this);
        Account[] accounts =  am.getAccountsByType(GoogleAuthUtil.GOOGLE_ACCOUNT_TYPE);

        //Retrieve the google token
        String token = GoogleAuthUtil.getToken(getApplicationContext(), accounts[0].name, PRIVATE_KEY); // This line generates error.
        .......
        .....
        }catch(Exception ex)
        {
          ex.printStackTrace();}
        }
}).start();

Upvotes: 1

Related Questions