2017-10-08 8 views
0

질문이 있습니다.admob id from activity

내 앱에 Admob 배너를 추가했습니다.

코드 :

@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() 
      .setRequestAgent("android_studio:ad_template").build(); 
    adView.loadAd(adRequest); 
} 

XML :

<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="xxxxxx-xxxxx" /> 

내가 광고를 변경하려면 : mainactivity에서 adUnitId.

예를 들어 만약 == 1 개 광고 : adUnitId = XXXXXXX 다른 광고 : adUnitId = YYYYYYY

어떻게 그렇게 할 수 있습니까?

감사합니다.

편집 : AndroidManifest를

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="demo.com.bannertest"> 

    <!-- Include required permissions for Google Mobile Ads to run. --> 
    <uses-permission android:name="android.permission.INTERNET" /> 
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> 

    <application 
     android:allowBackup="true" 
     android:icon="@mipmap/ic_launcher" 
     android:label="@string/app_name" 
     android:roundIcon="@mipmap/ic_launcher_round" 
     android:supportsRtl="true" 
     android:theme="@style/AppTheme"> 

     <!-- This meta-data tag is required to use Google Play Services. --> 
     <meta-data 
      android:name="com.google.android.gms.version" 
      android:value="@integer/google_play_services_version" /> 

     <activity 
      android:name=".MainActivity" 
      android:label="@string/app_name"> 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 

       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> <!-- Include the AdActivity configChanges and theme. --> 
     <activity 
      android:name="com.google.android.gms.ads.AdActivity" 
      android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize" 
      android:theme="@android:style/Theme.Translucent" /> 
    </application> 

</manifest> 

mainactivity :

public class MainActivity extends AppCompatActivity { 

    @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() 
       .setRequestAgent("android_studio:ad_template").build(); 
     adView .setAdUnitId("ca-app-pub-3940256099942544/6300978111"); 
     adView.loadAd(adRequest); 
    } 
} 

activity_main :

<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"> 

    <!-- 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" /> 

</RelativeLayout> 

로그 캣 : 필수 XML 속성 "adUnitId"가 누락되었습니다.

답변

0

이 코드를 사용하여 실제적으로 AdView를 만듭니다.

View adContainer = findViewById(R.id.adMobView); 
    AdView mAdView = new AdView(context); 
    mAdView.setAdSize(AdSize.BANNER); 
    mAdView.setAdUnitId(YOUR_BANNER_ID); 
    ((RelativeLayout)adContainer).addView(mAdView); 
    AdRequest adRequest = new AdRequest.Builder().build(); 
    mAdView.loadAd(adRequest); 

그리고 XML 파일

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

     <RelativeLayout 
      android:id="@+id/adMobView" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_alignParentBottom="true"/> 

</RelativeLayout> 
+0

같이 adView .setAdUnitId (banner_id)에서이 코드를 사용; 작동하지. – michaelzX

+0

광고가 제대로 작동하여 xml 파일에 adUnitId를 설정 했습니까? –

+0

네, 잘 작동합니다. – michaelzX