1. 概述
判断App在前台还是后台有2种方式。
第一种:自己定义一个MyApplication继承Application,在onCreate()方法中:
/**
* Email: 2185134304@qq.com
* Created by Novate 2018/4/25 17:34
* Version 1.0
* Params:
* Description:
*/
public class MyApplication extends Application{
public int count = 0;
@Override
public void onCreate() {
super.onCreate();
registerActivityLifecycleCallbacks(new ActivityLifecycleCallbacks() {
@Override
public void onActivityStopped(Activity activity) {
Log.v("viclee", activity + "onActivityStopped");
count--;
if (count == 0) {
Log.v("viclee", ">>>>>>>>>>>>>>>>>>>切到后台 lifecycle");
}
}
@Override
public void onActivityStarted(Activity activity) {
Log.v("viclee", activity + "onActivityStarted");
if (count == 0) {
Log.v("viclee", ">>>>>>>>>>>>>>>>>>>切到前台 lifecycle");
}
count++;
}
@Override
public void onActivitySaveInstanceState(Activity activity, Bundle outState) {
Log.v("viclee", activity + "onActivitySaveInstanceState");
}
@Override
public void onActivityResumed(Activity activity) {
Log.v("viclee", activity + "onActivityResumed");
}
@Override
public void onActivityPaused(Activity activity) {
Log.v("viclee", activity + "onActivityPaused");
}
@Override
public void onActivityDestroyed(Activity activity) {
Log.v("viclee", activity + "onActivityDestroyed");
}
@Override
public void onActivityCreated(Activity activity, Bundle savedInstanceState) {
Log.v("viclee", activity + "onActivityCreated");
}
});
}
}
第二种:直接在具体的某个 Activity中写一个判断是否在前台还是后台的方法,然后在 该Activity的 onStart()、onStop()中判断:
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
public static final String TAG = "viclee";
private boolean isCurrentRunningForeground = true;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.btn).setOnClickListener(this);
}
@Override
protected void onStart() {
super.onStart();
if (!isCurrentRunningForeground) {
Log.e(TAG, ">>>>>>>>>>>>>>>>>>>切到前台 activity process");
Toast.makeText(MainActivity.this , ">>>>>>>>>>>>>>>>>>>切到前台 activity process" , Toast.LENGTH_SHORT).show();
}
}
@Override
protected void onStop() {
super.onStop();
isCurrentRunningForeground = isRunningForeground();
if (!isCurrentRunningForeground) {
Log.e(TAG, ">>>>>>>>>>>>>>>>>>>切到后台 activity process");
Toast.makeText(MainActivity.this , ">>>>>>>>>>>>>>>>>>>切到后台 activity process" , Toast.LENGTH_SHORT).show();
}
}
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, "MainActivity isRunningForeGround"); // 前台进程
return true;
}
}
}
Log.e(TAG, "MainActivity isRunningBackGround"); // 后台进程
return false;
}
@Override
public void onClick(View v) {
if (v.getId() == R.id.btn) {
startActivity(new Intent(this,Main2Activity.class));
}
}
}
直接使用以上方式进行判断就ok。