通知立即触发

时间:2013-02-11 10:30:11

标签: android android-intent calendar notifications

我是android新手。我想在本地设置通知。我想在特定的日期和时间开火。但如果我把它设置为未来它会立即触发?

 Calendar calendar = Calendar.getInstance();       

         //---sets the time for the alarm to trigger---
         calendar.set(Calendar.YEAR, 2013);
         calendar.set(Calendar.MONTH, 1);
         calendar.set(Calendar.DAY_OF_MONTH, 11);                 
         calendar.set(Calendar.HOUR_OF_DAY, 15);
         calendar.set(Calendar.MINUTE, 54);
         //calendar.set(calendar.AM_PM,1);
         calendar.set(Calendar.SECOND, 0);


        String ns = Context.NOTIFICATION_SERVICE;
        NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);

        int icon = R.drawable.alp_btn_code_lock_touched_holo;        
        CharSequence tickerText = "Hello"; // ticker-text
        long when = System.currentTimeMillis();         
        Context context = getApplicationContext();     
        CharSequence contentTitle = "Hello";  
        CharSequence contentText = "Hello";      
        Intent notificationIntent = new Intent(this, Login.class);
        PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
        Notification notification = new Notification(icon, tickerText, calendar.getTimeInMillis());
        notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);


        mNotificationManager.notify(1, notification);

感谢您的帮助

2 个答案:

答案 0 :(得分:0)

使用AlarmManager代替直接使用通知。为此,您还必须实施BroadCastReceiver签出链接

http://smartandroidians.blogspot.in/2010/04/alarmmanager-and-notification-in.html

http://maephv.blogspot.in/2011/08/android-alarmmanager-and-notification.html

答案 1 :(得分:0)

Broadcasrreceiver中使用Alarmmanager,因为您的代码会像这样更改

 Calendar calendar = Calendar.getInstance();       

     //---sets the time for the alarm to trigger---
     calendar.set(Calendar.YEAR, 2013);
     calendar.set(Calendar.MONTH, 1);
     calendar.set(Calendar.DAY_OF_MONTH, 11);                 
     calendar.set(Calendar.HOUR_OF_DAY, 15);
     calendar.set(Calendar.MINUTE, 54);
     //calendar.set(calendar.AM_PM,1);
     calendar.set(Calendar.SECOND, 0);

  Intent intentval = new Intent(YourActivity.this, TimeAlarm.class);

    PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intentval, 0);
    alarm.set(AlarmManager.ELAPSED_REALTIME,  calendar.getTimeInMillis() , pendingIntent);
<{1>}中的

像这样使用

TimeAlarm.class