2017-02-07 94 views
0

我正在爲我的應用使用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); 
} 

} 

我需要更改以顯示定製的實時廣告。

回答

1

問題
你加入AdRequest.DEVICE_ID_EMULATOR,因此可以顯示模擬器的AdView(旗),但你不添加到您的真實設備ID。

ca-app-pub-3940256099942544/6300978111ad-unit-id不是設備ID。

解決方案
https://firebase.google.com/docs/admob/android/targeting

請添加您的真實設備ID參考鏈接。

+0

是ID爲模擬器和真實設備有什麼不同? –

+0

@UmashankarB yes id對於兩者都不同。請記住爲應請求測試廣告的每個設備添加測試設備ID。設備ID由Mobile Ads SDK寫入系統日誌,因此您可以運行應用並檢查logcat來查找設備的ID。 – dipali

+0

實時廣告需要哪個ID(自定義廣告),請讓我有點困惑 –

0

@ Ethan-Choi的答案應該被標記爲接受,因爲它回答了原來的問題。我沒有足夠的聲譽分,對答案,但響應的第二個問題的任擇議定書要求在@阮經天,崔的言論發表評論:

「我在AdMob中創建自己的插件,但我無法顯示該廣告真正 設備什麼是顯示自己的廣告」

這有可能是該解決方案的過程:AdRequest.Builder().setContentUrl(String contentUrl),其中的contentURL是您的自定義廣告的網址。按下面的參考:

「setContentUrl()爲指定目標的內容的網址」

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