在多个活动中使用相同的加载AdMob

时间:2015-07-25 12:44:07

标签: 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();

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

现在我要做的是在多个活动中使用相同的加载广告,因此广告不会在每个活动上反复加载。有没有办法做到这一点?

2 个答案:

答案 0 :(得分:1)

如果不打破其他原因,你就无法做到这一点。

实现目标的推荐方法是在新片段中加载内容,并保持单个活动完好无损地显示它们。

答案 1 :(得分:1)

你可以在应用程序类中加载广告 在任何活动中使用它。

实际ans

就像我一样,

App class

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>

和第二个活动布局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>
</LinearLayout>