为不同的活动实现 admob

Implementing admob for different activities(为不同的活动实现 admob)

本文介绍了为不同的活动实现 admob的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我总共有三个活动,我正在为每个活动实现 admob,每个活动都有自己的横幅,当活动发生变化时,另一个活动会因为后台加载广告而挂起一点,有什么办法可以切换时所有活动中都会出现一个横幅,以避免延迟.

I have three activities in total and i am implementing admob for each activity, every activity has its own banner and when the activity is changes the other activity hangs a little because of the ad loading in the background, is there any way that one banner appears in all the activities when switched to avoid delays.

推荐答案

你可以做到,只需在应用程序类中加载广告,然后在任何活动中使用它.

you can do it just load ad in application class and use it in any activity.

你可以下载demo

正如我所做的那样,

应用类

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>

这篇关于为不同的活动实现 admob的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:为不同的活动实现 admob

基础教程推荐