2

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

1 Answers1

1

I got a Perfect Solution. I am testing in oreo with all OS like oxygen, color, and miui. I am developing a VoIP Application and in my application one sip service is continuing running after an app is destroying or killed by the user. one more thing user set app is always running background and battery optimization in the phone setting.

I am Using Alarm Manager For Keep my service alive it is better than a job scheduler and call service again. I implement this code in-service class oncreate() method.

PendingIntent mKeepAlivePendingIntent;

     public class CallService extends Service {

                @Override
                public void onCreate() {
                    super.onCreate();
                    Intent intent = new Intent(this, RestartServiceBroadcast.class);
                    mKeepAlivePendingIntent = PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
                    ((AlarmManager) this.getSystemService(Context.ALARM_SERVICE)).setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime() + 60000, 60000, mKeepAlivePendingIntent);
                }

            }

Create BroadcastReceiver to call service again when user killed an application from memory task

public class RestartServiceBroadcast extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent.getAction().equals("service.RestartService")) {
        // Here Call a Service
    }

    }
}

Manifest Like this

<receiver
                android:name=".service.receiver.RestartServiceBroadcast"
                android:enabled="true"
                android:exported="true">
                <intent-filter>
                    <action android:name="service.RestartService" />
                </intent-filter>
   </receiver>

        <service
            android:name=".service.CallService"
            android:enabled="true"
            android:exported="false"
            android:stopWithTask="false">

        </service>

Thanks, Happy Coding....

Mayur
  • 735
  • 4
  • 14
  • Hey! i am working for it.. please tell what is mKeepAlivePendingIntent and android:process=":sipStack" here what is sipStack? and – Android Dev Jul 27 '19 at 11:24
  • A PendingIntent is a token that you give to a foreign application (e.g. NotificationManager , AlarmManager , Home Screen AppWidgetManager , or other 3rd party applications), which allows the foreign application to use your application's permissions to execute a predefined piece of code. – Mayur Jul 27 '19 at 11:58
  • ..... when call broadcast it check action in onrecieve() method in broadcast...... if (intent.getAction().equals("service.RestartService")) { // Here Call a Service } – Mayur Jul 27 '19 at 12:00
  • Hey! i tried your trick. But, it not worked for me if i remove app from recent activity it force stop my app and no broadcast receiver also fires. – Android Dev Jul 29 '19 at 05:54
  • what it mean? please explain – Android Dev Jul 29 '19 at 06:05
  • Thanks!! and i found solution is was due to manufacture differences – Android Dev Aug 01 '19 at 11:59