In one of my application i am fetching user current location using foreground service... my problem is when i remove app from recent application it force-stop my application and stop my service for this device i am getting such issue I tried in to my service like below
@Override
public void onTaskRemoved(Intent rootIntent) {
super.onTaskRemoved(rootIntent);
//TRY 1 but not worked!!!!
// Intent broadcastIntent = new Intent("uk.ac.shef.oak.ActivityRecognition.RestartSensor");
// sendBroadcast(broadcastIntent);
// stoptimertask();
// Intent broadcastIntent = new Intent(this, SensorRestarterBroadcastReceiver.class);
// sendBroadcast(broadcastIntent);
//
// writeDataInLogFile("Service onTaskRemoved ", " TRIED TO RESTART SERVICE FROM ONDESTROY ");
Log.i(TAG, "Task Removed!!!!!!!!!!!!!!!!!!!!!!");
try {
//TRY 2 ALSO NOT WORKING
Intent restartServiceIntent = new Intent(getApplicationContext(), this.getClass());
restartServiceIntent.setPackage(getPackageName());
PendingIntent restartServicePendingIntent = PendingIntent.getService(getApplicationContext(), 1, restartServiceIntent, PendingIntent.FLAG_ONE_SHOT);
AlarmManager alarmService = (AlarmManager) getApplicationContext().getSystemService(Context.ALARM_SERVICE);
alarmService.set(
AlarmManager.ELAPSED_REALTIME,
SystemClock.elapsedRealtime() + 1000,
restartServicePendingIntent);
} catch (Exception e) {}
And in manifest i've added my above service as
<service
android:name=".service.LocationUpdateForegroundService"
android:enabled="true"
android:stopWithTask="false"
android:exported="true" />
But when i remove app from recent app it force-close my app and if i check app in setting it display as force-close app.(And above issue is related to some device only.. not getting such issue in every device ) what should i do? please help me As per suggestion i done like in to my service class But yet not getting my solution!!!
try {
System.out.println("this is start!!!!!!!!!!!!!!!!!!!!");
Intent intent = new Intent(this, PowerOnOffBroadcastReceiver.class);
intent.setAction("service.RestartService");
PendingIntent mKeepAlivePendingIntent = PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
((AlarmManager) this.getSystemService(Context.ALARM_SERVICE)).setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime() + 10000, 10000, mKeepAlivePendingIntent);
}
catch (Exception e)
{
System.out.println("new implementation !!!!!!!!!!!!!!!!!");
}
I found some solution is : This But yet it stop my service when user click on close button of recent activity and it clears all apps from memory