Android编程判断当前应用是否在后台运行的方法示例
作者:迟做总比不做强 时间:2021-10-29 12:09:36
本文实例讲述了Android编程判断当前应用是否在后台运行的方法。分享给大家供大家参考,具体如下:
/** 判断程序是否在后台运行 */
public static boolean isRunBackground(Context context) {
ActivityManager activityManager = (ActivityManager) context
.getSystemService(Context.ACTIVITY_SERVICE);
List<RunningAppProcessInfo> appProcesses = activityManager
.getRunningAppProcesses();
for (RunningAppProcessInfo appProcess : appProcesses) {
if (appProcess.processName.equals(context.getPackageName())) {
if (appProcess.importance == RunningAppProcessInfo.IMPORTANCE_BACKGROUND) {
// 表明程序在后台运行
return true;
} else {
return false;
}
}
}
return false;
}
/** 判断程序是否在前台运行(当前运行的程序) */
public boolean isRunForeground() {
ActivityManager activityManager = (ActivityManager) getApplicationContext()
.getSystemService(Context.ACTIVITY_SERVICE);
String packageName = getApplicationContext().getPackageName();
List<RunningAppProcessInfo> appProcesses = activityManager
.getRunningAppProcesses();
if (appProcesses == null)
return false;
for (RunningAppProcessInfo appProcess : appProcesses) {
if (appProcess.processName.equals(packageName)
&& appProcess.importance == RunningAppProcessInfo.IMPORTANCE_FOREGROUND) {
return true;// 程序运行在前台
}
}
return false;
}
希望本文所述对大家Android程序设计有所帮助。
来源:http://blog.csdn.net/true100/article/details/45479827
标签:Android,应用
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
java实现倾斜水印铺满整张图
2023-12-24 22:34:21
![](https://img.aspxhome.com/file/2023/3/101773_0s.jpg)
Java封装的实现访问限定符、包
2023-03-20 07:08:48
![](https://img.aspxhome.com/file/2023/9/64069_0s.jpg)
Android性能优化以及数据优化方法
2021-09-02 03:46:18
![](https://img.aspxhome.com/file/2023/6/117166_0s.png)
SpringMVC中转发与重定向的区别浅析
2023-10-15 23:13:36
spring boot 日志配置详解
2023-09-24 00:23:11
![](https://img.aspxhome.com/file/2023/9/125229_0s.png)
Spring Boot配置接口WebMvcConfigurer的实现
2023-11-27 23:28:50
![](https://img.aspxhome.com/file/2023/3/60523_0s.png)
C#删除文件目录或文件的解决方法
2022-12-13 09:25:03
java面试题——详解HashMap和Hashtable 的区别
2023-08-06 16:38:25
使用Java实现Redis限流的方法
2023-09-27 01:43:47
![](https://img.aspxhome.com/file/2023/9/77999_0s.png)
Android数据持久化之File机制分析
2021-05-29 19:24:56
Java生产1-100的随机数简单实例(分享)
2021-08-15 02:52:28
Java Validation Api使用方法实例解析
2023-05-16 05:44:58
![](https://img.aspxhome.com/file/2023/7/119107_0s.png)
java中Vector类的常用方法详解
2023-11-26 10:29:07
![](https://img.aspxhome.com/file/2023/4/60234_0s.png)
C#四舍五入(函数)用法实例
2022-02-03 21:51:16
![](https://img.aspxhome.com/file/2023/4/100104_0s.png)
完美解决关于禁止ViewPager预加载的相关问题
2021-07-05 11:50:50
Spring Cloud gateway 网关如何拦截Post请求日志
2022-06-19 03:19:46
flutter Container容器实现圆角边框
2021-06-17 23:16:23
![](https://img.aspxhome.com/file/2023/1/110731_0s.gif)
Java面向对象编程的三大特征
2023-09-19 06:20:34
![](https://img.aspxhome.com/file/2023/8/58478_0s.png)
java 装饰模式(Decorator Pattern)详解
2023-08-10 09:56:21
Android Button 自带阴影效果另一种解决办法
2021-06-07 04:23:02