Android - 在应用关闭后,重新评估的视频广告永远不会再次加载

时间:2017-04-17 18:00:40

标签: android firebase admob

我是Android新视频广告的新手。我试图创建一个带有奖励视频广告的应用,如Rewarded Ads: Publishers Get Started

中所述

应用程序中有一个按钮,允许用户查看激励的视频。视频加载后启用该按钮。

我第一次打开应用程序时一切正常。我可以通过按下按钮查看多个视频广告。问题是,关闭应用程序后,按后退按钮,然后再次打开它。然后,广告再也不会加载。我没有收到来自RewardedVideoAdListener的消息,我把Toast放在非常实用的功能中。根本就没有反馈。我应该能够在onRewardedVideoAdFailedToLoad中查看Toast,但即便如此。就好像代码完全无法执行。

正如我所说,我是Android新视频广告的新手。也许我做错了什么,但我无法弄清楚是什么。我已经搜索了互联网,但我找不到任何答案。有些人在加载广告时提到了问题,但在应用程序关闭并再次打开后没有提到广告未加载的具体问题。我也检查了这个问题,但没有人回答: Question on StackOverflow

我在不同的手机上运行应用程序,我总能找到这个问题。

任何人都可以帮助我吗?

抱歉我的英文。我不是母语人士。

以下是我主要活动中的代码:

public class MainActivity extends AppCompatActivity implements RewardedVideoAdListener {

    private Button mWatchButton;
    private TextView mRewardLabel;
    private RewardedVideoAd mAd;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        mWatchButton = (Button)findViewById(R.id.btn_watch);

        mAd = MobileAds.getRewardedVideoAdInstance(this);
        mAd.setRewardedVideoAdListener(this);
        loadAd();

        mWatchButton.setEnabled(false);
        mWatchButton.setOnClickListener(new View.OnClickListener(){
                @Override
                public void onClick(View v) {
                    if(mAd.isLoaded()){
                        mAd.show();
                    }
                }
            }
        );

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });
    }

    private void loadAd(){
        if(!mAd.isLoaded()){
            mAd.loadAd("ca-app-pub-3940256099942544/5224354917", new AdRequest.Builder().build());
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onRewardedVideoAdLoaded() {
        mWatchButton.setEnabled(true);
        Toast.makeText(this, "onRewardedVideoAdLoaded", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onRewardedVideoAdOpened() {
        Toast.makeText(this, "onRewardedVideoAdOpened", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onRewardedVideoStarted() {
        Toast.makeText(this, "onRewardedVideoStarted", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onRewardedVideoAdClosed() {
        Toast.makeText(this, "onRewardedVideoAdClosed", Toast.LENGTH_SHORT).show();
        mWatchButton.setEnabled(false);
        loadAd();
    }

    @Override
    public void onRewarded(RewardItem rewardItem) {
        Toast.makeText(this, "onRewarded", Toast.LENGTH_SHORT).show();
        mRewardLabel.setText(String.format(Locale.getDefault(), "You got %d %s!", rewardItem.getAmount(), rewardItem.getType()));
    }

    @Override
    public void onRewardedVideoAdLeftApplication() {
        Toast.makeText(this, "onRewardedVideoAdLeftApplication", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onRewardedVideoAdFailedToLoad(int i) {
        Toast.makeText(this, "onRewardedVideoAdFailedToLoad", Toast.LENGTH_SHORT).show();
    }


    @Override
    public void onPause() {
        super.onPause();
        mAd.pause(this);
    }

    @Override
    public void onResume() {
        super.onResume();
        mAd.resume(this);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        mAd.destroy(this);
    }


}

以下是我布局的代码:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="com.xxxxxx.MainActivity"
    tools:showIn="@layout/activity_main">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        android:id="@+id/textView" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Ver o anúncio"
        android:id="@+id/btn_watch"
        android:layout_below="@+id/textView"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginTop="58dp"/>

</RelativeLayout>

我在Android Studio的logcat中没有收到任何警告或错误消息。

我正在使用:

classpath 'com.google.gms:google-services:3.0.0'

    dependencies {
        compile fileTree(dir: 'libs', include: ['*.jar'])
        testCompile 'junit:junit:4.12'
        compile 'com.android.support:appcompat-v7:23.4.0'
        compile 'com.android.support:design:23.4.0'
        compile 'com.google.firebase:firebase-ads:9.4.0'
    }

apply plugin: 'com.google.gms.google-services'

1 个答案:

答案 0 :(得分:1)

当应用再次进入前台时,请求另一个广告:

@Override
public void onResume() {
    super.onResume();
    loadAd();
}