Android 在多个活动上使用相同的已加载AdMob

Android 在多个活动上使用相同的已加载AdMob,android,admob,Android,Admob,我有一个应用程序,你可以打开无限数量的活动,就像文章应用程序一样,每次你点击一篇文章,就会创建一个新的活动。我正在使用此代码加载AdMob广告 AdView adView = new AdView(context); adView.setAdSize(AdSize.BANNER); adView.setAdUnitId(Config(context, "banner_unit_id")); AdRequest adRequest = new AdRequest.Builder().build()

我有一个应用程序,你可以打开无限数量的活动,就像文章应用程序一样,每次你点击一篇文章,就会创建一个新的活动。我正在使用此代码加载AdMob广告

AdView adView = new AdView(context);
adView.setAdSize(AdSize.BANNER);
adView.setAdUnitId(Config(context, "banner_unit_id"));
AdRequest adRequest = new AdRequest.Builder().build();

if(adView.getAdSize() != null || adView.getAdUnitId() != null){
    adView.loadAd(adRequest);
}  

现在我想做的是在多个活动中使用相同的加载广告,这样就不必在每个活动中反复加载广告。有什么方法可以做到这一点吗?

你不能在不破坏其他东西的情况下做到这一点


实现您所追求的目标的推荐方法是将您的内容加载到新片段中,并保持单个活动的完整性,以便在其中显示它们。

您可以只在应用程序类中加载ad,然后 在任何活动中使用它

实际的

当我这么做的时候

应用程序类

import android.app.Application;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdSize;
import com.google.android.gms.ads.AdView;

public class App extends Application {

AdView adView;

@Override
public void onCreate() {
    // TODO Auto-generated method stub

    super.onCreate();

    adView = new AdView(this);
    adView.setAdSize(AdSize.SMART_BANNER);
    adView.setAdUnitId("ca-app-pub-1267746788642565/8418489933");
    // Request for Ads
    AdRequest adRequest = new AdRequest.Builder().build();

    // Load ads into Banner Ads
    adView.loadAd(adRequest);
}

public void loadAd(LinearLayout layAd) {

    // Locate the Banner Ad in activity xml
    if (adView.getParent() != null) {
        ViewGroup tempVg = (ViewGroup) adView.getParent();
        tempVg.removeView(adView);
    }

    layAd.addView(adView);

}
}
主要活动

public class MainActivity extends Activity {

App app;
LinearLayout layAd;

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

    layAd = (LinearLayout) findViewById(R.id.layad);

    app = (App) getApplication();
    app.loadAd(layAd);

    Button btnNext = (Button) findViewById(R.id.next);
    btnNext.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Intent iNext = new Intent(MainActivity.this,
                    SecondActivity.class);
            startActivity(iNext);
        }
    });
}

@Override
protected void onResume() {
    // TODO Auto-generated method stub
    app.loadAd(layAd);
    super.onResume();
}
}
第二项活动

public class SecondActivity extends Activity {

App app;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_second);

    LinearLayout layAd = (LinearLayout) findViewById(R.id.layad);

    app = (App) getApplication();
    app.loadAd(layAd);
}
}
清单xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.admobdemo"
android:versionCode="1"
android:versionName="1.0" >

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="21" />

<application
    android:name="com.example.admobdemo.App"
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.example.admobdemo.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>
    <activity
        android:name="com.example.admobdemo.SecondActivity"
        android:label="@string/app_name" >
    </activity>

    <meta-data
        android:name="com.google.android.gms.version"
        android:value="@integer/google_play_services_version" />

    <activity
        android:name="com.google.android.gms.ads.AdActivity"
        android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize" />
</application>
</manifest>

主活动布局xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="${relativePackage}.${activityClass}" >

<LinearLayout
    android:id="@+id/layad"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical" >
</LinearLayout>

<Button
    android:id="@+id/next"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="${relativePackage}.${activityClass}" >

<LinearLayout
    android:id="@+id/layad"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical" >
</LinearLayout>
</LinearLayout>

和第二个活动布局xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="${relativePackage}.${activityClass}" >

<LinearLayout
    android:id="@+id/layad"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical" >
</LinearLayout>

<Button
    android:id="@+id/next"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="${relativePackage}.${activityClass}" >

<LinearLayout
    android:id="@+id/layad"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical" >
</LinearLayout>
</LinearLayout>

-他们只做一个片段活动。加载外接程序,您将在其中加载无限数量的片段