Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/184.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/powerbi/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 停止远程进程中的服务_Android_Process_Android Service - Fatal编程技术网

Android 停止远程进程中的服务

Android 停止远程进程中的服务,android,process,android-service,Android,Process,Android Service,在一个名为MyApp:MyOtherProcess的远程(特定于应用程序)进程中,我有一个名为MyService的后台服务当我刷走应用程序时,MyApp:MyOtherProcess和MyService会发生什么情况? Apps=>Running Apps UI告诉我我有0个进程和1个服务。我将其解释为MyApp:MyOtherProcess未激活,但在MyService被唤醒时将被唤醒 我认为MyService仍在启动,然后尝试停止该服务 停止我的服务: Intent i = new Inte

在一个名为
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");
  }
}
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");
  }
}
我的服务:

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");
  }
}
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");
  }
}
助手:

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;
}
我的服务:

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");
  }
}
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");
  }
}

我正在三星Galaxy S5上运行Android 4.4.4 API 19,Genymotion版本为2.3.1。

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


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

我不明白:您调用stopService期望调用onStartCommand?不,我调用stopService并期望调用onDestroy。那么,为什么要在stopService的意图中额外传递“StopInstance”呢?你到底想实现什么?我不明白为什么stopService()不调用onDestroy()。为了解决这个问题,我尝试启动这个命令(它似乎唤醒了远程进程)。然后在我启动命令后立即停止它。我的变通方法在onStartCommand()和onDestroy()都被调用的地方可以正常工作。stopService返回什么?IntentService看起来非常有用!但是,既然我已经处于一个不同的过程中,那么这是必要的吗?但是您正在创建一个服务,不是吗?只需创建IntentService类型,即完成工作后要停止的服务。它就是为了这个目的而设计的。但我希望我的服务能够长期运行。