GoogleAuthUtil.getToken()方法抛出IllegalStateException

时间:2017-01-25 13:29:19

标签: android google-signin google-authentication

我使用了GoogleApiClient个对象,继承了ConnectionCallbacks接口并使用两种不同的方法调用GoogleAuthUtil.getToken(Context context, String accountName, String scope)

1.覆盖onConnected()接口的ConnectionCallbacks方法。

  1. onClick()按钮方法。
  2. 在这两个方面,我得到getToken()方法的不同反应。

    1. onConnected ()方法调用时:它抛出如下错误:java.lang。 IllegalStateException :从主线程调用此方法可能导致死锁。

    2. 如果从 onClick()方法调用,它可以正常工作!!!

    3. 这两个电话有什么区别? onClick()在主线程中执行。正确?

      onClick()方法:

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

      onConnected()方法:

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

      getGoogleToken()方法:

      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();
          }
      }
      

      这是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!
      

1 个答案:

答案 0 :(得分:1)

尝试在后台线程中执行以下代码块。

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();