0

当我退出我的应用程序时,我想启动服务。

代码是:

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

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

代码是:

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

这是正确的做法吗?

如何停止我的旧服务?

提前致谢

4

2 回答 2

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); 
        }
    }
于 2014-07-04T11:12:10.620 回答
1

我认为您正在以正确的方式进行操作。只是应用程序再次启动时,需要先检查它是否在运行,然后再停止它。

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

于 2014-07-04T11:35:46.823 回答