我想确保Android上使用Android AdMob SDK的插页式广告可以关闭。 经过一些研究,在我看来,由于广告是一项单独的活动,这是不可能的。 我想要做的是在5秒后关闭广告。 有人可以向我保证,关闭插页式广告是不可能的,或者是否存在黑客攻击?
答案 0 :(得分:10)
这将是一个非常晚的答案,但我遇到了类似的问题。一个技巧可以是以编程方式调用后退按钮事件。
this.dispatchKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_BACK));
每当您按下后退按钮时,插页式广告都会关闭,因此触发后退按钮事件最终会关闭插页式广告。休息取决于要求。
答案 1 :(得分:1)
无法以编程方式关闭插页式广告。
答案 2 :(得分:1)
您可以通过实现finish()
来获取Admob非页内广告活动的引用,并在要关闭它时调用ActivityLifecycleCallbacks
:
private final Application.ActivityLifecycleCallbacks activityLifecycleCallbacks = new Application.ActivityLifecycleCallbacks() {
@Override
public void onActivityCreated(@NonNull Activity activity, @Nullable Bundle savedInstanceState) {
Log.e(TAG, "onActivityCreated: " + activity.getClass());
}
@Override
public void onActivityStarted(@NonNull Activity activity) {
Log.e(TAG, "onActivityStarted: " + activity.getClass());
}
@Override
public void onActivityResumed(@NonNull Activity activity) {
Log.e(TAG, "onActivityResumed: " + activity.getClass());
}
@Override
public void onActivityPaused(@NonNull Activity activity) {
Log.e(TAG, "onActivityPaused: " + activity.getClass());
}
@Override
public void onActivityStopped(@NonNull Activity activity) {
Log.e(TAG, "onActivityStopped: " + activity.getClass());
}
@Override
public void onActivitySaveInstanceState(@NonNull Activity activity, @NonNull Bundle outState) {
Log.e(TAG, "onActivitySaveInstanceState: " + activity.getClass());
}
@Override
public void onActivityDestroyed(@NonNull Activity activity) {
Log.e(TAG, "onActivityDestroyed: " + activity.getClass());
}
};
MyApplication.getInstance().registerActivityLifecycleCallbacks(activityLifecycleCallbacks);
E/TAG: onActivityCreated: class com.google.android.gms.ads.AdActivity
E/TAG: onActivityStarted: class com.google.android.gms.ads.AdActivity
E/TAG: onActivityResumed: class com.google.android.gms.ads.AdActivity
答案 3 :(得分:0)
对于新读者。 使用:
Intent intent = new Intent(activity, activity.getClass());
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
activity.startActivity(intent);
@Override
public void onAdOpened() {
// Code to be executed when the ad is displayed.
Log.d("mInterstitialAd", "onAdOpened ");
fullscreenAdShowing = true;
new Handler(Looper.getMainLooper()).postDelayed(new Runnable() {
@Override
public void run() {
if(!fullscreenAdShowing){
return;
}
Log.d("mInterstitialAd", "onAdOpened Handler 5 seconds run");
Intent intent = new Intent(activity, activity.getClass());
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
activity.startActivity(intent);
}
}, 5000);
}
@Override
public void onAdClosed() {
// Code to be executed when when the interstitial ad is closed.
Log.d("mInterstitialAd", "onAdClosed loadAd");
fullscreenAdShowing = false;
mInterstitialAd.loadAd(new AdRequest.Builder().build());
}