服务已经 运行 而它不应该在使用后台服务代码的位置

Service already running while it shouldnt in Location Using Background Service code

我有一个应用程序必须在许多活动中获取我所在的位置。我发现这段非常有用的完整代码 here。它是如何使用后台服务获取当前位置。我在我的代码中实现了它。第一次它工作正常。接下来,当它达到这个时 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();
  }

我收到消息说服务已经 运行。为什么会这样?我已经关闭并重新打开应用程序,为什么服务仍然 运行?请帮忙,这很重要,谢谢

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