6

My foreground service is killed on some devices like vivo after killing app, is there any workaround to keep it alive?

I am using foreground service like:

public class MyService extends IntentService {

    private final String TAG = "IntentService";
    private PowerManager.WakeLock wakeLock;

    public MyService() {
        super("MyService");
        setIntentRedelivery(true);
    }

    @Override
    protected void onHandleIntent(@Nullable Intent intent) {
        Log.d(TAG, "onHandleIntent: Service running");
        for (int i = 0; i < 20; i++) {
            Log.d(TAG, "onHandleIntent: service running status: " + i);
            SystemClock.sleep(3000);
        }
    }

    @Override
    public void onCreate() {
        Log.d(TAG, "onCreate: IntentService Created");
        super.onCreate();

        PowerManager powerManager = (PowerManager) getSystemService(POWER_SERVICE);
        this.wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
                "Service: WakeLock");
        this.wakeLock.acquire();
        Log.d(TAG, "onCreate: Wakelock acquired");


        Notification notification = new NotificationCompat.Builder(this, App.NOTIFICATION_CHANNEL_ID)
                .setContentTitle("Intent Service")
                .setContentText("Service running in background")
                .setSmallIcon(android.R.drawable.sym_def_app_icon)
                .build();
        startForeground(12, notification);
    }

    @Override
    public void onDestroy() {
        Log.d(TAG, "onDestroy: IntentService Destroyed");
        super.onDestroy();
        this.wakeLock.release();
        Log.d(TAG, "onDestroy: Wakelock released");
    }
}

Tenclea
  • 1,444
  • 2
  • 10
  • 23
  • 1
    Ask your user to change it's battery saving settings ? – Tenclea Aug 27 '20 at 10:07
  • 1
    Even if battery level is at normal mode, Foreground service is killed as soon as app is killed. – Anurag Mishra Aug 27 '20 at 10:10
  • 1
    It would do the same on my phone. Even if I had 100% of battery, my phone would automatically kill any services it can in order to save battery. They call it "smart mode" and it can be disabled in the app's settings. I might be wrong about this in your case, but you should try to check this just in case – Tenclea Aug 27 '20 at 10:18
  • 1
    Still it doesn't work. – Anurag Mishra Aug 27 '20 at 10:37
  • 1
    Depending on your Android OS from the phone manufacturer your application can be listed as "no-background-tasks" entity. I had something similar with my Xiaomi and nothing could help me before I found that list and allowed my app to run in the background – Sigmund Aug 27 '20 at 10:39
  • 1
    on destroy service again start foreground service – Abdur Rehman Aug 27 '20 at 10:42
  • 1
    @IgorSkryl how to get app allowed to run in background? – Anurag Mishra Aug 27 '20 at 10:49
  • 1
    @AbdurRehman starting service again in onDestroy method doesn't worked out. – Anurag Mishra Aug 27 '20 at 10:56
  • 1
    play song from this application and kill app, check it destroy its service, if not then check its algorithm to start service https://github.com/android/uamp – Abdur Rehman Aug 27 '20 at 12:27
  • 2
    play store link: https://play.google.com/store/apps/details?id=ch.blinkenlights.android.vanilla and github link: https://github.com/vanilla-music/vanilla opensource app – Abdur Rehman Aug 27 '20 at 12:30

1 Answers1

0

I got it worked using a workaround.

Registered a fake static implicit receiver like this:

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

<receiver android:name=".SampleBroadcast">
   <intent-filter>
      <action android:name="android.intent.action.BOOT_COMPLETED" />
   </intent-filter>
</receiver>

My SampleBroadcast File:

public class SampleBroadcast extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {

        if (Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction())) {
            Log.d("Foreground App", "Boot Completed");
        }

    }
}

This placed my app in autostart section of OS.

And now when i am starting my service, Even if i kill the app it is running.