我使用的是320×50 banner ad thru MoPub SDK for Android,使用的 SDK 是 mopub-sdk:5.4.1@aar, 遇到的問題是,有時候主畫面會被卡住,從logcat 來看,會程式會掛掉的 log:
2019-01-04 04:14:20.978 D/MoPub: Initializing MoPub with ad unit: 2cd91442492e4afaa124b68f4e8d9fa0
2019-01-04 04:14:21.631 D/MoPub: MoPubIdentifier initialized.
2019-01-04 04:14:21.696 D/MainTabActivity: mopub sdk init finished
2019-01-04 04:14:21.700 D/MainTabActivity: post mopub to loadAd().
2019-01-04 04:14:21.708 D/MoPub: Avid is available via reflection.
2019-01-04 04:14:21.709 D/MoPub: Moat is available via reflection.
2019-01-04 04:14:21.709 D/MoPub: Loading url: https://ads.mopub.com/m/ad?v=6&id=2cd91442492e4afaa124b68f4e8d9fa0&nv=5.4.1&dn=Xiaomi%2CRedmi%20Note%205%2Cwhyred&bundle=com.lwfd.datelinethailand&z=%2B0800&o=p&w=1080&h=2160&sc=2.75&mcc=466&mnc=89&cn=T%20Star&ct=2&av=1.4.2&udid=mp_tmpl_advertising_id&dnt=mp_tmpl_do_not_track&mr=1&android_perms_ext_storage=0&vv=3
正常執行的 log 1號:
2019-01-04 05:01:45.037 D/MoPub: Initializing MoPub with ad unit: 2cd91442492e4afaa124b68f4e8d9fa0
2019-01-04 05:01:45.908 D/MoPub: MoPubIdentifier initialized.
2019-01-04 05:01:46.364 D/MoPub: Posting AsyncTask to main thread for execution.
2019-01-04 05:01:46.372 D/MainTabActivity: mopub sdk init finished
2019-01-04 05:01:46.393 D/MainTabActivity: post mopub to loadAd().
2019-01-04 05:01:46.403 D/MoPub: Avid is available via reflection.
2019-01-04 05:01:46.403 D/MoPub: Moat is available via reflection.
2019-01-04 05:01:46.404 D/MoPub: Loading url: https://ads.mopub.com/m/ad?v=6&id=2cd91442492e4afaa124b68f4e8d9fa0&nv=5.4.1&dn=Xiaomi%2CRedmi%20Note%205%2Cwhyred&bundle=com.lwfd.datelinethailand&z=%2B0800&o=p&w=1080&h=2160&sc=2.75&mcc=466&mnc=89&cn=T%20Star&ct=2&av=1.4.2&udid=mp_tmpl_advertising_id&dnt=mp_tmpl_do_not_track&mr=1&android_perms_ext_storage=0&vv=3
2019-01-04 05:01:47.223 D/MoPub: Successfully hit tracking endpoint: https://ads.mopub.com/m/open
2019-01-04 05:01:47.801 I/MoPub: Ad failed to load.
正常執行的 log 2號:
2019-01-04 05:12:12.647 D/MoPub: Initializing MoPub with ad unit: 2cd91442492e4afaa124b68f4e8d9fa0
2019-01-04 05:12:13.080 D/MoPub: MoPubIdentifier initialized.
2019-01-04 05:12:13.111 D/MainTabActivity: mopub sdk init finished
2019-01-04 05:12:13.233 D/MoPub: Posting AsyncTask to main thread for execution.
2019-01-04 05:12:13.611 D/MainTabActivity: post mopub to loadAd().
2019-01-04 05:12:13.620 D/MoPub: Avid is available via reflection.
2019-01-04 05:12:13.620 D/MoPub: Moat is available via reflection.
2019-01-04 05:12:13.621 D/MoPub: Loading url: https://ads.mopub.com/m/ad?v=6&id=2cd91442492e4afaa124b68f4e8d9fa0&nv=5.4.1&dn=Xiaomi%2CRedmi%20Note%205%2Cwhyred&bundle=com.lwfd.datelinethailand&z=%2B0800&o=p&w=1080&h=2160&sc=2.75&mcc=466&mnc=89&cn=T%20Star&ct=2&av=1.4.2&udid=mp_tmpl_advertising_id&dnt=mp_tmpl_do_not_track&mr=1&android_perms_ext_storage=0&vv=3
2019-01-04 05:12:14.117 D/MoPub: Successfully hit tracking endpoint: https://ads.mopub.com/m/open
2019-01-04 05:12:14.984 I/MoPub: Ad failed to load.
說明:發現正常執行時,會丟出 D/MoPub: Posting AsyncTask to main thread for execution. 這段 debug code 是從 mopub 的 asynctask class 所丟出:
https://github.com/mopub/mopub-android-sdk/blob/master/mopub-sdk/mopub-sdk-base/src/main/java/com/mopub/common/util/AsyncTasks.java
public static <P> void safeExecuteOnExecutor(final @NonNull AsyncTask<P, ?, ?> asyncTask, final @Nullable P... params) {
Preconditions.checkNotNull(asyncTask, "Unable to execute null AsyncTask.");
if (Looper.getMainLooper() == Looper.myLooper()) {
asyncTask.executeOnExecutor(sExecutor, params);
} else {
MoPubLog.d("Posting AsyncTask to main thread for execution.");
sUiThreadHandler.post(new Runnable() {
@Override
public void run() {
asyncTask.executeOnExecutor(sExecutor, params);
}
});
}
}
發現,註解掉 mPersonalInfoManager = MoPub.getPersonalInformationManager(); 可以降低mopub 造成UI freeze 的問題。如果在 activity.onCreate() 時,同時執行 MoPub.initializeSdk() 還有 MoPub.getPersonalInformationManager() 大約試3~4次即可試出一次 freeze, 註解掉 MoPub.getPersonalInformationManager(), 連試 10次 UI 都沒被卡。
暫時解法,透過 handler.postDelayed(new Runnable() { 把程式執行時間錯開,降低卡住機率,多試幾次還是會被卡住一次,這是是 mopub 蓋了一個更高蓋的 view , view 下面的 UI 還是可以被主程式控制和修改UI,問題是被蓋了一個 Loading 在整個程式上面,整個程式都無法被點選和操作。