8

I have a foreground-service with notification. When the app is swiped away from recent apps tray, service gets killed and notification also gets removed. Here is the code for my service VoiceService.class:

@Override
public IBinder onBind(Intent intent) {
    return null;
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {

        showNotification();
        Toast.makeText(this, "Service Started!", Toast.LENGTH_SHORT).show();


    return START_STICKY;
}
private void showNotification() {
    new SetupTask(this).execute(); // a method in my code which works fine.
    Intent notificationIntent = new Intent(this, MainActivity.class);
    notificationIntent.setAction(Constants.ACTION.MAIN_ACTION);
    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
            | Intent.FLAG_ACTIVITY_CLEAR_TASK);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,
            notificationIntent, 0);



    Notification notification = new NotificationCompat.Builder(this)
            .setContentTitle("TutorialsFace Music Player")
            .setContentText("My song")
            .setSmallIcon(R.drawable.ic_launcher_foreground)
            .setContentIntent(pendingIntent)
            .setOngoing(true)
            .build();
    startForeground(Constants.NOTIFICATION_ID.FOREGROUND_SERVICE, notification);

}
 @Override
public void onDestroy() {
    super.onDestroy();
}

    @Override
public void onTaskRemoved(Intent rootIntent) {
}

I am using a Xiaomi device. I have also enabled Autostart in settings. I also tried adding below code to onTaskRemoved()

    Intent intent = new Intent(getApplicationContext(), VoiceService.class);
    PendingIntent pendingIntent = PendingIntent.getService(this, 1, intent, PendingIntent.FLAG_ONE_SHOT);
    AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
    alarmManager.set(AlarmManager.RTC_WAKEUP, SystemClock.elapsedRealtime() + 1000, pendingIntent);
    super.onTaskRemoved(rootIntent);

None of these solutions worked. Is there any problem in my onStartCommand()?

Shubham AgaRwal
  • 4,355
  • 8
  • 41
  • 62
  • Possible [related question](https://stackoverflow.com/questions/53084293/android-kills-background-services-in-xiaomi-huawei-etc#comment93075090_53084293). – greeble31 Nov 03 '18 at 12:59
  • Problem with Xiaomi exists if Autostart is not enabled. I have enabled that too. I am calling my service like this : Intent serviceIntent = new Intent(MainActivity.this, VoiceService.class); startService(serviceIntent); Is it wrong? – Bibin Johny Nov 03 '18 at 13:06
  • Nope, that's the way you're supposed to call it. – greeble31 Nov 03 '18 at 13:12
  • Then, what am I missing? – Bibin Johny Nov 03 '18 at 13:14
  • That's not entirely clear. The linked question has not been answered. The community has apparently not figured out how to solve this problem yet. – greeble31 Nov 03 '18 at 13:17
  • This is the issue with Xiaomi MIUI. Did you check it on other devices as well? like on stock emulator? – Birju Vachhani Aug 19 '19 at 05:10

0 Answers0