1

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

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

感谢帮助

4

2 回答 2

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

于 2013-02-11T10:39:47.887 回答
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);

TimeAlarm.class这样使用

public class TimeAlarm extends BroadcastReceiver {
NotificationManager nm;

@SuppressWarnings("deprecation")
@Override
public void onReceive(Context context, Intent intent) {     

    int notificationId = Integer.parseInt(intent.getData().getSchemeSpecificPart());     

    Intent alarmIntent = new Intent(context, TimeAlarm.class);
    alarmIntent.setData(Uri.parse("timer:" + notificationId));
    //alarmIntent.setAction(String.valueOf(alarm.ID));
    AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    PendingIntent displayIntent = PendingIntent.getBroadcast(context, 0, alarmIntent, 0);
    alarmManager.cancel(displayIntent);     

    nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);       

    PendingIntent contentIntent = PendingIntent.getActivity(context, 0, new Intent(), 0);
    Notification notif = new Notification(icon, tickerText, System.currentTimeMillis());
    notif.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
    nm.notify(1, notif);        
于 2013-02-11T11:07:07.730 回答