下面列出了怎么用android.app.ActivityManager.RunningServiceInfo的API类实例代码及写法,或者点击链接到github查看源代码。
/**
* 判断服务是否启动
* @param context
* @param serviceName
* @return
*/
public static boolean isRunning(Context context,String serviceName)
{
ActivityManager myAM=(ActivityManager)context.getSystemService(Context.ACTIVITY_SERVICE);
ArrayList<RunningServiceInfo> runningServices = (ArrayList<RunningServiceInfo>) myAM.getRunningServices(40);
//获取最多40个当前正在运行的服务,放进ArrList里,以现在手机的处理能力,要是超过40个服务,估计已经卡死,所以不用考虑超过40个该怎么办
for(int i = 0 ; i<runningServices.size();i++)//循环枚举对比
{
String className=runningServices.get(i).service.getClassName();
if(className.equals(serviceName))
{
return true;
}
}
return false;
}
/**
* 判断某个服务是否正在运行的方法
*
* @param mContext
* @param serviceName
* 是包名+服务的类名(例如:net.loonggg.testbackstage.TestService)
* @return true代表正在运行,false代表服务没有正在运行
*/
public boolean isServiceWork(Context mContext, String serviceName) {
boolean isWork = false;
ActivityManager myAM = (ActivityManager) mContext
.getSystemService(Context.ACTIVITY_SERVICE);
List<RunningServiceInfo> myList = myAM.getRunningServices(40);
if (myList.size() <= 0) {
return false;
}
for (int i = 0; i < myList.size(); i++) {
String mName = myList.get(i).service.getClassName().toString();
if (mName.equals(serviceName)) {
isWork = true;
break;
}
}
return isWork;
}
public static boolean checkServiceIsRunning(Context context, String serviceName) {
if (context == null || TextUtils.isEmpty(serviceName)) {
return false;
}
ActivityManager manager = (ActivityManager) context.getSystemService("activity");
if (manager == null) {
return false;
}
List<RunningServiceInfo> list = manager.getRunningServices(Integer.MAX_VALUE);
if (BaseTypeUtils.isListEmpty(list)) {
return false;
}
for (RunningServiceInfo service : list) {
if (service != null && service.service != null && serviceName.equals(service.service.getClassName())) {
return true;
}
}
return false;
}
private static boolean isServiceRunning(Context context) {
boolean isRunning = false;
ActivityManager activityManager = (ActivityManager) context
.getSystemService(Context.ACTIVITY_SERVICE);
List<RunningServiceInfo> serviceList = activityManager
.getRunningServices(Integer.MAX_VALUE);
if (serviceList == null || serviceList.size() == 0) {
return false;
}
for (int i = 0; i < serviceList.size(); i++) {
if (serviceList.get(i).service.getClassName().equals(
DownloadService.class.getName())) {
isRunning = true;
break;
}
}
return isRunning;
}
/**
* 停止本应用内的所有Service
* @param context context
*/
public static void stopAllServiceOfApp(final Context context) {
ActivityManager manager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
if (manager == null) {
return;
}
for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
if (context.getPackageName().equals(service.service.getPackageName())) {
Intent stopIntent = new Intent();
ComponentName serviceCMP = service.service;
stopIntent.setComponent(serviceCMP);
context.stopService(stopIntent);
// new AppRecManager(context).unregisterListener();
break;
}
}
// android.os.Process.killProcess(android.os.Process.myPid());
// System.exit(0);
}
/**
* Helper to query whether an application is currently running
* and its code has not been evicted from memory.
*
* @param context Application's context
* @param appName The package name or process name of the application.
* @return true if the application is running, false otherwise.
*/
public static boolean isRunning(final Context context, String appName) {
List<RunningAppProcessInfo> runningProcessInfo = Process.getRunningProcessInfo(context);
List<RunningServiceInfo> services = Service.getRunningServiceInfo(context);
for (RunningAppProcessInfo info : runningProcessInfo) {
if (info.processName.equals(appName) &&
info.importance != RunningAppProcessInfo.IMPORTANCE_EMPTY) {
return true;
}
}
for (RunningServiceInfo service : services) {
String name = StringHelper.formatProcessName(service.process);
if (name.equals(appName)) return true;
}
return false;
}
/**
* Checks if the service with the given name is currently running on the device.
*
* @param serviceName Fully qualified name of the server. <br/>
* For e.g. nl.changer.myservice.name
**/
public static boolean isServiceRunning(Context ctx, String serviceName) {
if (serviceName == null) {
throw new NullPointerException("Service name cannot be null");
}
// use application level context to avoid unnecessary leaks.
ActivityManager manager = (ActivityManager) ctx.getApplicationContext().getSystemService(Context.ACTIVITY_SERVICE);
for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
if (service.service.getClassName().equals(serviceName)) {
return true;
}
}
return false;
}
private boolean isTorchServiceRunning(Context context) {
ActivityManager mActivityManager = (ActivityManager) context.getSystemService(Activity.ACTIVITY_SERVICE);
List<ActivityManager.RunningServiceInfo> mList = mActivityManager.getRunningServices(100);
if (!(mList.size() > 0)) {
return false;
}
for (RunningServiceInfo mServiceInfo : mList) {
ComponentName mServiceName = mServiceInfo.service;
if (mServiceName.getClassName().endsWith(".TorchService")
|| mServiceName.getClassName().endsWith(".RootTorchService"))
return true;
}
return false;
}
public boolean checkRootMethod10() {
Log.d(Main.TAG, "check11");
boolean returnValue = false;
// Get currently running application processes
List<RunningServiceInfo> list = manager.getRunningServices(300);
if(list != null){
String tempName;
for(int i=0;i<list.size();++i){
tempName = list.get(i).process;
Log.d(Main.TAG, "process: " + tempName);
if(tempName.contains("supersu") || tempName.contains("superuser")){
Log.d(Main.TAG, "found one!");
returnValue = true;
}
}
}
return returnValue;
}
/**
* This method will find which router service is running. Use that info to find out more about that app and service.
* It will store the found service for later use and return the package name if found.
* @param pm An instance of a package manager. This is no longer used so null can be sent.
* @return
*/
public ComponentName componentNameForServiceRunning(PackageManager pm){
if(context==null){
return null;
}
ActivityManager manager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
//PackageManager pm = context.getPackageManager();
for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
//Log.d(TAG, service.service.getClassName());
//We will check to see if it contains this name, should be pretty specific
if ((service.service.getClassName()).toLowerCase(Locale.US).contains(SdlBroadcastReceiver.SDL_ROUTER_SERVICE_CLASS_NAME)){
//this.service = service.service; //This is great
if(service.started && service.restarting==0){ //If this service has been started and is not crashed
return service.service; //appPackageForComponenetName(service.service,pm);
}
}
}
return null;
}
/**
* We want to check to see if the Router service is already up and running
*
* @param context
* @return
*/
private boolean isRouterServiceRunning(Context context) {
if (context == null) {
return false;
}
ActivityManager manager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
//We will check to see if it contains this name, should be pretty specific
if ((service.service.getClassName()).toLowerCase(Locale.US).contains(SdlBroadcastReceiver.SDL_ROUTER_SERVICE_CLASS_NAME)) {
this.routerClassName = service.service.getClassName();
this.routerPackage = service.service.getPackageName();
return true;
}
}
return false;
}
private boolean isServiceRunning()
{
boolean result = false;
try {
// Return Plugin with ServiceRunning true/ false
ActivityManager manager = (ActivityManager)this.mContext.getSystemService(Context.ACTIVITY_SERVICE);
for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
if (this.mServiceName.equals(service.service.getClassName())) {
result = true;
}
}
} catch (Exception ex) {
Log.d(LOCALTAG, "isServiceRunning failed", ex);
}
return result;
}
/**
* Checks whether the recording service is currently running.
*
* @param ctx
* the current context
* @return true if the service is running, false otherwise
*/
public static boolean isServiceRunning(final Context ctx, final Class<?> cls) {
final ActivityManager activityManager = (ActivityManager) ctx
.getSystemService(Context.ACTIVITY_SERVICE);
final List<RunningServiceInfo> services = activityManager
.getRunningServices(Integer.MAX_VALUE);
for (final RunningServiceInfo serviceInfo : services) {
final ComponentName componentName = serviceInfo.service;
final String serviceName = componentName.getClassName();
if (serviceName.equals(cls.getName())) {
return true;
}
}
return false;
}
public static boolean isServiceRunning(Context context, String className) {
boolean isRunning = false;
for (RunningServiceInfo si : ((ActivityManager) context.getSystemService("activity")).getRunningServices(Integer.MAX_VALUE)) {
if (className.equals(si.service.getClassName())) {
isRunning = true;
}
}
return isRunning;
}
/**
* 判断服务是否运行
* @param className package.ServiceClassName - class.getName()
* @return {@code true} yes, {@code false} no
*/
public static boolean isServiceRunning(final String className) {
try {
ActivityManager activityManager = AppUtils.getActivityManager();
List<RunningServiceInfo> lists = activityManager.getRunningServices(Integer.MAX_VALUE);
for (RunningServiceInfo info : lists) {
if (className.equals(info.service.getClassName())) return true;
}
} catch (Exception e) {
LogPrintUtils.eTag(TAG, e, "isServiceRunning");
}
return false;
}
/**
* 获取所有运行的服务
* @return 服务名集合
*/
public static Set getAllRunningService() {
try {
Set<String> names = new HashSet<>();
ActivityManager activityManager = AppUtils.getActivityManager();
List<RunningServiceInfo> lists = activityManager.getRunningServices(Integer.MAX_VALUE);
for (RunningServiceInfo info : lists) {
names.add(info.service.getClassName());
}
return names;
} catch (Exception e) {
LogPrintUtils.eTag(TAG, e, "getAllRunningService");
}
return Collections.emptySet();
}
/**
* 获取所有运行的服务
*
* @return 服务名集合
*/
public static Set getAllRunningService() {
ActivityManager activityManager = (ActivityManager) Utils.getContext().getSystemService(Context.ACTIVITY_SERVICE);
List<RunningServiceInfo> info = activityManager.getRunningServices(0x7FFFFFFF);
Set<String> names = new HashSet<>();
if (info == null || info.size() == 0) return null;
for (RunningServiceInfo aInfo : info) {
names.add(aInfo.service.getClassName());
}
return names;
}
/**
* 判断服务是否运行
*
* @param className 完整包名的服务类名
* @return {@code true}: 是<br>{@code false}: 否
*/
public static boolean isServiceRunning(String className) {
ActivityManager activityManager = (ActivityManager) Utils.getContext().getSystemService(Context.ACTIVITY_SERVICE);
List<RunningServiceInfo> info = activityManager.getRunningServices(0x7FFFFFFF);
if (info == null || info.size() == 0) return false;
for (RunningServiceInfo aInfo : info) {
if (className.equals(aInfo.service.getClassName())) return true;
}
return false;
}
public static boolean checkServiceIsRunning(Context context) {
for (RunningServiceInfo service : ((ActivityManager) context.getSystemService("activity")).getRunningServices(Integer.MAX_VALUE)) {
if ("com.letv.android.client.worldcup.LetvDownloadService".equals(service.service.getClassName())) {
Constants.debug("com.letv.android.client.worldcup.LetvDownloadService is Running");
return true;
}
}
return false;
}
public static boolean isServiceRunning(Context mContext, String className) {
boolean isRunning = false;
List<RunningServiceInfo> serviceList = ((ActivityManager) mContext.getSystemService("activity")).getRunningServices(Integer.MAX_VALUE);
if (serviceList.size() <= 0) {
return false;
}
for (int i = 0; i < serviceList.size(); i++) {
if (((RunningServiceInfo) serviceList.get(i)).service.getClassName().equals(className)) {
LogInfo.log("XX", "isServiceRunning name " + ((RunningServiceInfo) serviceList.get(i)).service.getClassName());
isRunning = true;
break;
}
}
return isRunning;
}
public static ComponentName isServiceExisted(Context context, String packageName) {
for (RunningServiceInfo runningServiceInfo : ((ActivityManager) context.getSystemService("activity")).getRunningServices(Integer.MAX_VALUE)) {
ComponentName serviceName = runningServiceInfo.service;
if (serviceName.getPackageName().equals(packageName) && serviceName.getClassName().equals(new StringBuilder(String.valueOf(packageName)).append(".business.RemoteSSOService").toString())) {
return serviceName;
}
}
return null;
}
/**
* 检测服务是否运行
*
* @param context 上下文
* @param className 类名
* @return 是否运行的状态
*/
public static boolean isServiceRunning(Context context, String className) {
boolean isRunning = false;
ActivityManager activityManager = (ActivityManager) context
.getSystemService(Context.ACTIVITY_SERVICE);
List<RunningServiceInfo> servicesList = activityManager
.getRunningServices(Integer.MAX_VALUE);
for (RunningServiceInfo si : servicesList) {
if (className.equals(si.service.getClassName())) {
isRunning = true;
}
}
return isRunning;
}
public static boolean isMyServiceRunning(Context context, Class MyServiceClass) {
ActivityManager manager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
if (MyServiceClass.getName().equals(service.service.getClassName())) {
return true;
}
}
return false;
}
/**
* Return all of the services are running.
*
* @return all of the services are running
*/
public static Set getAllRunningServices() {
ActivityManager am = (ActivityManager) Utils.getApp().getSystemService(Context.ACTIVITY_SERVICE);
List<RunningServiceInfo> info = am.getRunningServices(0x7FFFFFFF);
Set<String> names = new HashSet<>();
if (info == null || info.size() == 0) return null;
for (RunningServiceInfo aInfo : info) {
names.add(aInfo.service.getClassName());
}
return names;
}
/**
* Return whether service is running.
*
* @param className The name of class.
* @return {@code true}: yes<br>{@code false}: no
*/
public static boolean isServiceRunning(@NonNull final String className) {
ActivityManager am = (ActivityManager) Utils.getApp().getSystemService(Context.ACTIVITY_SERVICE);
List<RunningServiceInfo> info = am.getRunningServices(0x7FFFFFFF);
if (info == null || info.size() == 0) return false;
for (RunningServiceInfo aInfo : info) {
if (className.equals(aInfo.service.getClassName())) return true;
}
return false;
}
public static boolean isMyServiceRunning(Context context) {
ActivityManager manager = (ActivityManager) context
.getSystemService(Context.ACTIVITY_SERVICE);
for (RunningServiceInfo service : manager
.getRunningServices(Integer.MAX_VALUE)) {
if (LiveAppService.class.getName().equals(
service.service.getClassName())) {
return true;
}
}
return false;
}
private boolean isMyServiceRunning() {
ActivityManager manager = (ActivityManager) getApplicationContext().getSystemService(Context.ACTIVITY_SERVICE);
for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
if (MyService.class.getName().equals(service.service.getClassName())) {
return true;
}
}
return false;
}
private boolean isMyServiceRunning() {
ActivityManager manager = (ActivityManager) mContext.getSystemService(Context.ACTIVITY_SERVICE);
for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
if (MyService.class.getName().equals(service.service.getClassName())) {
return true;
}
}
return false;
}
public static boolean isServiceRunning(Context context, Class serviceClass) {
ActivityManager manager =
(ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
if (serviceClass.getName().equals(service.service.getClassName())) {
return true;
}
}
return false;
}
public static boolean isServiceRunning(Context context, Class serviceClass) {
ActivityManager manager =
(ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
if (serviceClass.getName().equals(service.service.getClassName())) {
return true;
}
}
return false;
}