【发布时间】:2015-03-26 19:33:04
【问题描述】:
我在名为MyApp:MyOtherProcess 的远程(特定于应用程序)进程中有一个名为MyService 的后台服务。 当我滑开应用程序时,MyApp:MyOtherProcess 和 MyService 会发生什么情况?
Apps => Running Apps UI 告诉我我有 0 个进程和 1 个服务。我将此解释为 MyApp:MyOtherProcess 未激活,但会在 MyService 被唤醒时唤醒。
以为MyService还在启动,于是我尝试停止服务。
停止我的服务:
Intent i = new Intent(context, MyService.class);
if (isMyOtherProcessRunning(context)) {
//Test case: I have swiped away the application
//Ahh, nothing is logged so I think MyService.onDestory is not invoked!
//Is MyService still running???
context.stopService(context, i);
} else {
//Test case: I have not swiped away the application
//Good, "destroying" is logged so MyService.onDestroy was invoked!
context.stopService(context, i);
}
我的服务:
public MyService : Service {
@Override
public void onCreate() {
Log.d("MyService", "creating");
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.d("MyService", "starting");
return START_REDELIVER_INTENT;
}
@Override
public void onDestroy() {
Log.d("MyService", "destroying");
}
}
助手:
public boolean isMyOtherProcessRunning(Context applicationContext) {
ActivityManager activityManager = (ActivityManager)applicationContext.getSystemService(Context.ACTIVITY_SERVICE);
List<RunningAppProcessInfo> procInfos = activityManager.getRunningAppProcesses();
for(int i = 0; i < procInfos.size(); i++)
{
String processName = procInfos.get(i).processName;
if(processName.equals("MyApp:MyOtherProcess")) {
return true;
}
}
return false;
}
AndroidManifest.xml:
<service android:enabled="true" android:name="com.myapp.MyService" android:process=":MyOtherProcess" />
为了解决这个问题,我启动MyService,如果进程已被终止,则立即停止MyService。这似乎是不好的做法。 重新启动服务只是为了杀死它是一种不好的做法吗?我觉得我不明白当包含进程被“杀死”时 MyService 是如何维护的......进程甚至被杀死了首先...进程是否可以变得不活跃...迷路了!
当前的解决方法
停止我的服务:
Intent i = new Intent(context, MyService.class);
if (isMyOtherProcessRunning(context)) {
//Test case: I have swiped away the application
//Ahh, nothing is logged so I think MyService.onDestory is not invoked!
//Is MyService still running???
i.putExtra("stopImmediately", true);
context.stopService(context, i);
} else {
//Test case: I have not swiped away the application
//Good, "destroying" is logged so MyService.onDestroy was invoked!
context.stopService(context, i);
}
我的服务:
public MyService : Service {
@Override
public void onCreate() {
Log.d("MyService", "creating");
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.d("MyService", "starting");
if (intent.getBooleanExtra("stopImmediately", false)) {
this.stopSelf();
return START_NOT_STICKY;
}
return START_REDELIVER_INTENT;
}
@Override
public void onDestroy() {
Log.d("MyService", "destroying");
}
}
我在带有 Genymotion 版本 2.3.1 的三星 Galaxy S5 上运行 Android 4.4.4 API 19。
【问题讨论】:
-
我不明白:您调用 stopService 期望调用 onStartCommand?
-
不,我调用 stopService 并希望调用 onDestroy。
-
那么为什么要在 stopService 的意图中额外传递“stopImmediately”?你到底想达到什么目标?
-
我不明白为什么 stopService() 没有调用 onDestroy()。对于解决方法,我尝试启动命令(这似乎唤醒了远程进程)。然后在我启动命令后立即停止它。我的解决方法在调用 onStartCommand() 和 onDestroy() 时按预期工作。
-
stopService 返回什么?
标签: android process android-service