J'ai un service qui boucle essentiellement de 0-9 et dort 1000 pour chaque boucle. Ce service crée également une notification qui montre la progression. Je voulais ajouter une action qui me permet d'annuler le service. J'ai les codes suivants, mais cela ne semble pas fonctionner.Comment annuler IntentService à partir d'une notification?
Je suis venu avec ce qui suit de ce qui suit:
Android Jelly Bean notifications with actions
Android notification .addAction deprecated in api 23
public class FileOperationService extends IntentService {
public FileOperationService() {
super("FileOperationService");
}
@Override
protected void onHandleIntent(@Nullable Intent intent) {
Intent deleteIntent = new Intent(this, CancelFileOperationReceiver.class);
deleteIntent.putExtra("notification_id",1);
PendingIntent pendingIntentCancel = PendingIntent.getBroadcast(this, 0, deleteIntent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationManagerCompat manager = (NotificationManagerCompat.from(this));
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
Intent notificationIntent = new Intent(this, MainActivity.class);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Action action = new NotificationCompat.Action.Builder(android.R.drawable.ic_menu_close_clear_cancel, "Cancel", pendingIntentCancel).build();
builder.setContentIntent(pendingIntent);
builder.setContentText("In Progress");
builder.setSmallIcon(R.mipmap.ic_launcher);
builder.addAction(action);
builder.setProgress(9, 0, false);
for (int i = 0; i < 10; i++) {
Log.d("Service", String.valueOf(i));
builder.setProgress(9, i, false);
if (i == 9) {
builder.setContentTitle("Done");
builder.setContentText("");
builder.setProgress(0, 0, false);
manager.notify(1, builder.build());
} else {
manager.notify(1, builder.build());
}
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
public class CancelFileOperationReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Intent service = new Intent();
service.setComponent(new ComponentName(context, FileOperationService.class));
context.stopService(service);
NotificationManagerCompat manager = (NotificationManagerCompat.from(context));
manager.cancel(intent.getIntExtra("notification_id",0));
Log.d("Cancel","true");
}
}
Je peux voir qu'il annule la notification car elle se ferme chaque fois que je clique sur Annuler. Cependant, il ne semble pas annuler le service IntentService lorsqu'une nouvelle notification apparaît sur chaque boucle.
Ce que je suis en train de réaliser est d'empêcher le service de se déplacer vers l'avant. Comme dans mon exemple. Si j'annule pendant la 4ème itération, alors je veux que le service s'arrête là. – ank
Ensuite, vous pouvez vider le bloc "else if" pour lequel vous ne voulez pas exécuter la tâche. lorsque l'itération s'arrête, le service s'arrête automatiquement. – SilverSky