0

インテントの同じIDを渡しても同じインテントを取得できません。同じ保留中のインテントを取得できません。

保留中のインテントを作成するときに使用したのとまったく同じコンテキストがブロードキャスト受信者にあることを確認しました。

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); 
} 
/.. 
} 

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

を削除しますか? 「まったく同じ意図」とはどういう意味ですか? – x0r

+0

ユーザーが通知を却下したときに保留中の通知を取り消したい – GenerikV

+0

あなたはこれがなぜあなたが望むように働いていると思いませんか?あなたは何を見たいですか? –

答えて

0

alarmManagerとPendingIntentであなたが何をすべきかをしようとしている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()とcontextは同じ値を返します。何も変わりません。 – GenerikV

+0

この問題を参照するには、https://stackoverflow.com/questions/33046375/e-surface-getslotfrombufferlocked-unknown-buffer-0xab7519c0/33898248 –

+0

^その問題は別の話です.pendingIntentやalarmManagerとは関係ありません。 – GenerikV

関連する問題