如何创建多个报警? [英] How can create more than one alarm?
问题描述
我能够创建和取消与下面的code报警。 我想创建多个报警。报警时间来自一个ArrayList。在此ArrayList,我想为每一个日期报警。而$ P $取消按钮的psses将只取消当前报警。我该怎么办呢?
公共无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.main);
AlarmManager alarmManager =(AlarmManager)getSystemService(Context.ALARM_SERVICE);
setOneTimeAlarm();
buttonCancel.setOnClickListener(新Button.OnClickListener(){
@覆盖
公共无效的onClick(查看为arg0){
AlarmManager alarmManager =(AlarmManager)getSystemService(ALARM_SERVICE);
alarmManager.cancel(pendingIntent);
//告诉我们所做的用户。
Toast.makeText(MainActivity.this!取消,Toast.LENGTH_LONG).show();
}
});
}
私人无效setOneTimeAlarm(){
意向意图=新的意图(这一点,AReceiver.class);
pendingIntent = PendingIntent.getBroadcast(此,0,意向,PendingIntent.FLAG_ONE_SHOT);
alarmManager.set(AlarmManager.RTC_WAKEUP,calendar.getTimeInMillis(),pendingIntent);
}
我会后有一种方法,我在我所创建的应用程序之一,做了一个例子。
在打电话报警,你需要发送一个额外的ID是这样的: TMP是一个对象,它具有一个唯一的ID。
意向意图=新的意图(DisplayActivity.this,AlarmReciever.class);
intent.setData(Uri.parse(计时器:+ tmp.getId()));
PendingIntent pendingIntent = PendingIntent.getBroadcast(DisplayActivity.this,1,意向,0);
AlarmManager AM =(AlarmManager)getSystemService(ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP,calendar.getTimeInMillis(),pendingIntent);
然后检索与该行的ID:
的Long.parseLong(intent.getData()。getSchemeSpecificPart())
在取消刚刚创建相同的悬而未决的意图与ID和呼叫alarmmanager.cancel()
编辑:
要取消对用户点击(这里是tmp.getId就派上用场了),我只是用这个code,我想你需要创建相同pendingIntent才能够取消该项目的具体报警具体的报警。
意向意图=新的意图(DisplayActivity.this,AlarmReciever.class);
intent.setData(Uri.parse(计时器:+ tmp.getId()));
PendingIntent pendingIntent = PendingIntent.getBroadcast(DisplayActivity.this,1,意向,0);
AlarmManager AM =(AlarmManager)getSystemService(ALARM_SERVICE);
am.cancel(pendingIntent);
AlarmReciever是我的BroadcastReceiver。 DisplayActivity是活动本身。
这是在AlarmReciever我有以下的code:
的Long.parseLong(intent.getData()。getSchemeSpecificPart())
I am able to create and cancel an alarm with the code below. I want to create more than one alarm. Alarm times comes from an arraylist. In this arraylist I would like to create an alarm for each date. And presses of the cancel button will cancel only the current alarm. How can I do it?
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
setOneTimeAlarm();
buttonCancel.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View arg0) {
AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
alarmManager.cancel(pendingIntent);
// Tell the user about what we did.
Toast.makeText(MainActivity.this, "Cancel!", Toast.LENGTH_LONG).show();
}
});
}
private void setOneTimeAlarm() {
Intent intent = new Intent(this, AReceiver.class);
pendingIntent = PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);
alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
}
I'll post an example with a way I did it in one of the apps I created.
When calling the alarm you need to send an extra id like this: Tmp is an object which has a unique ID.
Intent intent = new Intent(DisplayActivity.this,AlarmReciever.class);
intent.setData(Uri.parse("timer:"+tmp.getId()));
PendingIntent pendingIntent = PendingIntent.getBroadcast(DisplayActivity.this,1, intent, 0);
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP,calendar.getTimeInMillis(),pendingIntent);
and then you retrieve the id with this line:
Long.parseLong(intent.getData().getSchemeSpecificPart())
When cancelling just create the same pending intent with the ID and call alarmmanager.cancel()
Edit:
To cancel the specific alarm on the item the user clicked on (here is where tmp.getId comes in handy) I just used this code, I think you need to create the same pendingIntent to be able to cancel the specific alarm.
Intent intent = new Intent(DisplayActivity.this,AlarmReciever.class);
intent.setData(Uri.parse("timer:"+tmp.getId()));
PendingIntent pendingIntent = PendingIntent.getBroadcast(DisplayActivity.this,1, intent, 0);
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
am.cancel(pendingIntent);
AlarmReciever is my BroadcastReceiver. DisplayActivity is the activity itself.
It's in AlarmReciever I have the following code:
Long.parseLong(intent.getData().getSchemeSpecificPart())
这篇关于如何创建多个报警?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!