-2

I found a solution Please help solve the problem of the beginner. I need to pre-load the advertising and at the click of a button "Next" play ads(during the transition between the activities). In my example, it is not working properly. Please correct my code properly and I will be very grateful to you.

public class Home_5_9Eng extends AppCompatActivity implements View.OnClickListener {
Button btnBack2;
Button btnNext2;
InterstitialAd mInterstitialAd;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_home_5_9_eng);
    AdView mAdView = (AdView) findViewById(R.id.adView);
    AdRequest adRequest = new AdRequest.Builder().build();
    mAdView.loadAd(adRequest);
    btnBack2 = (Button) findViewById(R.id.btnBack2);
    btnBack2.setOnClickListener(this);
    btnNext2 = (Button) findViewById(R.id.btnNext2);
    btnNext2.setOnClickListener(this);
    mInterstitialAd = new InterstitialAd(this);
    mInterstitialAd.setAdUnitId(getResources().getString(R.string.window_ad_unit_id));
    AdRequest adRequestInter = new AdRequest.Builder().build();
    mInterstitialAd.loadAd(adRequestInter);
}

@Override
public void onClick(View v) {
    switch (v.getId()){
        case R.id.btnBack2:
            Intent intent = new Intent(this, Home_5_8Eng.class);
            startActivity(intent);
            break;
        case R.id.btnNext2:
            if (mInterstitialAd != null && mInterstitialAd.isLoaded()) {
                mInterstitialAd.show();
            }
            intent =new Intent(this, Home_5_10Eng.class);
            startActivity(intent);
            break;
    }
    finish();
}}

I found a solution

public class Home_5_1 extends AppCompatActivity implements View.OnClickListener {

    Button btnNext2;
    InterstitialAd mInterstitialAd;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home_5_1);
        AdView mAdView = (AdView) findViewById(R.id.adView);
        AdRequest adRequest = new AdRequest.Builder().build();
        mAdView.loadAd(adRequest);
        btnNext2 = (Button) findViewById(R.id.btnNext2);
        btnNext2.setOnClickListener(this);
        mInterstitialAd = new InterstitialAd(this);
        mInterstitialAd.setAdUnitId(getResources().getString(R.string.window_ad_unit_id));
        AdRequest adRequestInter = new AdRequest.Builder().build();
        mInterstitialAd.loadAd(adRequestInter);

        mInterstitialAd.setAdListener(new AdListener() {

            @Override
            public void onAdClosed() {
                super.onAdClosed();
                    if (!mInterstitialAd.isLoading() && !mInterstitialAd.isLoaded()) {
                    AdRequest adRequestInter = new AdRequest.Builder().build();
                    mInterstitialAd.loadAd(adRequestInter);
                    }
                    Intent intent = new Intent(Home_5_1.this, Home_5_2.class);
                    startActivity(intent);
            }
        });

    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btnNext2:
                if (mInterstitialAd != null && mInterstitialAd.isLoaded()) {
                    mInterstitialAd.show();
                } else {
                    Intent intent = new Intent(this, Home_5_2.class);
                    startActivity(intent);
                    break;
                }
                finish();

        }}}

2 Answers2

0

Do you have added com.google.android.gms.ads.AdActivity declaration to your manifest ???

You can refer from This and this. It will helps you find your error.

<?xml version="1.0" encoding="utf-8"?>

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

<!-- Include required permissions for Google Mobile Ads to run-->
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <!--This meta-data tag is required to use Google Play Services.-->
    <meta-data android:name="com.google.android.gms.version"
    android:value="@integer/google_play_services_version" />
    <activity
        android:name=".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>
    <!--Include the AdActivity configChanges and theme. -->
    <activity android:name="com.google.android.gms.ads.AdActivity"
        android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"
        android:theme="@android:style/Theme.Translucent" />
</application>

  • Manifest all right. Advertising played at the click of a hardware button (arrow). I want to play ads at the click of a button NEXT programmatic as it is written in my code – dreamboatDev Feb 02 '16 at 11:59
0
  • Never do Networking code onCreate,make it call or services or AsynTask

  • Create a function ShowAds() and call on click, it will work

    void ShowAds() { // code to show Banner or InterstitialAd
    }

  • Add .addTestDevice(AdRequest.DEVICE_ID_EMULATOR); like

    AdRequest adRequestInter = new AdRequest.Builder() .addTestDevice(AdRequest.DEVICE_ID_EMULATOR) // All emulators .addTestDevice("Your Device ID") // An example device ID .build();

Only in Development Mode or your Admob account will be cancelled.

Remove .addTestDevice(AdRequest.DEVICE_ID_EMULATOR); and .addTestDevice("AC98C820A50B4AD8A2106EDE96FB87D4") before publishing.

Code in comments :

public class Home_5_1 extends AppCompatActivity implements View.OnClickListener {

    Button btnNext2;
    InterstitialAd mInterstitialAd;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home_5_1);
        AdView mAdView = (AdView) findViewById(R.id.adView);
        AdRequest adRequest = new AdRequest.Builder().build();
        mAdView.loadAd(adRequest);
        btnNext2 = (Button) findViewById(R.id.btnNext2);
        btnNext2.setOnClickListener(this);
        mInterstitialAd = new InterstitialAd(this);
        mInterstitialAd.setAdUnitId(getResources().getString(R.string.window_ad_unit_id));
        AdRequest adRequestInter = new AdRequest.Builder().build();  // Replace this line only with-  AdRequest request = new AdRequest.Builder().addTestDevice("AC98C820A50B4AD8A2106EDE96FB87D4").build();

        mInterstitialAd.loadAd(adRequestInter);

        mInterstitialAd.setAdListener(new AdListener() {

            @Override
            public void onAdClosed() {
                super.onAdClosed();
                    if (!mInterstitialAd.isLoading() && !mInterstitialAd.isLoaded()) {
                    AdRequest adRequestInter = new AdRequest.Builder().build();
                    mInterstitialAd.loadAd(adRequestInter);
                    }
                    Intent intent = new Intent(Home_5_1.this, Home_5_2.class);
                    startActivity(intent);
            }
        });

    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btnNext2:
                if (mInterstitialAd != null && mInterstitialAd.isLoaded()) {
                    mInterstitialAd.show();
                } else {
                    Intent intent = new Intent(this, Home_5_2.class);
                    startActivity(intent);
                    break;
                }
                finish();

        }}} 

When you run this code your Actual Device ID will be shown in Verbose copy and replace with "AC98C820A50B4AD8A2106EDE96FB87D4" and then do further development.

theshivamlko
  • 271
  • 2
  • 10