Android detects whether a service is still alive

Here is the programming house jb51 CC collects and arranges code fragments through the network.

Programming house Xiaobian now shares it with you and gives you a reference.

    import java.util.List;  
      
    import android.app.ActivityManager;  
    import android.app.ActivityManager.RunningServiceInfo;  
    import android.content.Context;  
      
    public class ServiceUtils {  
      
        /** 
         * 校验某个服务是否还活着  
         * serviceName :传进来的服务的名称 
         */  
        public static boolean isServiceRunning(Context context,String serviceName){  
            ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);  
            List<RunningServiceInfo> infos = am.getRunningServices(100);  
            for(RunningServiceInfo info : infos){  
                String name = info.service.getClassName();  
                if(name.equals(serviceName)){  
                    return true;  
                }  
            }  
            return false;  
        }  
    }  
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>