【问题标题】:Stopping services in remote processes停止远程进程中的服务
【发布时间】:2015-03-26 19:33:04
【问题描述】:

我在名为MyApp:MyOtherProcess 的远程(特定于应用程序)进程中有一个名为MyService 的后台服务。 当我滑开应用程序时,MyApp:MyOtherProcessMyService 会发生什么情况?

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


【解决方案1】:

我认为 IntentService 会帮助您解决问题。将您的服务扩展为 IntentService 而不是服务,您希望自动停止。

IntentService 是按需处理异步请求(表示为 Intent)的服务的基类。客户端通过 startService(Intent) 调用发送请求;服务根据需要启动,使用工作线程依次处理每个 Intent,并在工作结束时自行停止。

【讨论】:

  • IntentService 似乎真的很有用!但是有必要吗,因为我已经在不同的过程中了?
  • 但是您正在创建服务,不是吗?只需创建 IntentService 类型的服务,即完成工作后要停止的服务。它就是为此目的而制作的。
  • 但我希望我的服务能够长期运行。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-10-05
  • 1970-01-01
  • 1970-01-01
  • 2012-01-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多