Android 如何检测一个服务是否还在运行?
前言
欢迎大家我分享和推荐好用的代码段~~
声明
欢迎转载,但请保留文章原始出处:
CSDN:http://www.csdn.net
雨季o莫忧离:http://blog.csdn.net/luckkof
正文
public static boolean isServiceStarted(Context context,String PackageName){ boolean isStarted =false; try{ int intGetTastCounter = 1000; ActivityManager mActivityManager = (ActivityManager)context.getSystemService(Context.ACTIVITY_SERVICE); List<ActivityManager.RunningServiceInfo> mRunningService = mActivityManager.getRunningServices(intGetTastCounter); for (ActivityManager.RunningServiceInfo amService : mRunningService){ if(0 == amService.service.getPackageName().compareTo(PackageName)){ isStarted = true; break; } } }catch(SecurityException e){ e.printStackTrace(); } return isStarted; }