Android 我怎样才能停止我以前的服务?
我想在退出应用程序时启动服务 代码是:Android 我怎样才能停止我以前的服务?,android,android-intent,android-service,android-intentservice,Android,Android Intent,Android Service,Android Intentservice,我想在退出应用程序时启动服务 代码是: Intent i_service = new Intent(MainActivity.this,check.class); startService(i_service); 当我重新打开我的应用程序时,我想停止我退出应用程序时启动的服务 代码是: Intent i_service = new Intent(MainActivity.this,check.class); stopService(i_service); 这是正确的方法吗 如
Intent i_service = new Intent(MainActivity.this,check.class);
startService(i_service);
当我重新打开我的应用程序时,我想停止我退出应用程序时启动的服务
代码是:
Intent i_service = new Intent(MainActivity.this,check.class);
stopService(i_service);
这是正确的方法吗
如何才能停止我的旧服务
提前谢谢我想你是用正确的方式做的。只是当应用程序再次启动时,您需要首先检查它是否正在运行,然后停止它
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);
}
}
上面用“MSS”描述的代码给出的答案解释得很好 是的,你的做法是正确的。如果我的服务已经停止,它是否导致错误不它不会导致任何错误..它将检查,如果它正在运行,那么它将停止..或者您可以检查服务正在运行或未使用,如果它在服务中运行,则停止运行代码,如每当工作完成服务自动停止,则无需检查服务是否正在运行;如果您再次启动服务,则它将自动执行新工作。。。