2017-02-23 5 views
0

私のアプリのGoogleの広告IDを取得しようとしています。それは動作しますが、私がGalaxy S4(4.3)でテストします。漏れたServiceConnectionエラーで終了します。これは、Forceを閉じるのではなく、アクティビティのBackボタンをクリックするたびにエラーログを出力します。 S4は工場出荷時のリセット状態です。ギャラクシーJ5(6.0.1)ギャラクシーS4でGoogle AdIdを取得すると、アクティビティがリークしました

再生サービスが古く、AdIdがnullです。

W/GooglePlayServicesUtil: Google Play services out of date. Requires 10084000 but found 3225130

この問題を解決するために、任意のアイデア?

コード

public static class InitialisingTask extends AsyncTask<Context, Void, Void> { 

    private final String TAG = InitialisingTask.class.getSimpleName(); 

    @Override 
    protected Void doInBackground(Context... contexts) { 

     Context context = contexts[0]; 

     AdvertisingIdClient.Info idInfo = null; 
     try { 
      idInfo = AdvertisingIdClient.getAdvertisingIdInfo(context); 
     } catch (GooglePlayServicesNotAvailableException | GooglePlayServicesRepairableException | IOException e) { 
      Log.w(TAG, "fetch Google Ads is failed. exception message=" + e.getMessage()); 
     } 

     if (idInfo != null) { 
      String adId = idInfo.getId(); 
      setAdId(adId); 

      Log.d(TAG, "fetching Google Ads id finished. adId=" + adId); 
     } 

     return null; 
    } 
} 

エラーログServiceConnectionをリークしたキャプチャするContextWrapperを使用して解決

02-24 00:42:59.611 24301-24301/com.company.android.sample E/ViewRootImpl: sendUserActionEvent() mView == null 
02-24 00:42:59.611 24301-24301/com.company.android.sample E/ActivityThread: Activity com.company.android.sample.MainActivity has leaked ServiceConnection [email protected] that was originally bound here 
                      android.app.ServiceConnectionLeaked: Activity com.company.android.sample.MainActivity has leaked ServiceConnection [email protected] that was originally bound here 
                       at android.app.LoadedApk$ServiceDispatcher.<init>(LoadedApk.java:979) 
                       at android.app.LoadedApk.getServiceDispatcher(LoadedApk.java:873) 
                       at android.app.ContextImpl.bindServiceAsUser(ContextImpl.java:1818) 
                       at android.app.ContextImpl.bindService(ContextImpl.java:1806) 
                       at android.content.ContextWrapper.bindService(ContextWrapper.java:503) 
                       at com.google.android.gms.common.stats.zza.zza(Unknown Source) 
                       at com.google.android.gms.common.stats.zza.zza(Unknown Source) 
                       at com.google.android.gms.ads.identifier.AdvertisingIdClient.zzf(Unknown Source) 
                       at com.google.android.gms.ads.identifier.AdvertisingIdClient.zze(Unknown Source) 
                       at com.google.android.gms.ads.identifier.AdvertisingIdClient.getAdvertisingIdInfo(Unknown Source) 
                       at com.skb.nads.internal.sdk.v1.AdSdk$1.run(AdSdk.java:115) 
                       at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:390) 
                       at java.util.concurrent.FutureTask.run(FutureTask.java:234) 
                       at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080) 
                       at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573) 
                       at java.lang.Thread.run(Thread.java:841) 

答えて

0

@Override 
    protected Void doInBackground(Context... contexts) { 

     Context context = contexts[0]; 

     final List<ServiceConnection> leakableServiceList = new ArrayList<>(1); 
     try { 
      ContextWrapper contextWrapper = new ContextWrapper(context) { 
       @Override 
       public boolean bindService(Intent service, ServiceConnection conn, int flags) { 
        if ("com.google.android.gms.ads.identifier.service.START".equals(service.getAction()) 
          && "com.google.android.gms".equals(service.getPackage())) { 
         leakableServiceList.add(conn); 
        } 
        return super.bindService(service, conn, flags); 
       } 
      }; 
      AdvertisingIdClient.setShouldSkipGmsCoreVersionCheck(true); 
      AdvertisingIdClient.Info idInfo = AdvertisingIdClient.getAdvertisingIdInfo(contextWrapper); 
      if (idInfo != null) { 
       String adId = idInfo.getId(); 
       setAdId(adId); 
       Log.d(TAG, "fetching Google Ads id finished. adId=" + adId); 
      } 
      // https://developers.google.com/android/reference/com/google/android/gms/ads/identifier/AdvertisingIdClient 
     } catch (GooglePlayServicesNotAvailableException | GooglePlayServicesRepairableException | IOException | IllegalStateException e) { 
      Log.w(TAG, "fetch Google Ads is failed. exception message=" + e.getMessage()); 
     } finally { 
      for (ServiceConnection conn: leakableServiceList) { 
       try { 
        context.unbindService(conn); 
        Log.w(TAG, "Oh! leaking service connection is detected and fixed."); 
       } catch(IllegalArgumentException e) { 
        // OK - already unbind and not leaked! 
       } catch(Throwable t) { 
        Log.e(TAG, "something terrible happens", t); 
       } 
      } 
     } 

     return null; 
    } 
関連する問題