Skip to main content

Featured post

Admob App Open Ads Integration in Android | How to integrate App Open Ads in Android Project?

  Hi, Gooogle Admob recently launched App Open Ads Ad Type with beta version. This ad will show when user open your app. it is like splash screen ad. but this is official ad. so chances of policy violation is less. For this ad, you can use interstitial ad unit id. Follow the below steps for integration app open ads into your android project. Step 1,   integrate admob library and android lifecycle library in app/build.gradle file.      implementation 'com.google.android.gms:play-services-ads:19.4.0'     def lifecycle_version = "2.0.0"     implementation "androidx.lifecycle:lifecycle-extensions:$lifecycle_version"     implementation "androidx.lifecycle:lifecycle-runtime:$lifecycle_version"     annotationProcessor "androidx.lifecycle:lifecycle-compiler:$lifecycle_version" Step 2,  after integrate admob library and andorid lifecycle library. create  MyApplication.java class in default package where your mainacitvity. Here is the source code

Admob App Open Ads Integration in Android | How to integrate App Open Ads in Android Project?

 


Hi, Gooogle Admob recently launched App Open Ads Ad Type with beta version. This ad will show when user open your app. it is like splash screen ad. but this is official ad. so chances of policy violation is less. For this ad, you can use interstitial ad unit id. Follow the below steps for integration app open ads into your android project.

Step 1,  integrate admob library and android lifecycle library in app/build.gradle file.

    implementation 'com.google.android.gms:play-services-ads:19.4.0'
    def lifecycle_version = "2.0.0"
    implementation "androidx.lifecycle:lifecycle-extensions:$lifecycle_version"
    implementation "androidx.lifecycle:lifecycle-runtime:$lifecycle_version"
    annotationProcessor "androidx.lifecycle:lifecycle-compiler:$lifecycle_version"


Step 2,  after integrate admob library and andorid lifecycle library. create  MyApplication.java class in default package where your mainacitvity. Here is the source code of MyApplication.java class code below.

MyApplication.java

import android.app.Application;

import com.google.android.gms.ads.MobileAds;
import com.google.android.gms.ads.initialization.InitializationStatus;
import com.google.android.gms.ads.initialization.OnInitializationCompleteListener;

/** The Application class that manages AppOpenManager. */
public class MyApplication extends Application {

    private static AppOpenManager appOpenManager;
    @Override
    public void onCreate() {
        super.onCreate();
        MobileAds.initialize(
                this,
                new OnInitializationCompleteListener() {
                    @Override
                    public void onInitializationComplete(InitializationStatus initializationStatus) {}
                });

        appOpenManager = new AppOpenManager(this);
    }
}




Step 3,  after create MyApplication class add this class into Application tag AndroidMenifest.xml file. and also add admob app id meta tag into androidmenifest.xml file.here is full code of andoridmenifest.xml file.

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

    <application
        android:name=".MyApplication"
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <meta-data
            android:name="com.google.android.gms.ads.APPLICATION_ID"
            android:value="ca-app-pub-3940256099942544~3347511713"/>

        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

</manifest>


Step 4, now create AppOpenManager.java class file into your local android project package name.Here is the source code of AppOpenManage.java class.

import android.app.Activity;
import android.app.Application;
import android.os.Bundle;
import android.util.Log;

import androidx.lifecycle.LifecycleObserver;
import androidx.lifecycle.OnLifecycleEvent;
import androidx.lifecycle.ProcessLifecycleOwner;

import com.google.android.gms.ads.AdError;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.FullScreenContentCallback;
import com.google.android.gms.ads.LoadAdError;
import com.google.android.gms.ads.appopen.AppOpenAd;

import java.util.Date;

import static androidx.lifecycle.Lifecycle.Event.ON_START;

/** Prefetches App Open Ads. */
public class AppOpenManager  implements LifecycleObserver,Application.ActivityLifecycleCallbacks{
    private static final String LOG_TAG = "AppOpenManager";
    private static final String AD_UNIT_ID = "ca-app-pub-3940256099942544/1033173712";
    private AppOpenAd appOpenAd = null;
    private static boolean isShowingAd = false;
    private long loadTime = 0;


    private AppOpenAd.AppOpenAdLoadCallback loadCallback;
    private Activity currentActivity;

    private final MyApplication myApplication;

    /** Constructor */
    public AppOpenManager(MyApplication myApplication) {
        this.myApplication = myApplication;
        this.myApplication.registerActivityLifecycleCallbacks(this);
        ProcessLifecycleOwner.get().getLifecycle().addObserver(this);
    }

