【问题标题】:AdMob: "activity has leaked ServiceConnection" and "AdvertisingIdClient unbindService failed."AdMob:“活动已泄露 ServiceConnection”和“AdvertisingIdClient unbindService 失败。”
【发布时间】:2015-01-27 22:20:29
【问题描述】:

我在我的应用中使用 AdMob。我遵循了实施指南,横幅正确显示和加载了广告。我的 MainActivity 负责创建引用、构建 AdRequest 和显示横幅,以及暂停、恢复和销毁 AdView。每次加载 AdRequest 时,当调用 Activity 的 onStop() 时,会创建以下异常:

6706-6706/com.rocca.controlloSpese E/ActivityThread﹕ Activity com.rocca.controlloSpese.MainActivity has leaked ServiceConnection com.google.android.gms.common.b@52b1cc3c that was originally bound here
android.app.ServiceConnectionLeaked: Activity com.rocca.controlloSpese.MainActivity has leaked ServiceConnection com.google.android.gms.common.b@52b1cc3c that was originally bound here
        at android.app.LoadedApk$ServiceDispatcher.<init>(LoadedApk.java:970)
        at android.app.LoadedApk.getServiceDispatcher(LoadedApk.java:864)
        at android.app.ContextImpl.bindServiceCommon(ContextImpl.java:1577)
        at android.app.ContextImpl.bindService(ContextImpl.java:1560)
        at android.content.ContextWrapper.bindService(ContextWrapper.java:517)
        at com.google.android.gms.ads.identifier.a.b(SourceFile:330)
        at com.google.android.gms.ads.identifier.a.a(SourceFile:187)
        at com.google.android.gms.ads.identifier.a.b(SourceFile:239)
        at com.google.android.a.t.f(SourceFile:132)
        at com.google.android.a.t.b(SourceFile:182)
        at com.google.android.a.q.a(SourceFile:258)
        at com.google.android.a.q.a(SourceFile:195)
        at com.google.android.gms.ads.internal.m.a(SourceFile:107)
        at com.google.android.gms.ads.internal.request.c.a(SourceFile:99)
        at com.google.android.gms.ads.internal.util.b.run(SourceFile:17)
        at com.google.android.gms.ads.internal.util.d.call(SourceFile:29)
        at com.google.android.gms.ads.internal.util.e.call(SourceFile:49)
        at java.util.concurrent.FutureTask.run(FutureTask.java:237)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
        at java.lang.Thread.run(Thread.java:841)

随后是此信息:

6706-6781/com.rocca.controlloSpese I/AdvertisingIdClient﹕ AdvertisingIdClient unbindService failed.
java.lang.IllegalArgumentException: Service not registered: com.google.android.gms.common.b@52b1cc3c
        at android.app.LoadedApk.forgetServiceDispatcher(LoadedApk.java:922)
        at android.app.ContextImpl.unbindService(ContextImpl.java:1611)
        at android.content.ContextWrapper.unbindService(ContextWrapper.java:529)
        at com.google.android.gms.ads.identifier.a.c(SourceFile:275)
        at com.google.android.gms.ads.identifier.b.c(SourceFile:100)
        at com.google.android.gms.ads.identifier.b.run(SourceFile:110)

如果 AdRequest 未构建和加载,则不会发生这种情况。我在 onCreate() 中设置了 IABHelper,如果用户没有购买我的“remove-ads”sku,则会显示广告。这是我用来加载广告的代码:

private void showAds() {
    int adresult = GooglePlayServicesUtil.isGooglePlayServicesAvailable(MainActivity.this);
    if (adresult == ConnectionResult.SUCCESS) {
        //if possible, show ads
        AdRequest adRequest = new AdRequest.Builder()
                .addTestDevice(AdRequest.DEVICE_ID_EMULATOR) 
                .addTestDevice("C144E9DA02EA7B26F74ED2C231F31D38") 
                .addTestDevice("93BABD84466B8C1EF529D2FB39D1ACE8") 
                .addTestDevice("BEAA738068664AE9BBF673E37A782E03") 
                .addTestDevice("E51508081F77DF84C129EE471DE67141") 
                .build();
        adView.setVisibility(View.VISIBLE);
        adView.loadAd(adRequest);
    } else {
        //if there's a problem, show error and close app
        GooglePlayServicesUtil.getErrorDialog(adresult, MainActivity.this, 0, new DialogInterface.OnCancelListener() {
            @Override
            public void onCancel(DialogInterface dialogInterface) {
                finish();
            }
        }).show();
    }
}

adView 的pause()resume()destroy() 在各自的活动方法中被调用。实际上,应用程序的性能不会受到影响,但我宁愿避免内存泄漏。如何避免该异常?

编辑:Manifest.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.rocca.controlloSpese" >

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="com.android.vending.BILLING" />
<uses-permission android:name="com.rocca.controlloSpese.BROADCAST_PERMISSION" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".MainActivity"
        android:finishOnCloseSystemDialogs="true"
        android:label="@string/app_name"
        android:launchMode="singleTop" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <meta-data
        android:name="com.google.android.gms.version"
        android:value="@integer/google_play_services_version" />
    <activity
        android:name="com.google.android.gms.ads.AdActivity"
        android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize" />        
</application>

【问题讨论】:

  • 你能把你的清单文件放在这里吗?
  • 当然,我删除了其他声明以使其简短明了。只有 MainActivity 加载广告。
  • 你在 onStop() 方法中做了什么特别的事情吗?
  • 在 onStop() 中只有一行代码与 admob 无关,甚至不值得更新问题。
  • groups.google.com/forum/#!topic/google-admob-ads-sdk/… 似乎他们正在解决这个问题。

标签: android memory-leaks admob


【解决方案1】:

仅使用 AdMob 网站 here 中的示例快速启动项目,我看到的行为完全相同。我在 Nexus 5 上看到了这个正在运行的 Lollipop。

因此,这在我看来像是一个 AdMob 错误。

是否有办法获得对 AdMob 服务的引用并手动取消绑定该服务以查看是否会停止错误?

【讨论】:

  • 这里也一样。我认为这与 mopub 适配器有关,但看起来不是。你找到解决方案了吗?
  • 我使用 Google Play Service AdMob 的应用也有同样的问题
  • 这里也一样。你有什么解决办法吗?
【解决方案2】:

下面的代码解决了我的问题;

@Override
    protected void onDestroy() {
        // TODO Auto-generated method stub
        mAdView.destroy();
        super.onDestroy();
    }

@Override
protected void onResume() {
    // TODO Auto-generated method stub
    super.onResume();
    mAdView.resume();

}


@Override
protected void onPause() {
    // TODO Auto-generated method stub
    super.onPause();

    mAdView.pause();
}

【讨论】:

  • 正如我在 OP 中所述,我正在拨打这些电话,但发生了异常。也许你也想分享你的 onCreate()?
  • 对不起我的帖子。我做到了,一两次之后没有看到任何错误,然后我看到了错误,所以我的方法不起作用我仍在寻找它,如果你找到一些解决方案,请告诉我谢谢:)
  • 为我解决了这个问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-05-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多