enter image description hereI want to create an app which runs in the background. I have tried all stackoverflow solutions. Those solutions are all working when the app is cleared by myself. But when I'm performing "clear all recent apps". The service stops. I'm using Android v7.1.2 Android Nougat.
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
log("Received start command flags:%d, startId:%d", flags, startId);
Notification.Builder builder = new Notification.Builder(SOSService.this);
builder.setSmallIcon(R.mipmap.ic_launcher)
.setContentText("Ambysure SOS App is running")
.setAutoCancel(false);
Notification notification = builder.getNotification();
notification.flags |= Notification.FLAG_NO_CLEAR
| Notification.FLAG_ONGOING_EVENT;
startForeground(100, notification);
return START_STICKY;
}
@Override
public void onDestroy() {
super.onDestroy();
Log.i("EXIT", "ondestroy!");
Intent myIntent = new Intent(getApplicationContext(), SOSService.class);
PendingIntent pendingIntent = PendingIntent.getService(getApplicationContext(), 0, myIntent, 0);
AlarmManager alarmManager1 = (AlarmManager) getSystemService(ALARM_SERVICE);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.add(Calendar.SECOND, 10);
alarmManager1.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
Toast.makeText(getApplicationContext(), "Start Alarm", Toast.LENGTH_SHORT).show();
}
@Override
public void onTaskRemoved(Intent rootIntent) {
super.onTaskRemoved(rootIntent);
Log.i("TASKREMOVED", "onTaskRemoved!");
Intent myIntent = new Intent(getApplicationContext(), SOSService.class);
PendingIntent pendingIntent = PendingIntent.getService(getApplicationContext(), 0, myIntent, 0);
AlarmManager alarmManager1 = (AlarmManager) getSystemService(ALARM_SERVICE);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.add(Calendar.SECOND, 10);
alarmManager1.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
Log.i("TASKREMOVED", "Start Alarm!");
}