如何在应用程序关闭时停止从多个活动调用的 Android 服务?
How to stop a Android service called from multiple activites when app is closed?
我有一个 public class Tracking extends Service
被几乎每个 activity 调用,像这样:
Intent serviceIntent = new Intent(this, Tracking.class);
serviceIntent.putExtra("name", "Tabla3b Kras");
serviceIntent.putExtra("time", TimeGenerator.getTime());
startService(serviceIntent);
而且效果很好,public void onCreate()
仅在第一次被调用,public int onStartCommand(Intent intent, int flags, int startId)
每隔一次被调用。
我遇到的问题是当用户关闭应用程序时。假设双击后退键或进入打开的应用程序列表并将其关闭。我收到 "Unfortunately, "appname" 已停止工作。
我知道我可以在 onStop() 中关闭服务,但由于我从几乎所有活动中调用服务,我必须在所有活动中实施 onStop() 还是只执行它就足够了在 MainActivity.java?如果用户在活动之间切换,服务会停止吗?
服务是后台进程。这并不意味着每个 activity.
start/stop
- 您可以在您的应用程序中启动您的服务一次。就像您在 第一个 activity(启动)或应用程序级别 class 中开始一样。您不需要在每个 activity.
启动服务
- 同样,您不需要停止每个 activity 的服务。您可以在处理
onBackPress()
. 时停止服务一次
How do I then pass variables to the service from other activites?
您可以通过多种方式在活动和服务之间进行通信。
- 使用
Binder
. You can follow this answer.
- 你可以使用
Handler
. Follow this.
- 你可以使用
BroadcastReceiver
. Follow this.
- 使用 GreenBot 的
EventBus
。由于易于实施,这个库现在越来越受欢迎。
如果你问我的偏好,我早些时候使用 BroadcastReceiver
& Binder
。但是现在我使用EventBus。你可以在EventBus documentation.
中简单的实现步骤
建议:
检查您的服务是否已经 运行。如果不是运行.
,请在使用前启动服务
private boolean isMyServiceRunning(Class<?> serviceClass) {
ActivityManager manager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
if (serviceClass.getName().equals(service.service.getClassName())) {
return true;
}
}
return false;
}
我有一个 public class Tracking extends Service
被几乎每个 activity 调用,像这样:
Intent serviceIntent = new Intent(this, Tracking.class);
serviceIntent.putExtra("name", "Tabla3b Kras");
serviceIntent.putExtra("time", TimeGenerator.getTime());
startService(serviceIntent);
而且效果很好,public void onCreate()
仅在第一次被调用,public int onStartCommand(Intent intent, int flags, int startId)
每隔一次被调用。
我遇到的问题是当用户关闭应用程序时。假设双击后退键或进入打开的应用程序列表并将其关闭。我收到 "Unfortunately, "appname" 已停止工作。
我知道我可以在 onStop() 中关闭服务,但由于我从几乎所有活动中调用服务,我必须在所有活动中实施 onStop() 还是只执行它就足够了在 MainActivity.java?如果用户在活动之间切换,服务会停止吗?
服务是后台进程。这并不意味着每个 activity.
start/stop- 您可以在您的应用程序中启动您的服务一次。就像您在 第一个 activity(启动)或应用程序级别 class 中开始一样。您不需要在每个 activity. 启动服务
- 同样,您不需要停止每个 activity 的服务。您可以在处理
onBackPress()
. 时停止服务一次
How do I then pass variables to the service from other activites?
您可以通过多种方式在活动和服务之间进行通信。
- 使用
Binder
. You can follow this answer. - 你可以使用
Handler
. Follow this. - 你可以使用
BroadcastReceiver
. Follow this. - 使用 GreenBot 的
EventBus
。由于易于实施,这个库现在越来越受欢迎。
如果你问我的偏好,我早些时候使用 BroadcastReceiver
& Binder
。但是现在我使用EventBus。你可以在EventBus documentation.
建议:
检查您的服务是否已经 运行。如果不是运行.
,请在使用前启动服务private boolean isMyServiceRunning(Class<?> serviceClass) {
ActivityManager manager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
if (serviceClass.getName().equals(service.service.getClassName())) {
return true;
}
}
return false;
}