即使父 Activity 暂停,admob AdView 使用的 WebViewCoreThread 也在使用高 CPU

WebViewCoreThread used by admob AdView is using high CPU even when parent Activity is paused(即使父 Activity 暂停,admob AdView 使用的 WebViewCoreThread 也在使用高 CPU)
本文介绍了即使父 Activity 暂停,admob AdView 使用的 WebViewCoreThread 也在使用高 CPU的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Google Admob SDK v6.1.0 (https://developers.google.com/mobile-ads-sdk/download),并以编程方式(不是 XML)实例化 com.google.ads.AdView,并将其动态添加到我的 Activity 中的 LinearLayout 中.

I'm using Google Admob SDK v6.1.0 (https://developers.google.com/mobile-ads-sdk/download), and I instantiate the com.google.ads.AdView programmatically (not in XML), and add it into a LinearLayout, dynamically in my Activity.

我的一位用户报告说,当他们在我的 Activity 中单击 Home 按钮(以使其成为背景)时,他们开始看到来自我的应用的 CPU 使用率很高.我能够在 Jellybean 平台上重现这一点,并注意到高 CPU 使用率的来源是 WebViewCoreThread.

One of my users reported that when they click the Home button while in my Activity (in order to background it), they start seeing high CPU usage sourced to my app. I was able to reproduce this on a Jellybean platform, and noticed that the source for high CPU usage was a WebViewCoreThread.

我的 Activity 根本不使用任何 WebView,但我能够逐步完成 Activity 的初始化,并注意到当我实例化 AdMob AdView 对象时,这个 WebViewCoreThread 启动.正如 AdMob 引用中的状态,我在我的 Activity 的 onDestroy() 方法中对这个 AdView 调用了 destroy().我也更改了我的代码,在我的 onPause() 方法中调用 AdView.onDestroy().但似乎没有什么会导致 WebViewCoreThread 停止.我想,如果那个线程粘在我身边,我会没事的.但是如果我一遍又一遍地启动我的活动,这个线程开始使用我的 CPU 的 8% 到 25% 之间的任何地方,即使我的活动不在前台.

My Activity doesn't use any WebViews at all, but I was able to step through my Activity's initialization, and noticed that this WebViewCoreThread starts when I instantiate the AdMob AdView object. As state in the AdMob's references, I call destroy() on this AdView in my Activity's onDestroy() method. And I aso changed my code to call AdView.onDestroy() in my onPause() method. But nothing seems to be causing the WebViewCoreThread to stop. I guess, I'm okay if that thread sticks around. But if I start my Activity several times over and over again, this thread starts using anywhere between 8 to 25% of my CPU, even my activity is not in the foreground.

我注意到其他一些用户说您必须调用 WebView.onPause() 作为纠正措施.(http://stackoverflow.com/questions/2040963/webview-threads-never-stop-webviewcorethread-cookiesyncmanager-http0-3) 但这对我来说是不可能的,因为我的网络视图是由 AdMob 的 AdView 创建的.我还更改了我的代码,为 mt Admob AdView 的容器 LinearLayout 对象调用 .removeAllViews(),然后调用 System.gc() 强制垃圾收集,但似乎没有什么能杀死我的 WebViewCoreThread,最终它开始占用 CPU,直到我强制-杀死我的应用程序的进程.

I noticed a few other users saying that you must call WebView.onPause() as a corrective action. (http://stackoverflow.com/questions/2040963/webview-threads-never-stop-webviewcorethread-cookiesyncmanager-http0-3) But this isn't directly possible for me, since my web view is created by AdMob's AdView. I also changed my code to call .removeAllViews() for mt Admob AdView's container LinearLayout object, and then call System.gc() to force garbage collection, but nothing seems to kill my WebViewCoreThread and eventually it starts eating up the CPU until I force-kill my app's process.

任何线索为什么 AdMob 会这样做,以及如何强制终止该线程?

Any clues why AdMob is doing this, and how I can force this thread to be killed?

我附加了一个我创建的类来封装 AdView 创建和销毁.我在我的活动初始化中调用了这个类的 getNewAd() 方法.我在我的 Activity 的 onPause() 和 onDestroy() 方法中调用了这个类的 removeAd():

I'm attaching a class that I created to encapsulate AdView creation and destruction. I call this class's getNewAd() method in my activity's initialization. And I call this class's removeAd() in my Activity's onPause() and onDestroy() methods:

package com.shiprack.client;

import com.google.ads.AdRequest;
import com.google.ads.AdSize;
import com.google.ads.AdView;
import com.mobclix.android.sdk.Mobclix;
import com.mobclix.android.sdk.MobclixMMABannerXLAdView;

import android.app.Activity;
import android.view.Gravity;
import android.view.ViewGroup.LayoutParams;
import android.widget.LinearLayout;

public class AdManager {
    public AdManager(EventLog logger, LinearLayout container, Activity activity) {
        _container = container;
        _activity = activity;
        _eventLogger = logger;
    }

    public void setNetwork(int network) {
        _network = network;
    }

    public void getNewAd() {
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT);
        params.gravity = Gravity.CENTER;
        switch (_network) {
            case TrackDatabase.AD_NETWORK_ADMOB: {
                _admobBanner = new AdView(_activity, AdSize.BANNER, "a14dc419375634c");
                _container.addView(_admobBanner, params);
                _admobBanner.loadAd(new AdRequest());
                break;
            }
            case TrackDatabase.AD_NETWORK_MOBCLIX: {
                Mobclix.onCreate(_activity);
                _mobclixBanner = new MobclixMMABannerXLAdView(_activity);
                _container.addView(_mobclixBanner, params);
                _mobclixBanner.getAd();
                break;
            }
        }
    }

    public void removeAd() {
        switch (_network) {
            case TrackDatabase.AD_NETWORK_ADMOB: {
                _admobBanner.destroy();
                break;
            }
            case TrackDatabase.AD_NETWORK_MOBCLIX: {
                _mobclixBanner.cancelAd();
                break;
            }
        }
        _container.removeAllViews();
    }

    private EventLog _eventLogger;
    private LinearLayout _container;
    private Activity _activity;
    private AdView _admobBanner;
    private MobclixMMABannerXLAdView _mobclixBanner;
    private int _network;
}

推荐答案

不确定是否还有人需要此信息,但我自己一直在寻找解决方案.显然 AdMob 仍然存在缺陷.

Not sure if anybody still needs this information, but I have been searching for a solution to this myself. Apparently AdMob is still flawed.

唯一的问题是,这将阻止所有 WebView 在后台运行.仅当您的应用依赖此操作时才会出现问题.

The only problem is, this will stop all WebViews from running in the background. Only a problem if your app depends on this to operate.

添加到 onPause() :

new WebView(this).pauseTimers();

onResume() :

new WebView(this).resumeTimers();

这来自一位声称正在调查的 Google 员工:https://groups.google.com/d/msg/google-admob-ads-sdk/Qu4G19NFAuI/wcNkoV0AeDUJ

This came from a Google Employee who claims that they are looking into it: https://groups.google.com/d/msg/google-admob-ads-sdk/Qu4G19NFAuI/wcNkoV0AeDUJ

这篇关于即使父 Activity 暂停,admob AdView 使用的 WebViewCoreThread 也在使用高 CPU的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

How to target newer versions in .gitlab-ci.yml using auto devops (java 11 instead of 8 and Android 31 instead of 29)(如何在.gitlab-ci.yml中使用自动开发工具(Java 11而不是8,Android 31而不是29)瞄准较新的版本)
Android + coreLibraryDesugaring: which Java 11 APIs can I expect to work?(Android+core LibraryDesugering:我可以期待哪些Java 11API能够工作?)
How to render something in an if statement React Native(如何在If语句中呈现某些内容Reaction Native)
How can I sync two flatList scroll position in react native(如何在本机Reaction中同步两个平面列表滚动位置)
Using Firebase Firestore in offline only mode(在仅脱机模式下使用Firebase FiRestore)
Crash on Google Play Pre-Launch Report: java.lang.NoSuchMethodError(Google Play发布前崩溃报告:java.lang.NoSuchMethodError)