我正在开发一个应用程序,用户可以在其中添加事件,然后在事件即将发生时通知他们。我知道通知应该有唯一的 ID,但我将如何在我的应用程序上实现它?有什么建议么?或者你知道的任何教程会对我有所帮助。谢谢。
这是我迄今为止尝试过的:
public class CalendarEvent extends Activity {
TimePicker time;
DatePicker date;
EditText title, description;
int notifID;
private int year;
private int month;
private int day;
private int hour;
private int minute;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.clendar);
//---Button view---
Button btnOpen = ( Button ) findViewById( R.id.btEventDone );
btnOpen.setOnClickListener( new View.OnClickListener() {
public void onClick(View v) {
time = ( TimePicker ) findViewById( R.id.tpEventTime );
date = ( DatePicker ) findViewById( R.id.dpEventDate );
title = ( EditText ) findViewById ( R.id.etEventTitle );
description = ( EditText ) findViewById ( R.id.etEventDescription );
//---use the AlarmManager to trigger an alarm---
AlarmManager alarmManager = ( AlarmManager ) getSystemService( ALARM_SERVICE );
//---get current date and time---
Calendar calendar = Calendar.getInstance();
//---sets the time for the alarm to trigger---
calendar.set( Calendar.YEAR, date.getYear() );
calendar.set( Calendar.MONTH, date.getMonth() );
calendar.set( Calendar.DAY_OF_MONTH, date.getDayOfMonth() );
calendar.set( Calendar.HOUR_OF_DAY, time.getCurrentHour() );
calendar.set( Calendar.MINUTE, time.getCurrentMinute() );
calendar.set( Calendar.SECOND, 0 );
//---PendingIntent to launch activity when the alarm triggers---
Intent i = new Intent( CalendarEvent.this, DisplayNotification.class );
year = calendar.get( Calendar.YEAR );
month = calendar.get( Calendar.MONTH );
day = calendar.get( Calendar.DAY_OF_MONTH );
hour = calendar.get( Calendar.HOUR );
minute = calendar.get( Calendar.MINUTE );
String strTitle = title.getText().toString();
String strDescription = description.getText().toString();
String strDate = String.valueOf( month ) + "-" + String.valueOf( day ) + "-" + String.valueOf( year );
StringBuilder sb = new StringBuilder();
if(hour>=12){
sb.append(hour-12).append( ":" ).append(minute).append(" PM");
}else{
sb.append(hour).append( ":" ).append(minute).append(" AM");
}
String strTime = sb.toString();
//---assign an ID of 1---
i.putExtra( "NotifID", notifID );
i.putExtra( "Title", strTitle );
i.putExtra( "Description", strDescription );
i.putExtra( "Date", strDate );
i.putExtra( "Time", strTime );
PendingIntent displayIntent = PendingIntent.getActivity(
getBaseContext(), 0, i, 0 );
//---sets the alarm to trigger---
alarmManager.set( AlarmManager.RTC_WAKEUP,
calendar.getTimeInMillis(), displayIntent );
finish();
}
});
}