停止远程进程中的服务

时间:2015-03-26 19:33:04

标签: android process android-service

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

Apps =>运行应用程序UI告诉我我有0个进程和1个服务。我将此解释为MyApp: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???
  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);
}

为MyService:

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是如何维护的......这个过程甚至是首先被杀害......一个过程变得只是不活跃......所以迷失了!

当前解决方法

停止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);
}

为MyService:

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的Samsung Galaxy S5上运行Android 4.4.4 API 19.

1 个答案:

答案 0 :(得分:0)

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

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