因此,我试图让它在奖励用户之后自动关闭广告,这是我到目前为止所获得的。
请记住,我是Android Studio和Java整体的新手。如果它们隔开,它们将采用不同的方法。
private RewardedVideoAd mAd;
AdView adView;
void init_admob(){
adView = (AdView) findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder().setRequestAgent("android_studio:ad_template").build();
adView.loadAd(adRequest);
mAd = MobileAds.getRewardedVideoAdInstance(this);
mAd.setRewardedVideoAdListener(this);
loadRewardedVideoAd();
}
public void onRewarded(RewardItem reward) {
Toast.makeText(this, reward.getAmount()+ " " +getString(R.string.points_received), Toast.LENGTH_SHORT).show();
award(reward.getAmount(),getString(R.string.admob_credit));
exitAd();
}
public void exitAd() {
//dispatchKeyEvent(new KeyEvent (KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_BACK)); Tried But didn't work (No Errors Just didn't work)
//dispatchKeyEvent(new KeyEvent (KeyEvent.ACTION_UP, KeyEvent.KEYCODE_BACK)); Tried but didn't work (No Errors Just didn't work)
try {
LinearLayout linLay = (LinearLayout) findViewById(R.id.adView);
linLay.removeView(adView);
} catch (Exception e) {
e.printStackTrace();
}
}
LinLay错误
java.lang.ClassCastException: com.google.android.gms.ads.AdView cannot be cast to android.widget.LinearLayout
所以我真的不知道该尝试什么,或者在广告获得任何帮助后,如何关闭AdView!
答案 0 :(得分:1)
我认为不可能。从法律上讲也是不对的。展示广告时,应等待用户点击或取消广告。如果您以编程方式关闭,则意味着您已经有效地迫使用户取消了广告。 (这可能对用户有利,但请从广告客户的角度考虑)
答案 1 :(得分:0)
您无法关闭应用内广告,但这是我所做的。
private fun showRewardedVideo() {
if (mRewardedAd.isLoaded) {
mRewardedAd.show(
this,
object : RewardedAdCallback() {
override fun onUserEarnedReward(
rewardItem: RewardItem
) {
Toast.makeText(this@MainActivity, "onUserEarnedReward", Toast.LENGTH_LONG).show()
addCoins(rewardItem.amount)
// come back to ad activity
startActivity(Intent(this@MainActivity, MainActivity2::class.java))
}