我正在爲我的應用使用admob廣告。我已經採取默認活動作爲來自android studio的admob活動,然後我改變了adunit id。當我在模擬器中運行時,它顯示了廣告,但是當我在真實設備上運行相同的代碼時它沒有顯示任何內容,出現了空白屏幕。我正在使用三星J7和三星二重奏來運行apk。沒有任何表現,我用不同的方式進行了搜索,但找不到什麼是錯的。這是我的代碼。 這是我的xml文件。Admob廣告不會在真實Android設備上顯示
<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);
}
}
我需要更改以顯示定製的實時廣告。
是ID爲模擬器和真實設備有什麼不同? –
@UmashankarB yes id對於兩者都不同。請記住爲應請求測試廣告的每個設備添加測試設備ID。設備ID由Mobile Ads SDK寫入系統日誌,因此您可以運行應用並檢查logcat來查找設備的ID。 – dipali
實時廣告需要哪個ID(自定義廣告),請讓我有點困惑 –