Ich habe einen Hintergrunddienst mit der Bezeichnung MyService
in einem (anwendungsspezifischen) Remote-Prozess namens MyApp:MyOtherProcess
. Was passiert mit MyApp:MyOtherProcess
und MyService
, wenn ich die Anwendung wische?Stoppen von Diensten in Remoteprozessen
Die Apps => Running Apps UI sagt mir, ich habe 0 Prozesse und 1 Service. Ich interpretiere dies als MyApp:MyOtherProcess
ist nicht aktiv, aber wird aufwachen, wenn MyService
erwacht ist.
Denken MyService
ist noch gestartet, habe ich dann versucht, den Dienst zu stoppen.
Stopp 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");
}
}
Helper:
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" />
Um dies zu umgehen starte ich MyService
und sofort MyService
stoppen, wenn der Prozess wurde getötet. Das scheint eine schlechte Übung zu sein. Neustart eines Dienstes, nur um es eine schlechte Praxis zu töten? Ich habe das Gefühl, ich verstehe nicht, wie MyService
beibehalten wird, wenn der enthaltende Prozess "getötet" wird ... ist der Prozess sogar in erster Linie getötet ... kann ein Prozess einfach inaktiv werden ... so verloren!
Aktuelle Abhilfe
Stopp 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");
}
}
Ich bin mit Android 4.4.4 API 19 auf einem Samsung Galaxy S5 mit Genymotion Version 2.3.1.
ich verstehe es nicht: Sie rufen StopService erwartet onStartCommand aufgerufen werden? – pskink
Nein, ich rufe stopService an und erwarte, dass onDestroy aufgerufen wird. –
Warum also "stopImmediately" extra in der Absicht für stopService übergeben? was genau willst du erreichen? – pskink