简体   繁体   English

服务已在运行,但不应使用后台服务代码定位

[英]Service already running while it shouldnt in Location Using Background Service code

I am having an app that has to get the location of where I am in many activities. 我正在使用一个应用程序,该应用程序必须获取我在许多活动中所处的位置。 I found this very helpful complete piece of code here . 我发现这个代码非常有帮助的完整片在这里 Its how to Get Current Location Using Background Service. 它是如何使用后台服务获取当前位置的。 I implemented it in my code. 我在代码中实现了它。 The first time it worked ok. 第一次工作正常。 The next, when it reached this if 下一个,当达到这个时

if (mPref.getString("service", "").matches("")) {
        medit.putString("service", "service").commit();
        Intent intent = new Intent(getApplicationContext(), GoogleService.class);
        startService(intent);

 } else {
        Toast.makeText(getApplicationContext(), "Service is already running", Toast.LENGTH_SHORT).show();
  }

I get the message that service is already running. 我收到消息,该服务已在运行。 Why this happens? 为什么会这样? I have closed and reopened the app, why the service still running? 我已关闭并重新打开该应用程序,为什么该服务仍在运行? Help pls, its very important, thank you 请帮助,它非常重要,谢谢

如果您使用SharedPreferences保留有关Service的信息,则该信息将在应用程序重启后继续存在,因此您的if条件始终为false (当您执行mPref.commit() ,会将密钥对保存到文件中)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM