为了实现手势密码,如果切换到后台再次打开的话,需要弹出,以下的方法在BaseActivity里总体判断
@Override
protected void onStart() {
isCurrentRunningForeground = isRunningForeground();
if (isCurrentRunningForeground) {
Log.e("hq", "onStart>>>>>>>>>>>>>>>>>>>切到前台 activity process");
Toast.makeText(MainActivity.this,"onStart fore",Toast.LENGTH_SHORT).show();
}
super.onStart();
}
@Override
protected void onStop() {
isCurrentRunningForeground = isRunningForeground();
if (!isCurrentRunningForeground) {
Log.e("hq",">>>>>>>>>>>>>>>>>>>切到后台 activity process");
}
super.onStop();
}
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.d("hq","EntryActivity isRunningForeGround");
return true;
}
}
}
Log.d("hq", "EntryActivity isRunningBackGround");
return false;
}