0

Je suis incapable de retrouver la même intention même après avoir passé le même ID de l'intention.Impossible de récupérer la même intention en attente.

J'ai vérifié que le récepteur de diffusion a exactement le même contexte utilisé lors de la création de l'intention en attente.

public class AlarmReceiver extends BroadcastReceiver { 

public static String NOTIFICATION_ID = "notification-id"; 
public static String NOTIFICATION = "notification"; 
public static MediaPlayer mMediaPlayer; 
public static NotificationManager notificationManager; 
public static Notification notification; 
public static int id; 
Context ctx; 

@Override 
public void onReceive(final Context context, Intent intent) { 
    ctx = context; 

    Log.d("WTF", ""+intent.getAction()); 

    if (intent.getAction() != null) { 
     String action = intent.getAction(); 
     switch (action) { 
      case "SNOOZE": 
       Log.v("shuffTest", "Pressed Snoozed"); 
       break; 
      case "STOP_ACTION": 
       Log.v("shuffTest", "Pressed Stop"); 
       mMediaPlayer.stop(); 
       notificationManager.cancel(id); 
       stopReceiverServices(); 
       break; 
     } 
    } else { 
     notificationManager = (NotificationManager) context.getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE); 
     Notification notification = intent.getParcelableExtra(NOTIFICATION); 
     id = intent.getIntExtra(NOTIFICATION_ID, 0); 
     notificationManager.notify(id, notification); 
     playAlarmSound(); 
    } 

    // This is the Intent to deliver to our service. 
    //Intent service = new Intent(context, SimpleWakefulService.class); 
    // Start the service, keeping the device awake while it is launching. 
    //startWakefulService(context, service) 
} 

public void stopReceiverServices(){ 
    Intent notificationIntent = new Intent(ctx.getApplicationContext(), AlarmReceiver.class); 
    notificationIntent.putExtra(AlarmReceiver.NOTIFICATION_ID, 123); 
    notificationIntent.putExtra(AlarmReceiver.NOTIFICATION, notification); 
    Log.d("TAG", "Context A1: "+ctx.getApplicationContext().toString()); 
    Log.d("TAG", "Context A2: "+ctx.toString()); 
    AlarmManager alarmManager = (AlarmManager) ctx.getApplicationContext().getSystemService(Context.ALARM_SERVICE); 
    Log.d("TAG", "AlarmManager Before: " + alarmManager); 
    PendingIntent pendingIntent = PendingIntent.getBroadcast(ctx.getApplicationContext(), 987654321, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT); 
    Log.d("TAG", "PendingIntent Before: " + pendingIntent); 
    alarmManager.cancel(pendingIntent); 
    pendingIntent.cancel(); 
    Log.d("TAG", "AlarmManager After: " + alarmManager); 
    Log.d("TAG", "PendingIntent After: " + pendingIntent); 
} 
/.. 
} 

Lors de la création de PendingIntent.

void setAlarm(Context context) { 
    mContext = context; 
    Intent notificationIntent = new Intent(context, AlarmReceiver.class); 
    notificationIntent.putExtra(AlarmReceiver.NOTIFICATION_ID, 123); 
    notificationIntent.putExtra(AlarmReceiver.NOTIFICATION, getNotification("Wake Up! Wake Up")); 

    AlarmManager alarmManager = (AlarmManager) context.getApplicationContext().getSystemService(Context.ALARM_SERVICE); 
    PendingIntent pendingIntent = PendingIntent.getBroadcast(context.getApplicationContext(), 987654321, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT); 

    Calendar calendar = Calendar.getInstance(); 
    calendar.set(Calendar.HOUR_OF_DAY, hourOfDay); 
    calendar.set(Calendar.MINUTE, minuteOfHour); 

    alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent); 

    Log.d("TAG", "AlarmManager After setting : " + alarmManager); 
    Log.d("TAG", "PendingIntent After setting : " + pendingIntent); 

    Log.d("Ctx setAlarm1 ", context.getApplicationContext().toString()); 
} 

Logcat:

06-14 20:01:39.297 23767-23767/com.apps.testapp D/TAG: AlarmManager After setting : [email protected] 
06-14 20:01:39.297 23767-23767/com.apps.testapp D/TAG: PendingIntent After setting : PendingIntent{7c1d34: [email protected]} 
06-14 20:01:39.297 23767-23767/com.apps.testapp D/Ctx setAlarm1: [email protected] 
06-14 20:01:39.305 23767-23881/com.apps.testapp E/Surface: getSlotFromBufferLocked: unknown buffer: 0xb91721a8 
06-14 20:01:44.334 23767-23767/com.apps.testapp D/WTF: null 
06-14 20:01:44.545 23767-23767/com.apps.testapp D/MediaPlayer: setSubtitleAnchor in MediaPlayer 
06-14 20:01:49.772 23767-23767/com.apps.testapp D/WTF: STOP_ACTION 
06-14 20:01:49.773 23767-23767/com.apps.testapp V/shuffTest: Pressed Stop 
06-14 20:01:49.777 23767-23767/com.apps.testapp D/TAG: Context A1: [email protected] 
06-14 20:01:49.777 23767-23767/com.apps.testapp D/TAG: Context A2: [email protected] 
06-14 20:01:49.777 23767-23767/com.apps.testapp D/TAG: AlarmManager Before: [email protected] 
06-14 20:01:49.780 23767-23767/com.apps.testapp D/TAG: PendingIntent Before: PendingIntent{cc07e1b: [email protected]} 
06-14 20:01:49.782 23767-23767/com.apps.testapp D/TAG: AlarmManager After: [email protected] 
06-14 20:01:49.782 23767-23767/com.apps.testapp D/TAG: PendingIntent After: PendingIntent{cc07e1b: [email protected]} 
+3

ce que vous essayez de faire? que voulez-vous dire par "exactement la même intention"? – x0r

+0

Je veux annuler le pendingIntent lorsque les utilisateurs rejettent la notification. – GenerikV

+0

Pourquoi pensez-vous que cela ne fonctionne pas comme vous le souhaitez? Qu'attendez-vous de voir? –

Répondre

0

Dans AlarmManager et PendingIntent enlever getApplicationConext()

void setAlarm(Context context) { 

... 

AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); 
PendingIntent pendingIntent = PendingIntent.getBroadcast(context), 987654321, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT); 

... 
} 
+0

context.getApplicationContext() et le contexte renvoie la même valeur, cela va changer n'importe quoi. – GenerikV

+0

Reportez-vous à ce https://stackoverflow.com/questions/33046375/e-surface-getslotfrombufferlocked-unknown-buffer-0xab7519c0/33898248 –

+0

^ce problème est une autre histoire, rien à voir avec pendingIntent ou alarmManager. – GenerikV