Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/187.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
android如何检查服务是否正在运行_Android_Service_Gps - Fatal编程技术网

android如何检查服务是否正在运行

android如何检查服务是否正在运行,android,service,gps,Android,Service,Gps,我有一个获取GPS坐标的应用程序。它工作正常,但我想在调用活动中测试服务是否正在运行。我有下面的代码,我认为会检查,但它返回假时,在通知栏中的GPS图标仍在闪烁。有人知道为什么吗?提前谢谢 private boolean isGpsServiceRunning() { ActivityManager manager = (ActivityManager)getSystemService(ACTIVITY_SERVICE); for (RunningServ

我有一个获取GPS坐标的应用程序。它工作正常,但我想在调用活动中测试服务是否正在运行。我有下面的代码,我认为会检查,但它返回假时,在通知栏中的GPS图标仍在闪烁。有人知道为什么吗?提前谢谢

private boolean isGpsServiceRunning() {
          ActivityManager manager = (ActivityManager)getSystemService(ACTIVITY_SERVICE);
          for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
            if ("LocationService".equals(service.service.getClassName())) {
              return true;
            }
          }
          return false;
        }


您应该与
LocationService.class.getName()
进行比较,而不是使用硬编码的值。此外,您正在使用的值对应于,这可能不是您在此处想要的值。

此方法对我来说很好用,并且易于使用:

private boolean isMyServiceRunning() {
    ActivityManager manager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);

    Log.i(TAG,"MyService.class.getName() = "+ButtonOnBootService.class.getName());              

    for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {

        Log.i(TAG,"service.getClassName() = " +service.service.getClassName());

        if (ButtonOnBootService.class.getName().equals(service.service.getClassName())) {
            return true;
        }
    }
    return false;   
}

它是否真的回归了?您是否尝试打印所有正在运行的服务,只是为了验证您没有查找错误的值。另外,我建议使用LocationService.class.getSimpleName()而不是硬编码。只是修复了它,我还需要输入包名。跟你说的一样。创建一个答案,我会接受。感谢(ActivityManager)获取系统服务(ACTIVITY_服务);您可能需要添加如下检查:service.uid==Process.myUid()。这将加快搜索速度并避免在另一个过程中找到服务,尽管我不太可能同时运行相同的免费/付费版本的应用程序!谢谢我会调查的。以任何方式加速android听起来都很有用:-)
private boolean isMyServiceRunning() {
    ActivityManager manager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);

    Log.i(TAG,"MyService.class.getName() = "+ButtonOnBootService.class.getName());              

    for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {

        Log.i(TAG,"service.getClassName() = " +service.service.getClassName());

        if (ButtonOnBootService.class.getName().equals(service.service.getClassName())) {
            return true;
        }
    }
    return false;   
}
 public boolean check(){
     ActivityManager manager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
    for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) 
            {
                if ("com.example.yourpackagename.YourserviceName"
                        .equals(service.service.getClassName())) 
                {
                    return true;
                }
            }
         return false;
    }