当前位置:操作系统 > 安卓/Android >>

Android 如何检测一个服务是否还在运行?

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;                
}

 

补充:移动开发 , Android ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,