0

アクティビティに設定したBroadcastReceiverのonReceiveメソッドで繰り返しアラームを停止・解除したい。どこで cancel() メソッドを呼び出せばよいですか、BroadCastReceiver の OnReceive メソッドで alarmManager のインスタンスを取得するにはどうすればよいですか。

以下はコードスニペットです

public void onCreate(Bundle savedInstanceState) {

setContentView(R.layout.main);

Intent intent1 = new Intent(this, MyBroadcastReceiver.class);       

PendingIntent sender = PendingIntent.getBroadcast(this, 1234567, intent, 0);  

AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE)

alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()
                + (i * 1000), 60*1000, pendingIntent);

}

public class MyBroadcastReceiver extends BroadcastReceiver {

静的 int id;

@Override

public void onReceive(Context context, Intent intent) {

// Vibrate the mobile phone

Vibrator vibrator = (Vibrator) context
                .getSystemService(Context.VIBRATOR_SERVICE);

vibrator.vibrate(2000);

id=id+1;

NotificationManager notificationManager = (NotificationManager) context
                .getSystemService(Context.NOTIFICATION_SERVICE);

Notification notification = new Notification(R.drawable.ic_launcher,
                "A new notification", System.currentTimeMillis());

// Hide the notification after its selected

notification.flags |= Notification.FLAG_AUTO_CANCEL;

Intent intent1 = new Intent(context, Repeat.class);

PendingIntent activity = PendingIntent.getActivity(context, id, intent1,
                PendingIntent.FLAG_CANCEL_CURRENT);

notification.setLatestEventInfo(context, "This is the title",
                "Notified", activity);

notification.number += 1;


notificationManager.notify(id, notification);




 String as = Context.ALARM_SERVICE;

    PendingIntent sender = PendingIntent.getBroadcast(context, 1234567, intent, 0);  

    AlarmManager amg = (AlarmManager) context.getSystemService(as);

    amg.cancel(sender); 


}

}  
4

1 に答える 1

0

アラームをキャンセルしたい場合は、次のコードを使用できます。(私はこれをテストしていませんが、同様のことを行って NotificationService をキャンセルします)

    String as = Context.ALARM_SERVICE;
    AlarmManager amg = (AlarmManager) this.getSystemService(as);
    PendingIntent myIntent = PendingIntent.getBroadcast(this.getApplicationContext(), 234324243, intent1, 0);
    amg.cancel(myIntent); 
于 2012-08-23T06:13:16.867 に答える