码迷,mamicode.com
首页 > 其他好文 > 详细

监控Activity在前后台状态的切换

时间:2015-10-13 16:40:56      阅读:145      评论:0      收藏:0      [点我收藏+]

标签:


public class BaseActivity extends Activity{


@Override
protected void onStop() {
boolean isOnForeground=isAppOnForeground();
RaiingLog.d("前后台测试-->>onStop-->>" + isAppOnForeground()+", name-->>"+this.getClass().getName()+", isShowDialog-->>"+isShowDialog);
super.onStop();
}

@Override
protected void onResume() {
super.onResume();
boolean isOnForeground=isAppOnForeground();
RaiingLog.d("前后台测试-->>onResume-->>" + isAppOnForeground()+", name-->>"+this.getClass().getName()+", isShowDialog-->>"+isShowDialog);

}
/**
* 程序是否在前台运行
*
* @return
*/
public boolean isAppOnForeground() {
// Returns a list of application processes that are running on the
// device

ActivityManager activityManager = (ActivityManager) getApplicationContext().getSystemService(Context.ACTIVITY_SERVICE);
String packageName = getApplicationContext().getPackageName();

List<ActivityManager.RunningAppProcessInfo> appProcesses = activityManager
.getRunningAppProcesses();
if (appProcesses == null)
return false;

for (ActivityManager.RunningAppProcessInfo appProcess : appProcesses) {
// The name of the process that this object is associated with.
if (appProcess.processName.equals(packageName)
&& appProcess.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND) {
return true;
}
}

return false;
}
}

监控Activity在前后台状态的切换

标签:

原文地址:http://www.cnblogs.com/xiaorenwu702/p/4874876.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!