停止远程进程中的服务

Ste*_*ler 5 android process android-service

我有一个名为MyService远程(特定于应用程序)进程调用的后台服务MyApp:MyOtherProcess. 会发生什么MyApp:MyOtherProcessMyService我滑开应用程序?

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);
}
Run Code Online (Sandbox Code Playgroud)

为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");
  }
}
Run Code Online (Sandbox Code Playgroud)

帮手:

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;
}
Run Code Online (Sandbox Code Playgroud)

AndroidManifest.xml中:

<service android:enabled="true" android:name="com.myapp.MyService" android:process=":MyOtherProcess" />
Run Code Online (Sandbox Code Playgroud)

为了解决这个问题,我开始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);
}
Run Code Online (Sandbox Code Playgroud)

为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");
  }
}
Run Code Online (Sandbox Code Playgroud)

我在带有Genymotion版本2.3.1的Samsung Galaxy S5上运行Android 4.4.4 API 19.

小智 0

我认为 IntentService 会对您的情况有所帮助。将您的服务扩展为 IntentService 而不是您希望自动停止的服务。

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