码迷,mamicode.com
首页 > 移动开发 > 详细

Android检测服务是否运行

时间:2014-10-11 11:57:25      阅读:226      评论:0      收藏:0      [点我收藏+]

标签:android   style   blog   http   color   io   使用   ar   for   

直接上代码:

/**
     * 判断服务是否后台运行
     * 
     * @param context
     *            Context
     * @param className
     *            判断的服务名字
     * @return true 在运行 false 不在运行
     */
    public static boolean isServiceRun(Context mContext, String className) {
        boolean isRun = false;
        ActivityManager activityManager = (ActivityManager) mContext
                .getSystemService(Context.ACTIVITY_SERVICE);
        List<ActivityManager.RunningServiceInfo> serviceList = activityManager
                .getRunningServices(40);
        int size = serviceList.size();
        for (int i = 0; i < size; i++) {
            if (serviceList.get(i).service.getClassName().equals(className) == true) {
                isRun = true;
                break;
            }
        }
        return isRun;
    }

使用代码:

   boolean isRun = isServiceRun(getApplicationContext(), "com.baidu.location.f");

注:com.baidu.location.f为service的全类名。

 

http://blog.csdn.net/android_ls/article/details/8606014

Android检测服务是否运行

标签:android   style   blog   http   color   io   使用   ar   for   

原文地址:http://www.cnblogs.com/tianciliangen/p/4018354.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!