【问题标题】:How can I stop my old service?如何停止我的旧服务?
【发布时间】:2014-07-04 10:58:01
【问题描述】:

我想在退出应用时启动服务。

代码是:

Intent i_service   = new Intent(MainActivity.this,check.class);
startService(i_service); 

当我重新打开我的应用程序时,我想停止我在退出应用程序时启动的服务。

代码是:

 Intent i_service   = new Intent(MainActivity.this,check.class);
 stopService(i_service); 

这是正确的做法吗?

如何停止我的旧服务?

提前致谢

【问题讨论】:

  • 是的,你的做法是正确的..
  • 如果我的服务已经停止了,是否会导致错误
  • 不,它不会导致任何错误..它会检查,如果它正在运行,那么它将停止..
  • 或者你可以服务是否正在运行,如果它正在运行则停止stackoverflow.com/questions/600207/…
  • 在服务中执行代码,例如每当工作完成时服务自动停止,然后无需检查服务是否正常工作;如果您再次启动服务,那么它会自动执行新的工作...

标签: android android-intent android-service android-intentservice


【解决方案1】:
Check Your service is run or not and then stop ur service on oncreate() of ur activity as below,



  ActivityManager manager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
    for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
        if (check.getName().equals(service.service.getClassName())) {
           Intent i_service   = new Intent(MainActivity.this,check.class);
 stopService(i_service); 
        }
    }

【讨论】:

    【解决方案2】:

    我认为你的做法是正确的。只是应用程序再次启动时,需要先检查它是否在运行,然后再停止。

    上面“MSS”描述的代码答案很好地解释了它!

    【讨论】:

      猜你喜欢
      • 2017-07-28
      • 1970-01-01
      • 1970-01-01
      • 2018-01-02
      • 2017-03-17
      • 1970-01-01
      • 2015-01-17
      • 2014-12-15
      • 2012-02-27
      相关资源
      最近更新 更多