繁体   English   中英

Android先前的通知无法替换为新的通知

[英]Android previous notification can't be replaced with new notification

下面我用3个班

    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.clendar);

    time = ( TimePicker ) findViewById( R.id.tpEventTime );
    date = ( DatePicker ) findViewById( R.id.dpEventDate );      
    title = ( EditText ) findViewById ( R.id.etEventTitle );
    description = ( EditText ) findViewById ( R.id.etEventDescription );
    header = ( TextView ) findViewById ( R.id.txtMenuHeader );
    header.setText( R.string.calendar );
    back = ( Button ) findViewById( R.id.tabs_back );
    home = ( Button ) findViewById ( R.id.tabs_home );
    back.setOnClickListener( this );
    home.setOnClickListener( this );

    //---Button view---
    Button btnOpen = ( Button ) findViewById( R.id.btEventDone );
    btnOpen.setOnClickListener( new View.OnClickListener() {

        public void onClick(View v) {  

            String strT = title.getText().toString();
            String strDes = description.getText().toString();

            // check if user did not input anything
            if( strT.isEmpty() || strDes.isEmpty() ){

                Toast.makeText( getBaseContext(), "You have blank fields!", Toast.LENGTH_SHORT ).show();

            }
            else{

            //---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() );

            //---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 );

            int intMonth = month + 1;
            String strMonth = "";
            String strDay = "";

            if(month < 10){

                strMonth = "0" + intMonth;
            }
            if(day < 10){

                strDay  = "0" + day ;
            }

            //Date d = new Date(year, month, day);
            //SimpleDateFormat dateFormatter = new SimpleDateFormat(
                        //    "MM-dd-yyyy");
            //String strDate = dateFormatter.format(d);
            String strTitle = title.getText().toString();
            String strDescription = description.getText().toString();
            String strDate = strMonth + "-" + strDay + "-" + 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(), notifID, i, 0 );               


            //---sets the alarm to trigger---
            alarmManager.set( AlarmManager.RTC_WAKEUP, 
                calendar.getTimeInMillis(), displayIntent );
            finish();
        }

        }
    }); 

}

警报详细信息:

EditText title, description, date, time;
Button done;

@Override
public void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.alarmdetails);  

    //---look up the notification manager service---
    NotificationManager nm = ( NotificationManager ) 
        getSystemService( NOTIFICATION_SERVICE );

    title = ( EditText ) findViewById ( R.id.etDetailTitle );
    description = ( EditText ) findViewById ( R.id.etDetailDescription );
    date = ( EditText ) findViewById ( R.id.etDetailDate );
    time = ( EditText ) findViewById ( R.id.etDetailTime );
    done = ( Button ) findViewById ( R.id.btnAlarmDone );

    done.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            // TODO Auto-generated method stub
            finish();
        }
    });

    //---cancel the notification---
    nm.cancel( getIntent().getExtras().getInt( "NotifID" ) ); 

    Date d = new Date();
    SimpleDateFormat dateFormatter = new SimpleDateFormat(
                    "MM-dd-yyyy hh:mm");
    String strDate = dateFormatter.format(d);

    String strTitle = getIntent().getExtras().getString( "Title" );
    String strDescription = getIntent().getExtras().getString( "Description" );
    strDate = getIntent().getExtras().getString( "Date" );
    String strTime = getIntent().getExtras().getString( "Time" );
    title.setText( strTitle );
    description.setText( strDescription );
    date.setText( strDate );
    time.setText( strTime );


}
}

显示通知:

 @Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

//---get the notification ID for the notification; 
// passed in by the NotifyActivity---
int notifID = getIntent().getExtras().getInt( "NotifID" );
String strTitle = getIntent().getExtras().getString( "Title" );
String strDescription = getIntent().getExtras().getString( "Description" );
String strDate = getIntent().getExtras().getString( "Date" );
String strTime = getIntent().getExtras().getString( "Time" );

//---PendingIntent to launch activity if the user selects 
// the notification---
Intent i = new Intent( DisplayNotification.this, AlarmDetails.class );
i.putExtra( "NotifID", notifID );  
i.putExtra( "Title", strTitle );
i.putExtra( "Description", strDescription );
i.putExtra( "Date", strDate );
i.putExtra( "Time", strTime );

PendingIntent detailsIntent = 
    PendingIntent.getActivity(this, 0, i, 0);

NotificationManager nm = ( NotificationManager )
    getSystemService( NOTIFICATION_SERVICE );
Notification notif = new Notification(
    R.drawable.ic_launcher, 
    "iHealthFirst: Notification!",
    System.currentTimeMillis() );

CharSequence from = "iHealthFirst - New Notification";
CharSequence message = "This is your alert, click to view";        
notif.setLatestEventInfo(this, from, message, detailsIntent);
notif.flags = Notification.FLAG_INSISTENT;
notif.defaults |= Notification.DEFAULT_SOUND;

//---100ms delay, vibrate for 250ms, pause for 100 ms and
// then vibrate for 500ms---
notif.vibrate = new long[] { 100, 250, 100, 500};        
nm.notify( notifID, notif );

//---destroy the activity---
finish();
}

}

首次创建通知时,它可以工作。 但是当我创建另一个通知时,仍会显示以前的通知。 如何显示另一个通知? 谢谢

您可以更新当前版本,也可以取消已发布的版本,然后再创建新版本。

您可以使用相同的通知ID替换先前的通知。

nm.notify( notifID, notif );

这里对所有通知使用相同的notifID ,因此您可以将任何整数值用作notifID

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM