Admob广告不会显示在真正的Android设备上

时间:2017-02-07 04:01:22

标签: android admob ads live

我正在为我的应用使用admob广告。我已经从android studio中将默认活动视为admob活动,然后我更改了adunit id。当我在模拟器中运行时,它正在显示广告但是我在真实设备中运行相同的代码它没有显示任何内容,出现了一个空白屏幕。我正在使用三星J7和三星二人组来运行apk。没有任何表现,我搜索不同的方式,但无法找出错误。这是我的代码。 这是我的xml文件。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/hello_world" />

<!-- view for AdMob Banner Ad -->
<com.google.android.gms.ads.AdView

    android:id="@+id/adView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    ads:adSize="BANNER"
    ads:adUnitId="@string/banner_ad_unit_id"
    />

这里是活动课。

public class MainActivity extends AppCompatActivity {
// Remove the below line after defining your own ad unit ID.
private static final String TOAST_TEXT = "Test ads are being shown. "
        + "To show live ads, replace the ad unit ID in res/values/strings.xml with your own ad unit ID.";


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

    // Load an ad into the AdMob banner view.
    AdView adView = (AdView) findViewById(R.id.adView);
    AdRequest adRequest = new AdRequest.Builder()
            .addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
            .addTestDevice("ca-app-pub-3940256099942544/6300978111")
            .setRequestAgent("android_studio:ad_template").build();
    adView.loadAd(adRequest);

    // Toasts the test ad message on the screen. Remove this after defining your own ad unit ID.
    Toast.makeText(this, TOAST_TEXT, Toast.LENGTH_LONG).show();
}


@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();

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

    return super.onOptionsItemSelected(item);
}

}

我需要更改以展示自定义的直播广告。

2 个答案:

答案 0 :(得分:2)

<强>问题
您添加了AdRequest.DEVICE_ID_EMULATOR,因此您可以在模拟器中显示AdView(横幅广告),但不会添加到您的真实设备ID中。

  

ca-app-pub-3940256099942544/6300978111 ad-unit-id 不是设备ID。

<强>解决方案
https://firebase.google.com/docs/admob/android/targeting

请添加您的真实设备ID,请参阅链接。

答案 1 :(得分:0)

@ Ethan-Choi的答案应该被标记为已被接受,因为它回答了原始问题。我没有足够的声誉点来评论这个答案,但是在回答第二个问题时,OP在@ Ethan-Choi的评论中提到了:

  

“我在admob中创建了自己的添加但我无法在真实中显示该广告   设备。什么是显示自己的广告的程序“

解决方案可能是:AdRequest.Builder().setContentUrl(String contentUrl),其中contentUrl是自定义广告的网址。根据以下参考:

  

“setContentUrl()设置用于定位目的的内容网址”

https://developers.google.com/android/reference/com/google/android/gms/ads/AdRequest.Builder

相关问题