245 lines
7.4 KiB
Plaintext
245 lines
7.4 KiB
Plaintext
|
package com.novelbook.android.AD;
|
|||
|
|
|||
|
import android.app.Activity;
|
|||
|
import android.app.Application;
|
|||
|
import android.content.ComponentCallbacks2;
|
|||
|
import android.content.Intent;
|
|||
|
import android.content.res.Configuration;
|
|||
|
import android.os.Build;
|
|||
|
import android.os.Bundle;
|
|||
|
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;
|
|||
|
import com.google.android.gms.ads.MobileAds;
|
|||
|
import com.novelbook.android.Main2Activity;
|
|||
|
import com.novelbook.android.MyApp;
|
|||
|
import com.novelbook.android.bean.AdSetting;
|
|||
|
import com.novelbook.android.utils.Constants;
|
|||
|
|
|||
|
import java.util.Date;
|
|||
|
|
|||
|
import static com.novelbook.android.utils.Constants.AD_TENCENT_QQ;
|
|||
|
import static com.novelbook.android.utils.Constants.AD_TOUTIAO;
|
|||
|
|
|||
|
public class SplashAdManager implements Application.ActivityLifecycleCallbacks, ComponentCallbacks2 {
|
|||
|
final static String TAG= SplashAdManager.class.getSimpleName();
|
|||
|
private boolean mIsBackground;
|
|||
|
private static long lastAd =0;
|
|||
|
//private static long interVal = 5000;//Constants.AD_SPLASH_INVTERVAL;
|
|||
|
private static int pages =0;
|
|||
|
|
|||
|
private static InterstitialAd mInterstitialAd ;
|
|||
|
/******
|
|||
|
* 使用方式:在Application中直接构造即可
|
|||
|
* @param application
|
|||
|
*/
|
|||
|
public SplashAdManager(Application application) {
|
|||
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
|
|||
|
application.registerActivityLifecycleCallbacks(this);
|
|||
|
application.registerComponentCallbacks(this);
|
|||
|
}
|
|||
|
|
|||
|
}
|
|||
|
|
|||
|
/*****
|
|||
|
* 解除绑定
|
|||
|
*/
|
|||
|
public void release(Application application) {
|
|||
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
|
|||
|
application.unregisterActivityLifecycleCallbacks(this);
|
|||
|
application.unregisterComponentCallbacks(this);
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
@Override
|
|||
|
public void onActivityCreated(Activity activity, Bundle savedInstanceState) {
|
|||
|
if(activity instanceof SplashActivity){
|
|||
|
return;
|
|||
|
}
|
|||
|
if(Constants.AD_SETTING.getSplash2() .getSource() == Constants.AD_MOB) {
|
|||
|
getAdMob(activity);
|
|||
|
}
|
|||
|
Log.d(TAG, "onActivityCreated:mInterstitialAd initialized ");
|
|||
|
/* if(activity instanceof Main2Activity){
|
|||
|
|
|||
|
//showAd(activity);
|
|||
|
Log.d("zzr", " 启动应用");
|
|||
|
}*/
|
|||
|
}
|
|||
|
|
|||
|
@Override
|
|||
|
public void onActivityStarted(Activity activity) {
|
|||
|
|
|||
|
}
|
|||
|
|
|||
|
@Override
|
|||
|
public void onActivityResumed(Activity activity) {
|
|||
|
|
|||
|
if(activity instanceof SplashActivity){
|
|||
|
return;
|
|||
|
}
|
|||
|
if (mIsBackground) {
|
|||
|
mIsBackground = false;
|
|||
|
|
|||
|
showSplash2Ad(activity);
|
|||
|
|
|||
|
Log.d(TAG, "从广告页回到前台 "+activity);
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
@Override
|
|||
|
public void onActivityPaused(Activity activity) {
|
|||
|
|
|||
|
}
|
|||
|
|
|||
|
@Override
|
|||
|
public void onActivityStopped(Activity activity) {
|
|||
|
|
|||
|
}
|
|||
|
|
|||
|
@Override
|
|||
|
public void onActivitySaveInstanceState(Activity activity, Bundle outState) {
|
|||
|
|
|||
|
}
|
|||
|
|
|||
|
@Override
|
|||
|
public void onActivityDestroyed(Activity activity) {
|
|||
|
|
|||
|
}
|
|||
|
|
|||
|
@Override
|
|||
|
public void onTrimMemory(int level) {
|
|||
|
//监听应用是否退到后台
|
|||
|
if (level == TRIM_MEMORY_UI_HIDDEN) {
|
|||
|
mIsBackground = true;
|
|||
|
Log.i("zzr", "应用退出到后台");
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
@Override
|
|||
|
public void onConfigurationChanged(Configuration newConfig) {
|
|||
|
|
|||
|
}
|
|||
|
|
|||
|
@Override
|
|||
|
public void onLowMemory() {
|
|||
|
|
|||
|
}
|
|||
|
static void showInterAd(Activity activity){
|
|||
|
clearPages();
|
|||
|
}
|
|||
|
static void showSplash2Ad(Activity activity){
|
|||
|
if(canShowAd()) {
|
|||
|
lastAd = new Date().getTime();
|
|||
|
|
|||
|
if(Constants.AD_SETTING.getSplash2() .getSource() == Constants.AD_MOB){
|
|||
|
|
|||
|
Log.d(TAG, "showSplashAd:mInterstitialAd.isLoaded() " +mInterstitialAd.isLoaded());
|
|||
|
if(mInterstitialAd!=null && mInterstitialAd.isLoaded()) {
|
|||
|
SplashAdManager.setSplashTime();
|
|||
|
activity.getWindow().getDecorView().setSystemUiVisibility(
|
|||
|
View.SYSTEM_UI_FLAG_LAYOUT_STABLE
|
|||
|
| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
|
|||
|
| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
|
|||
|
| View.SYSTEM_UI_FLAG_HIDE_NAVIGATION // hide nav bar
|
|||
|
| View.SYSTEM_UI_FLAG_FULLSCREEN // hide status bar
|
|||
|
| View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
|
|||
|
// | View.SYSTEM_UI_FLAG_IMMERSIVE
|
|||
|
);
|
|||
|
mInterstitialAd.show();
|
|||
|
mInterstitialAd.loadAd(new AdRequest.Builder().build());
|
|||
|
}
|
|||
|
return;
|
|||
|
}
|
|||
|
|
|||
|
Intent intent = new Intent(activity, SplashActivity.class);
|
|||
|
intent.putExtra(SplashActivity.EXTR_LUNCHER, false);
|
|||
|
activity.startActivity(intent);
|
|||
|
}
|
|||
|
}
|
|||
|
static boolean canShowAd(){
|
|||
|
if(!Constants.AD_SETTING.isShowAdsense()){
|
|||
|
return false;
|
|||
|
}
|
|||
|
long in = new Date().getTime() -lastAd;
|
|||
|
int interVal = Constants.AD_SETTING.getSplash2().getInterval4Display()*1000;
|
|||
|
Log.d(TAG, "canShowAd: interval is, " +in + ", canshow? "+ (in >interVal));
|
|||
|
return in >interVal;
|
|||
|
}
|
|||
|
public static void setSplashTime(){ lastAd = new Date().getTime(); }
|
|||
|
public static void clearPages(){
|
|||
|
pages =0;
|
|||
|
}
|
|||
|
public static void plusPage(Activity activity){
|
|||
|
pages++;
|
|||
|
int pcnt =Constants.AD_SETTING.getSplash2().getPageCount4Display();;
|
|||
|
|
|||
|
if(pages > pcnt){
|
|||
|
Log.d(TAG, String.format("canShowAd: plusPage pages %s, maxpage %s ", pages,pcnt));
|
|||
|
clearPages();
|
|||
|
|
|||
|
showSplash2Ad(activity);
|
|||
|
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
void getAdMob(Activity activity){
|
|||
|
mInterstitialAd = new InterstitialAd(activity);
|
|||
|
Log.d(TAG, "getAdMob: mIntersititiaAd is null ?" + (mInterstitialAd==null));
|
|||
|
if(mInterstitialAd!=null)
|
|||
|
mInterstitialAd.setAdListener(new AdListener() {
|
|||
|
|
|||
|
@Override
|
|||
|
public void onAdLoaded() {
|
|||
|
// Code to be executed when an ad finishes loading.
|
|||
|
Log.d(TAG, "onAdLoaded: ");
|
|||
|
|
|||
|
}
|
|||
|
|
|||
|
@Override
|
|||
|
public void onAdFailedToLoad(int errorCode) {
|
|||
|
// Code to be executed when an ad request fails.
|
|||
|
Log.d(TAG, "onAdFailedToLoad: ");
|
|||
|
|
|||
|
}
|
|||
|
|
|||
|
@Override
|
|||
|
public void onAdOpened() {
|
|||
|
// Code to be executed when the ad is displayed.
|
|||
|
Log.d(TAG, "onAdOpened: ");
|
|||
|
}
|
|||
|
|
|||
|
@Override
|
|||
|
public void onAdClicked() {
|
|||
|
// Code to be executed when the user clicks on an ad.
|
|||
|
Log.d(TAG, "onAdClicked: ");
|
|||
|
}
|
|||
|
|
|||
|
@Override
|
|||
|
public void onAdLeftApplication() {
|
|||
|
// Code to be executed when the user has left the app.
|
|||
|
Log.d(TAG, "onAdLeftApplication: ");
|
|||
|
}
|
|||
|
|
|||
|
@Override
|
|||
|
public void onAdClosed() {
|
|||
|
// Code to be executed when the interstitial ad is closed.
|
|||
|
Log.d(TAG, "onAdClosed: ");
|
|||
|
|
|||
|
}
|
|||
|
});
|
|||
|
|
|||
|
|
|||
|
mInterstitialAd.setAdUnitId( Constants.AD_SETTING.getSplash2().getSlotId(Constants.AD_MOB));
|
|||
|
mInterstitialAd.loadAd(new AdRequest.Builder().build());
|
|||
|
|
|||
|
}
|
|||
|
|
|||
|
}
|