Android 判断服务和进程是否存在方法

版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/pugongying1988/article/details/79203367

1、权限:

   <uses-permission android:name="android.permission.GET_TASKS" />
    <uses-permission android:name="android.permission.REAL_GET_TASKS" />

2、代码:

public static boolean isServicesExisted(Context context, String serName) {

ActivityManager ac = (ActivityManager) context
.getSystemService(Context.ACTIVITY_SERVICE);
List<ActivityManager.RunningServiceInfo> serviceList = ac.getRunningServices(Integer.MAX_VALUE);
for (int i = 0; i < serviceList.size(); i++) {
RunningServiceInfo serviceInfo = serviceList.get(i);
ComponentName serviceName = serviceInfo.service;
if (serviceName.getClassName().equals(serName)) {
return true;
}
}
return false;
}

public static boolean isRunningTaskExist(Context context,String processName){
ActivityManager am=(ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
List<RunningAppProcessInfo> processList = am.getRunningAppProcesses();
for(RunningAppProcessInfo info:processList){
if (info.processName.equals(processName)){
                  return true;
}
}
return false;
}
展开阅读全文

没有更多推荐了,返回首页