Friday, September 22, 2017

interstitialAd Show in android || One activity to another activity Lunching

step 1: 

 Add first in your App.gradle file

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'    })
    compile 'com.android.support:appcompat-v7:25.3.1'    compile 'com.google.android.gms:play-services-ads:9.2.0'    testCompile 'junit:junit:4.12'}

step 2: 

MainActivity.java

package com.edupointbd.amirul.notificationdemo;

import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.support.v4.app.TaskStackBuilder;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.app.NotificationCompat;
import android.util.Log;
import android.view.View;

import com.google.android.gms.ads.AdListener;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.InterstitialAd;

public class MainActivity extends AppCompatActivity {


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


    }

    public void notificationBtn(View view){

        NotificationCompat.Builder builder = (NotificationCompat.Builder) new NotificationCompat.Builder(this)
                .setSmallIcon(R.drawable.amirul)
                .setContentTitle("ICT HUB")
                .setContentText("how are you");

        Intent resultIntent = new Intent(this, ResultActivity.class);
        TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
        stackBuilder.addParentStack(ResultActivity.class);
        stackBuilder.addNextIntent(resultIntent);

        PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(
                0, PendingIntent.FLAG_UPDATE_CURRENT );
        builder.setContentIntent(resultPendingIntent);
        NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

        mNotificationManager.notify(101, builder.build());

    }

    public void addMod(View view){
        AdManager adManager = new AdManager(this,"ca-app-pub-3940256099942544/1033173712");
        adManager.createAd();
        Intent intent = new Intent(getApplicationContext(),SecondActivity.class);
        startActivity(intent);

    }
}


Activitymain.xml


<?xml version="1.0" encoding="utf-8"?><RelativeLayout 
xmlns:android="http://schemas.android.com/apk/res/android"   
 xmlns:tools="http://schemas.android.com/tools"    
android:id="@+id/activity_main"   
 android:layout_width="match_parent"  
  android:layout_height="match_parent"    
android:paddingBottom="@dimen/activity_vertical_margin"    
android:paddingLeft="@dimen/activity_horizontal_margin"    
android:paddingRight="@dimen/activity_horizontal_margin"   
 android:paddingTop="@dimen/activity_vertical_margin"    
tools:context="com.edupointbd.amirul.notificationdemo.MainActivity">

 <Button     android:id="@+id/btn1"    
 android:text="do it"    
 android:onClick="notificationBtn"    
 android:layout_width="match_parent"    
 android:layout_height="wrap_content" />
 <Button     android:text="New activity"   
  android:layout_below="@+id/btn1"    
 android:id="@+id/btnadd"     android:onClick="addMod"  
   android:layout_width="match_parent"    
 android:layout_height="wrap_content" />
</RelativeLayout>

step 3:

AdManage.java

package com.edupointbd.amirul.notificationdemo;

import android.app.Activity;

import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.InterstitialAd;

/** * Created by Amirul on 22-Sep-17. */
public class AdManager {
    private static InterstitialAd interstitialAd;

    private static boolean isInterAdsShowed = false;
    private Activity activity;
    private String AD_UNIT_ID;

    AdManager(Activity activity, String AD_UNIT_ID) {

        this.activity = activity;
        this.AD_UNIT_ID = AD_UNIT_ID;
        createAd();
    }

  void createAd() {
        // Create an ad.        interstitialAd = new InterstitialAd(activity);
        interstitialAd.setAdUnitId(AD_UNIT_ID);

        AdRequest adRequest = new AdRequest.Builder()
                //.addTestDevice(AdRequest.DEVICE_ID_EMULATOR)             
   //.addTestDevice(TEST_DEVICE_ID)                .build();

        // Load the interstitial ad.        interstitialAd.loadAd(adRequest);
    }

    static InterstitialAd getAd() {
        if (interstitialAd != null && interstitialAd.isLoaded() && !isInterAdsShowed) {
            isInterAdsShowed = true;
            return interstitialAd;
        } else return null;
    }
}


step 4:

SecondActivity.java

package com.edupointbd.amirul.notificationdemo;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;

import com.google.android.gms.ads.InterstitialAd;

public class SecondActivity extends AppCompatActivity {

    @Override    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);
        InterstitialAd ad = AdManager.getAd();
        if (ad != null) {
            ad.show();

        }else {
            Log.d("Add","empty");
        }
    }
}


activity_secound.xml

<?xml version="1.0" encoding="utf-8"?><RelativeLayout
 xmlns:android="http://schemas.android.com/apk/res/android"   
 xmlns:tools="http://schemas.android.com/tools"   
 android:id="@+id/activity_second"    android:layout_width="match_parent"  
 android:layout_height="match_parent"   
 android:paddingBottom="@dimen/activity_vertical_margin"    
android:paddingLeft="@dimen/activity_horizontal_margin"   
 android:paddingRight="@dimen/activity_horizontal_margin" 
  android:paddingTop="@dimen/activity_vertical_margin"   
 tools:context="com.edupointbd.amirul.notificationdemo.SecondActivity">
    <Button        android:id="@+id/it"     

   android:text="welcome to you"     
   android:layout_width="match_parent"    
    android:layout_height="wrap_content" />
</RelativeLayout>