Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Admob on Multiple Activities?

I have 7 Activities in my application. I wants to display admob in every activity

Whether i have to create each AdView in every activity?

or

is there any alternative to reuse previous activity container OR prevent it from destroy so can i use in next activity....

Any code or hint we'll b appreciate.

Thankx

like image 848
MicroEyes Avatar asked Apr 19 '12 18:04

MicroEyes


People also ask

Can an app have multiple activities?

Most apps contain multiple screens, which means they comprise multiple activities. Typically, one activity in an app is specified as the main activity, which is the first screen to appear when the user launches the app. Each activity can then start another activity in order to perform different actions.

How many ad units can be created in AdMob?

Thus, if your app contains only banner ads, it can have only one Ad Unit ID. If your app contains banner and interstitial ads, it can have two Ad Unit IDs.


2 Answers

I DID this. Thankx to yorkw comment. This is not an efficient code. But you can modify accordingly. That reduces your code for each activity.

Just Extends "TestingAdmobActivity" & call SetupAds() to call your advs.

My SuperClass "TestingAdmobActivity.java"

package com.test.myadmob;

import com.google.ads.Ad;
import com.google.ads.AdListener;
import com.google.ads.AdRequest;
import com.google.ads.AdRequest.ErrorCode;
import com.google.ads.AdSize;
import com.google.ads.AdView;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.LinearLayout;

public class TestingAdmobActivity extends Activity implements AdListener{
    public AdView adView;
    public String ADV_PUB_ID = "a14e2fb60918999";
    private boolean adVisible = true;
    LinearLayout layout;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.i("Admob", "Calling External");
    }

    public void SetupAds(){ 
    Log.i("AdMob", "Start Setup");
    layout = new LinearLayout(this);
    layout.setOrientation(LinearLayout.VERTICAL);
    layout.setGravity(android.view.Gravity.BOTTOM | android.view.Gravity.CENTER_HORIZONTAL);   //To put AdMob Adv to Bottom of Screen
    Log.i("AdMob", "End Layout Setup");

    addContentView(layout, new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));

    adView = new AdView(this, AdSize.BANNER, ADV_PUB_ID);
    adView.setAdListener(this);
    Log.i("AdMob", "Init complete Adview");

    layout.addView(adView, new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
    Log.i("AdMob", "Done AddView Layout");

    AdRequest request = new AdRequest();    
    request.addTestDevice(AdRequest.TEST_EMULATOR);
    request.addKeyword("LifeOK");

    adView.loadAd(request);

    Log.i("AdMob", "End Setup");    
    }

    private Handler handler = new Handler() 
    {
        public void  handleMessage(Message msg) 
        {
            switch (msg.what)
            {
                case 0:     //Disable Adv
                    if (adVisible)
                        adVisible = false;
                    break;

                case 1:     //Enable Adv
                    if (!adVisible)
                    {
                        Log.i("AdMob", "Case 1");                       
                        adVisible = true;
                    }
                    break;

                case 2:     //Enable but Hide Adv
                        adView.setVisibility(View.GONE);                
                    break;

                case 3:     //Enable but Show Adv
                        adView.setVisibility(View.VISIBLE);
                    break;

                default:
                    break;
            }
        }
    };

    public void DisableAds()
    {
        Log.i("AdMob", "Request Disable Adv");
        handler.sendEmptyMessage(0);
    }

    public void EnableAds()
    {
        Log.i("AdMob", "Request Enable Adv");
        handler.sendEmptyMessage(1);
    }

    public void HideAdv()  //Enable Adv but Hide
    {
        Log.i("AdMob", "Request Hide Adv");
        handler.sendEmptyMessage(2);
    }

    public void ShowAdv()  //Show Adv
    {
        Log.i("AdMob", "Request Show Adv");
        handler.sendEmptyMessage(3);
    }

    @Override
    public void onDismissScreen(Ad arg0) {
        // TODO Auto-generated method stub
        Log.d("AdMob", "Dismiss Screen");
    }

    @Override
    public void onFailedToReceiveAd(Ad arg0, ErrorCode arg1) {
        // TODO Auto-generated method stub
        Log.d("AdMob", "failed to receive ad (" + arg1 + ")");    
    }

    @Override
    public void onLeaveApplication(Ad arg0) {
        // TODO Auto-generated method stub
        Log.d("AdMob", "Leaving Application");
    }

    @Override
    public void onPresentScreen(Ad arg0) {
        // TODO Auto-generated method stub
        Log.d("AdMob", "Present Screen");
    }

    @Override
    public void onReceiveAd(Ad arg0) {
        // TODO Auto-generated method stub
        Log.d("AdMob", "Adv Received");
    }
}

My FirstActivityClass "NewActivity_1.java"

package com.test.myadmob;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;

public class NewActivity_1 extends TestingAdmobActivity {

     /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Log.i("Admob", "OnCreate");
        SetupAds();
        Log.i("Admob", "Done");

        Button button1 = (Button) findViewById(R.id.button1);
        button1.setOnClickListener(new View.OnClickListener() {         
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Log.i("Admob", "Going to Activity 2");
                Intent mainIntent = new Intent().setClass(NewActivity_1.this, NewActivity_2.class);
                startActivity(mainIntent);
            }
        });
    }
}

My SecondActivityClass "NewActivity_2.java"

package com.test.myadmob;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;

public class NewActivity_2 extends TestingAdmobActivity {

     /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Log.i("Admob", "OnCreate");
        SetupAds();
        Log.i("Admob", "Done");

        Button button1 = (Button) findViewById(R.id.button1);
        button1.setOnClickListener(new View.OnClickListener() {         
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Log.i("Admob", "Going Back to Activity 1");
                finish();
            }
        });
    }
}

My AndroidManifest.xml

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

    <uses-sdk android:minSdkVersion="7" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:label="@string/app_name"
            android:name=".NewActivity_1" >
            <intent-filter >
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".TestingAdmobActivity" ></activity>
        <activity android:name=".NewActivity_2" ></activity>

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

        </activity>
    </application>

    <!-- AdMob SDK requires Internet permission -->
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> 
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />              <!-- to get Android Device ID -->   

</manifest>

Note: for the sake of permissions used by google admob sdk, i have to build this on android 4.0 sdk with min-sdk version 7

like image 152
MicroEyes Avatar answered Oct 25 '22 20:10

MicroEyes


In my app I have a cache of 0..12 ads at any given time. I'm reusing them accross different Fragments in an endless ViewPager. The caching class is in charge of loading the providing the ads to the Fragments.

The trick is to:

  1. Call the AdView's onDestory only when you're sure you're done with that AdView instance for good. This means that the Fragments themselves are not in charge of this.

  2. Passing the AdView's themselves between the Fragments, we need to remember to detach each AdView from its hierarchy:

(only on the UI thread of course):

public void detachFromHirerchy() 
{
    View adView = getAdView();
    if ( adView != null )
    {
        ViewGroup parent = (ViewGroup) adView.getParent();
        if (parent != null) 
        {
            parent.removeView( adView );
        }
    }

}
like image 45
Vaiden Avatar answered Oct 25 '22 20:10

Vaiden