gpt4 book ai didi

android - Admob 不使用 AsyncTask

转载 作者:行者123 更新时间:2023-11-29 16:13:25 25 4
gpt4 key购买 nike

我可以使用 AsyncTask 加载 admob 广告吗?

我试过了,但在运行时收到很多警告,广告没有加载

创建新 AdView 实例时的警告之一是

07-06 09:57:31.170: W/webview(1113): java.lang.Throwable: Warning: A WebView method was called on thread 'AsyncTask #1'. All WebView methods must be called on the UI thread. Future versions of WebView may not support use on other threads.

我的方法是这样的

 @Override
protected AdView doInBackground(Activity... activityParam)
{
Looper.prepare();
activity = activityParam[0];
// Create the adView
AdView adView = new AdView(activity, AdSize.BANNER, "xxxxxxxxxxxxx");
return adView;
}

请告知是否应该在非 UI 线程上调用它,我这样做的唯一原因是为了防止 UI 阻塞..

编辑。

可能是我没有表达清楚我的问题,

我正在尝试在后台加载广告,因为它似乎会阻塞几秒钟,我不想阻止我的 Activity 界面。

阅读这篇文章 How to avoid Admob blocking the UI thread建议为避免 UI 阻塞应该使用 AsyncTask

那么我想知道是否可以替代在后台加载广告......?

最佳答案

问题是您在 doInBackground 方法中使用了 View ,而不是在 UI 线程中。只需在 onPostExecute(在 doInBackground 之后调用)或 onPreExecute(在 doInBackground 之前调用)方法中处理您的 View AsyncTaskUI 线程中调用。

关于android - Admob 不使用 AsyncTask,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11356121/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com