我正试图在某个时间和日期发出警报或通知。我希望能够为数组中的特定日期提供多个警报。我不知道在哪里继续我的按钮工作:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Button button = (Button)findViewById(R.id.button1);
// button.setOnClickListener(this);
final CheckBox checkBox = (CheckBox) findViewById(R.id.checkBox1);
final Button button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v) {
if (checkBox.isChecked()) {
}
}
提前感谢您帮助我。
答案 0 :(得分:3)
您将要使用Alarm Manager
这个tutorial是一个非常简单的例子,如何使用它。
另一个简单的exmaple将其分解为
的基本步骤 修改强>
要发出通知,请修改广播接收器以在收到警报事件时创建通知。
Android Status Notification Documentation很好地展示了创建通知的基本步骤。
String ns = Context.NOTIFICATION_SERVICE;
NotificationManager mNotificationManager =(NotificationManager)receiverContext.getSystemService(ns);
Notification notification = new Notification(R.drawable.notification_icon, "Hello", System.currentTimeMillis());
CharSequence contentTitle = "My notification";
CharSequence contentText = "Hello World!";
Intent notificationIntent = new Intent(receiverContext, TargetActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(receiverContext, 0, notificationIntent, 0);
notification.setLatestEventInfo(receiverContext, contentTitle, contentText, contentIntent);
private static final int notificationID = 1;
mNotificationManager.notify(notificationID, notification);
编辑2
Creating notification at specified time using Alarm Manager
答案 1 :(得分:1)
您可以尝试这样:
ArrayList<Calendar> alarmTimes = getAlarmTimes(); //Make your own function which returns alarm times as an array of Calendar type
for(int i=0; i<alarmTimes.size(); i++)
{
Intent intent = new Intent(this, YourClass.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this.getApplicationContext(), requestCode, intent, PendingIntent.FLAG_UPDATE_CURRENT | Intent.FILL_IN_DATA);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, alarmTimes[i].getTimeInMillis(), pendingIntent);
}
如果您想独立处理每个警报,请为每个警报使用不同的 requestCode 。 此外,您可能需要查看 alarmManager.set()函数中使用的不同标志。根据您的应用需求进行修改。
http://developer.android.com/reference/android/app/PendingIntent.html
你必须制作一个AlarmReceiver类来处理警报响起时你想做的事情(确保在AndroidManifest中将此类添加为 receiver )。如果您想发送额外信息以及警报,那么您可以使用 intent extras ,如下所示:
intent.putExtra("info1", value1);
intent.putExtra("info2", value2);
其中 info1 是额外数据的名称,value1是您发送的值。
希望这有帮助!
编辑:
ArrayList<Calendar> alarmTimes = new ArrayList<Calendar>();
Calendar calendar = Calendar.getInstance();
calendar.set(CALENDAR.MONTH, 7);
calendar.set(CALENDAR.DAY_OF_MONTH, 17);
calendar.set(CALENDAR.HOUR_OF_DAY, 13);
calendar.set(CALENDAR.MINUTE, 0);
alarmTimes.add(calendar);
//Repeat the above code for all alarm times and then return array object
return alarmTimes;
答案 2 :(得分:0)
Calendar cal = Calendar.getInstance(); //retrieves a calendar object w/ current time
cal.add(cal.MINUTE, 1); //adds 1 minute to current time
Intent alarmIntent = new Intent(this, CustomAlarmReceiver.class);
/* creates a new PendingIntent using the static variable eventID.
* using eventID allows you to create multiple events with the same code
* without a unique id the intent would just be updated with new extras each time its created
*/
PendingIntent pendingAlarm = PendingIntent.getBroadcast(this, eventID, alarmIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
eventID+=1;
/* get the Alarm service and set the alarm with the time to go off and what to do when the
* alarm is received */
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), pendingAlarm);
Intent intent1=new Intent(getApplicationContext(),ListActivity.class);
startActivity(intent1);
intent1.putExtra("date", date);
intent1.putExtra("task", message);
}