Answer the question
In order to leave comments, you need to log in
Always running service with no activity?
Hello, I tried several options, but none of them work.
Does someone know how to make such a service, or at least, so that it re-creates itself when an activity from the list is killed?
The service works, but as soon as I remove the activity from the stack, it stops working. Preferably starting with android 5(API 21).
Here's what I tried.
In service. Doesn't restart.
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
return START_STICKY;
}
@Override
public void onTaskRemoved(Intent rootIntent) {
Intent restartService = new Intent(this,
this.getClass());
restartService.setPackage(getPackageName());
PendingIntent restartServicePI = PendingIntent.getService(
this, 0, restartService,
0);
AlarmManager alarmService = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarmService.setRepeating(AlarmManager.RTC_WAKEUP, SystemClock.elapsedRealtime(), 5 * 1000, restartServicePI);
//Этот вариант тоже не работает
// getApplicationContext().startService(new Intent(getApplicationContext(), BackgroundService.class));
}
@Override
public void onDestroy() {
super.onDestroy();
sendBroadcast(new Intent("YouWillNeverKillMe"));
}
public class BootBroadcast extends BroadcastReceiver {
@Override
public void onReceive(Context ctx, Intent intent) {
Log.i("WTW", "ServeiceDestroy onReceive...");
ctx.startService(new Intent(ctx.getApplicationContext(), BackgroundService.class));
}
}
<receiver android:name=".BootBroadcast">
<!--<intent-filter>-->
<!--<action android:name="android.intent.action.BOOT_COMPLETED" />-->
<!--</intent-filter>-->
<intent-filter>
<action android:name="YouWillNeverKillMe" >
</action>
</intent-filter>
</receiver>
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question