如何处理Android的多个通知

时间:2012-11-05 19:27:17

标签: android

我正在开发一个应用程序,用户可以在其中添加事件,然后在事件即将发生时通知他们。我知道通知应该有唯一的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();
        }
    }); 

}

1 个答案:

答案 0 :(得分:1)

通知需要唯一的ID。您正在为 Intent 创建唯一ID。要从AlarmManager发布通知,请在服务中使用BroadcastReceiver。当服务收到从AlarmManager发送的Intent时,服务可以创建并发布通知。