您的位置:首页 > 移动开发 > Android开发

Android后台切回到应用显示广告页

2017-06-17 13:27 766 查看
看到市面上的很多app从后台切回到app会有广告页显示,因此也尝试了下,经过测试,以下的代码可以实现。

/**
* Created by myc on 2017/6/17.
* More Code on 1101255053@qq.com
* Description:
*/
public class BaseActivity extends Activity {
private static final String TAG = "BaseActivity";
//用来控制应用前后台切换的逻辑
private boolean isCurrentRunningForeground = true;
@Override
protected void onStart() {
super.onStart();
if (!isCurrentRunningForeground) {
isCurrentRunningForeground=true;
//处理跳转到广告页逻辑
Intent intennt=new Intent(this,LoadingActivity.class);
startActivity(intennt);
Log.e(TAG, ">>>>>>>>>>>>>>>>>>>切回前台 activity process");
}
}

@Override
protected void onStop() {
super.onStop();
isCurrentRunningForeground = isRunningForeground();
if (!isCurrentRunningForeground) {
Log.e(TAG,">>>>>>>>>>>>>>>>>>>切到后台 activity process");
}
}

public boolean isRunningForeground() {
ActivityManager activityManager = (ActivityManager) this.getSystemService(Context.ACTIVITY_SERVICE);
List<ActivityManager.RunningAppProcessInfo> appProcessInfos = activityManager.getRunningAppProcesses();
// 枚举进程,查看该应用是否在运行
for (ActivityManager.RunningAppProcessInfo appProcessInfo : appProcessInfos) {
if (appProcessInfo.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND) {
if (appProcessInfo.processName.equals(this.getApplicationInfo().processName)) {
Log.e(TAG,"EntryActivity isRunningForeGround");
return true;
}
}
}
Log.e(TAG, "EntryActivity isRunningBackGround");
return false;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  android
相关文章推荐