猿问

打赏后如何自动关闭打赏广告 [Android-Studio]

所以我试图让它在奖励用户后自动关闭广告,这是我到目前为止所得到的。


请记住,我是 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!


慕容3067478
浏览 174回答 2
2回答

12345678_0001

我不认为这是可能的。这在法律上也是不正确的。当您展示广告时,您应该等待用户点击或取消广告。如果您以编程方式关闭,则意味着您已有效地强迫用户取消广告。(可能对用户有好处,但要站在广告商的角度考虑)

呼如林

您无法关闭应用内广告,但这是我所做的。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))                  }
随时随地看视频慕课网APP

相关分类

Java
我要回答