为不同的活动实施admob

时间:2015-03-27 15:44:12

标签: android eclipse admob

我总共有三个活动,我正在为每个活动实施admob,每个活动都有自己的横幅,当活动发生变化时,其他活动因为背景中的广告加载而稍微挂起,有什么办法吗切换时,所有活动中都会出现一个横幅,以避免延误。

3 个答案:

答案 0 :(得分:17)

您只需在应用程序类中加载广告即可 在任何活动中使用它。

您可以下载demo

就像我一样,

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>

答案 1 :(得分:2)

理想的方法是为每个screens使用片段。这样,您就可以使用具有单一广告的单个活动。

如果您想要使用多个活动,那么我所知道的唯一解决方法是使用静态方法加载广告:

public class MyAdView {
    public static void SetAD(AdView adView){  
        AdRequest adRequest = new AdRequest.Builder()
            .addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
            .build();
        adView.loadAd(adRequest);
    }

}

<强>用法:

public class SomeActivity extends Activity {
    private AdView adView;  

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.caller_main);
        MyAdView.SetAd((AdView)findViewById(R.id.adView));
    }   
}

答案 2 :(得分:0)

我知道很久以前就已经回答了这个问题,但是当您已经创建了包含许多活动的整个应用程序时,我想我有一个更好的方法。尽管RBK的解决方案确实不错,但它要求您浏览每个活动的布局文件并添加以下LinearLayout:

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

但是,如果您有很多不同的活动和很多不同的布局文件(特别是如果您像我一样并且在完成大部分应用程序之前就忘记了admob集成),这可能会变得非常乏味。我发现最简单的改进是创建了ActivityAppCompatActivity的子类来处理广告的放置和加载:

与RBK的解决方案类似,我们创建一个Application类 App 。但是,这将有一些小的变化。我们希望将横幅广告放置在活动中的特定ViewGroup(例如layad)中,而不希望将其自动添加到活动的底部,而无需向布局文件添加任何内容。我们用执行此操作的方法替换loadAd

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-3940256099942544/6300978111");
        // Request for Ads
        AdRequest adRequest = new AdRequest.Builder().build();

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

    // run by each activity to show the banner ad on the bottom
    public void addBannerAdToActivity(AD_ACTIVITY context) {
        // get the root linear layout. We'll insert our banner ad at the end of it
        LinearLayout rootView = context.getRootLayout();

        // if the banner already has a parent, remove it...
        if (adView.getParent() != null) {
            ViewGroup par = (ViewGroup) adView.getParent();
            par.removeView(adView);
        }

        // set 'rootView' as the new parent of the banner:
        rootView.addView(adView);

        adView.setVisibility(View.VISIBLE); // just incase the banner isn't visible for some reason

        // Now the ad will be placed on the bottom of the activity requesting it
    }
}

然后,我们创建一个扩展了AppCompatActivity的抽象类 AD_ACTIVITY

public abstract class AD_ACTIVITY extends AppCompatActivity {
    
    private LinearLayout root;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // The content view of an AD_ACTIVITY is a linear layout with the first element being the main content and the second element being the banner ad:
        root = new LinearLayout(this); // create a new LinearLayout
        root.setOrientation(LinearLayout.VERTICAL);
   
        super.setContentView(root); // set that as the content view...
    }

    @Override
    public void setContentView(int layoutResID) {
        // Inflate the layout file and get a reference to it:
        ViewGroup inflatedView = (ViewGroup) ((LinearLayout) getLayoutInflater().inflate(layoutResID,root)).getChildAt(0);
        
        // we want the main content to take up the whole vertical screen with space at the bottom for the banner ad
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        params.weight = 1; // to do this, we set the weight of 'inflatedView' to 1
        inflatedView.setLayoutParams(params);

        // LOAD BANNER AD AND ADD IT TO 'root':
        ((App) getApplicationContext()).addBannerAdToActivity(this);
    }

    @Override
    protected void onResume() {
        super.onResume();

        // LOAD BANNER AD AND ADD IT TO 'root':
        ((App) getApplicationContext()).addBannerAdToActivity(this);
    }

    public LinearLayout getRootLayout() {
        return root;
    }
}

现在我们要做的是遍历所有活动并将其扩展为AD_ACTIVITY而不是AppCompatActivity。例如:

MainActivity.java

public class MainActivity extends AppCompatActivity {

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

更改为:

public class MainActivity extends AD_ACTIVITY {

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

更改横幅的位置,行为和位置非常容易,因为对AD_ACTIVITY所做的任何更改都将应用于扩展横幅的所有活动。