我有两个申请。一个只包含Service
,我从其他应用中启动此Service
应用。像,
Intent intent = new Intent();
intent.setClassName(context.getString(R.string.package_location_service),
context.getString(R.string.package_location_service) + ".GetLocationService"); // package name and class name
serviceIntent.setFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
startService(serviceIntent);
但我想检查Service
是否已经在运行!如果它是同一应用程序内的服务,那么我可以通过
public static boolean isServiceRunning(String serviceClassName){
final ActivityManager activityManager = (ActivityManager)Application.getContext().getSystemService(Context.ACTIVITY_SERVICE);
final List<RunningServiceInfo> services = activityManager.getRunningServices(Integer.MAX_VALUE);
for (RunningServiceInfo runningServiceInfo : services) {
if (runningServiceInfo.service.getClassName().equals(serviceClassName)){
return true;
}
}
return false;
}
但是如何检查我的情景?有什么帮助吗?