    /** Creates and returns ad request. */
    private AdRequest getAdRequest() {
        return new AdRequest.Builder().build();
    }

    /** Utility method that checks if ad exists and can be shown. */
    public boolean isAdAvailable() {
        return appOpenAd != null && wasLoadTimeLessThanNHoursAgo(4);
    }

    /** Request an ad */
    public void fetchAd() {
        // Have unused ad, no need to fetch another.
        if (isAdAvailable()) {
            return;
        }

        loadCallback =
                new AppOpenAd.AppOpenAdLoadCallback() {
                    /**
                     * Called when an app open ad has loaded.
                     *
                     * @param ad the loaded app open ad.
                     */
                    @Override
                    public void onAppOpenAdLoaded(AppOpenAd ad) {
                        AppOpenManager.this.appOpenAd = ad;
                        AppOpenManager.this.loadTime = (new Date()).getTime();
                    }

                    /**
                     * Called when an app open ad has failed to load.
                     *
                     * @param loadAdError the error.
                     */
                    @Override
                    public void onAppOpenAdFailedToLoad(LoadAdError loadAdError) {
                        // Handle the error.
                    }

                };
        AdRequest request = getAdRequest();
        AppOpenAd.load(
                myApplication, AD_UNIT_ID, request,
                AppOpenAd.APP_OPEN_AD_ORIENTATION_PORTRAIT, loadCallback);
    }

    /** Utility method to check if ad was loaded more than n hours ago. */
    private boolean wasLoadTimeLessThanNHoursAgo(long numHours) {
        long dateDifference = (new Date()).getTime() - this.loadTime;
        long numMilliSecondsPerHour = 3600000;
        return (dateDifference < (numMilliSecondsPerHour * numHours));
    }


    /** ActivityLifecycleCallback methods */
    @Override
    public void onActivityCreated(Activity activity, Bundle savedInstanceState) {}

    @Override
    public void onActivityStarted(Activity activity) {
        currentActivity = activity;
    }

    @Override
    public void onActivityResumed(Activity activity) {
        currentActivity = activity;
    }

    @Override
    public void onActivityStopped(Activity activity) {}

    @Override
    public void onActivityPaused(Activity activity) {}

    @Override
    public void onActivitySaveInstanceState(Activity activity, Bundle bundle) {}

    @Override
    public void onActivityDestroyed(Activity activity) {
        currentActivity = null;
    }

    /** Shows the ad if one isn't already showing. */
    public void showAdIfAvailable() {
        // Only show ad if there is not already an app open ad currently showing
        // and an ad is available.
        if (!isShowingAd && isAdAvailable()) {
            Log.d(LOG_TAG, "Will show ad.");

            FullScreenContentCallback fullScreenContentCallback =
                    new FullScreenContentCallback() {
                        @Override
                        public void onAdDismissedFullScreenContent() {
                            // Set the reference to null so isAdAvailable() returns false.
                            AppOpenManager.this.appOpenAd = null;
                            isShowingAd = false;
                            fetchAd();
                        }

                        @Override
                        public void onAdFailedToShowFullScreenContent(AdError adError) {}

                        @Override
                        public void onAdShowedFullScreenContent() {
                            isShowingAd = true;
                        }
                    };

            appOpenAd.show(currentActivity, fullScreenContentCallback);

        } else {
            Log.d(LOG_TAG, "Can not show ad.");
            fetchAd();
        }
    }

    /** LifecycleObserver methods */
    @OnLifecycleEvent(ON_START)
    public void onStart() {
        showAdIfAvailable();
        Log.d(LOG_TAG, "onStart");
    }
}

Comments

Popular posts from this blog

Supported Apps as Dark Theme for Dark Mode for Apps & Phone UI | Night Mode

Dark Mode for Apps & Phone UI | Night Mode List of apps that support the Android Dark Mode. Instagram Google Playstore Google Pay Google Photos Google Chrome Google Fit GBoard Microsoft Launcher Pocket Casts KWGT Kustom Widget Maker Retro Music Player Transit Some Apps which support dark mode but you need to switch dark mode manully from their app settings. Here is the list of app.

How to use Tile Settings in Dark Mode for Apps?

First step for unlock quick settings tile is click on unlock shortcut. Now, You can see that Watch Video Ad (Button). But button is disable yet.Wait for some seconds. so video ad is loaded. when video ad is loaded button is enabled. after that click on it and watch complete video. When you watch complete video Quick settings tile is enabled. After Watching complete video ad. Swipe downlaod, You can see below screen.Click on expand menu which shown in below screenshot. You can see the red circle which shown in below screenhot. Just drag it on above side and then place it where ever you want.