2017-02-23 75 views
0

我正在努力獲取谷歌的廣告ID爲我的應用程序。它可以工作,但是當我在Galaxy S4(4.3)上測試它時。它完成泄漏ServiceConnection錯誤。這不會使Force關閉,但每次單擊Activity上的返回按鈕時都會打印錯誤日誌。 S4是新鮮出廠復位狀態。在我的Galaxy J5(6.0.1)上沒問題(6.0.1)活動泄漏ServiceConnection獲取谷歌AdId在銀河S4

我看到播放服務已過期,AdId爲空。

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

錯誤日誌

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

使用ContextWrapper解決來捕獲泄漏ServiceConnection

@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; 
    } 
相關問題