ユーザーがイベントを追加して、イベントが発生しようとしているときに通知できるアプリケーションを開発しています。通知には一意の 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();
}
});
}