Serviços Android: START_STICKY não funciona no Kitkat
Estou usando serviços em um aplicativo para ouvir quantas vezes o usuário pressiona seu botão de energia. A implementação estava funcionando bem em todos os dispositivos. Mas quando eu testei o aplicativo no Android Kitkat, notei algo errado.
Assim que desloco o aplicativo para longe dos aplicativos recentes, o aplicativo não escuta mais o botão liga / desliga.
Aqui está o código com o qual estou trabalhando:
public class Receiver extends Service {
Notification notification;
private static final int NOTIFICATION_ID = 0;
NotificationManager manager;
PendingIntent toOpen;
Intent intent;
private BroadcastReceiver POWER_BUTTON = new Powerbuttonrecceiver();
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
filter.addAction(Intent.ACTION_SCREEN_OFF);
registerReceiver(POWER_BUTTON, filter);
startNotify();
return START_STICKY;
}
@Override
public void onDestroy() {
// TODO Auto-generated method stub
unregisterReceiver(POWER_BUTTON);
dismissNotification();
super.onDestroy();
}
public void startNotify(){
manager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
int icon = R.drawable.ic_launcher_drop;
CharSequence tickerText = "Service activated";
CharSequence tickerContent = "Service is now on. You can press your power button and the app will listen to it. Tap to turn this feature off";
intent = new Intent(Receiver.this, Options.class);
toOpen = PendingIntent.getActivity(getApplicationContext(), 0, intent, 0);
notification = new NotificationCompat.Builder(getApplicationContext())
.setContentTitle(tickerText)
.setContentText(tickerContent)
.setSmallIcon(icon)
.setOngoing(true)
.setContentIntent(toOpen)
.build();
manager.notify(NOTIFICATION_ID, notification);
}
public void dismissNotification(){
manager.cancel(NOTIFICATION_ID);
}
}
Como se pode perceber, estou usando uma notificação para indicar que o serviço está ativo. O que me confunde é que, após o furto do aplicativo recentes, a notificação ainda permanece, o que está inativo é aBroadcastReceiver
? ou isso é falso?
Na apponDestroy
Eu não chamei nenhuma função para registrar ou cancelar o registro, assim como parar o serviço. Mais uma vez, esse problema é visto apenas no Android KitKat. Por favor, se vocês sabem o que está acontecendo. Ajudar :)
ATUALIZAR: Eu também notei com oPlay Music
em Kitkat, quando eu o tiro longe do aplicativo recente, a música pára. Isso é um bug no Kitkat? Mas os serviços de som / mediaplayer emSoundCloud
funciona mesmo quando é removido do aplicativo Recentes.
ATUALIZAR: Registrado comoEdição 63618 no rastreador de problemas do Android. Leia os comentários do problema para mais detalhes.