Um serviço em primeiro plano é eliminado e a notificação é removida quando o aplicativo é retirado de aplicativos recentes
Eu tenho umforeground-service
com notificação. Quando o aplicativo é retirado da bandeja de aplicativos recentes, o serviço é interrompido e a notificação também é removida. Aqui está o código para o meu serviçoVoiceService.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) {
}
Estou usando um dispositivo Xiaomi. Também habilitei o Autostart nas configurações. Também tentei adicionar o código abaixo aonTaskRemoved()
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);
Nenhuma dessas soluções funcionou. Existe algum problema no meuonStartCommand()
?
Esta é a minha primeira pergunta SO. Alguém, por favor ajude.