直接上代码:
/**
* 判断服务是否后台运行
*
* @param context
* Context
* @param className
* 判断的服务名字
* @return true 在运行 false 不在运行
*/
public static boolean isServiceRun(Context mContext, String className) {
boolean isRun = false;
ActivityManager activityManager = (ActivityManager) mContext
.getSystemService(Context.ACTIVITY_SERVICE);
List<ActivityManager.RunningServiceInfo> serviceList = activityManager
.getRunningServices(40);
int size = serviceList.size();
for (int i = 0; i < size; i++) {
if (serviceList.get(i).service.getClassName().equals(className) == true) {
isRun = true;
break;
}
}
return isRun;
}
使用代码:
boolean isRun = isServiceRun(getApplicationContext(), "com.baidu.location.f");
注:com.baidu.location.f为service的全类名。
判断Android服务后台运行状态
本文提供了一个Java方法,用于检查Android应用中的特定服务是否正在后台运行。通过获取当前活动管理器并查找运行服务列表来实现此功能。
2万+

被折叠的 条评论
为什么被折叠?



