Android通知时间

Android通知时间,android,notifications,Android,Notifications,我有一个公共类来做通知 public class notifyHelper { public void sendNotification(Activity caller, Class<?> activityToLaunch, String title, String msg, int numberOfEvents, boolean flashLed, boolean vibrate, int ID) { NotificationManager notifier = (Not

我有一个公共类来做通知

public class notifyHelper  {
public void sendNotification(Activity caller, Class<?> activityToLaunch, String title, String msg, int numberOfEvents, boolean flashLed, boolean vibrate, int ID) {
    NotificationManager notifier = (NotificationManager) caller.getSystemService(Context.NOTIFICATION_SERVICE);

    // Create this outside the button so we can increment the number drawn over the notification icon.
    // This indicates the number of alerts for this event.
    long whenTo = System.currentTimeMillis() + (1000 * 60 * 15);
    final Notification notify = new Notification(R.drawable.icon, "", whenTo);

    notify.icon = R.drawable.icon;
    notify.tickerText = "TV Spored ++";
    notify.when = whenTo;
    notify.number = numberOfEvents;
    notify.flags |= Notification.FLAG_AUTO_CANCEL;

    if (flashLed) {
    // add lights
        notify.flags |= Notification.FLAG_SHOW_LIGHTS;
        notify.ledARGB = Color.CYAN;
        notify.ledOnMS = 500;
        notify.ledOffMS = 500;
        notify.defaults |= Notification.DEFAULT_SOUND;
    }

    if (vibrate) {

        notify.vibrate = new long[] {100, 200, 200, 200, 200, 200, 1000, 200, 200, 200, 1000, 200};
    }

    Intent toLaunch = new Intent(caller, activityToLaunch);
    PendingIntent intentBack = PendingIntent.getActivity(caller, 0, toLaunch, 0);

    notify.setLatestEventInfo(caller, title, msg, intentBack);
    notifier.notify(ID, notify);
}

public static void clear(Activity caller) {
    NotificationManager notifier = (NotificationManager) caller.getSystemService(Context.NOTIFICATION_SERVICE);
    notifier.cancelAll();
}
}
公共类notifyHelper{
public void sendNotification(活动调用方、类activityToLaunch、字符串标题、字符串消息、int numberOfEvents、布尔flashLed、布尔振动、int ID){
NotificationManager通知程序=(NotificationManager)caller.getSystemService(Context.NOTIFICATION\u服务);
//在按钮外创建此按钮,以便我们可以增加在通知图标上绘制的数字。
//这表示此事件的警报数。
long whenTo=System.currentTimeMillis()+(1000*60*15);
最终通知通知=新通知(R.drawable.icon,”,whenTo);
notify.icon=R.drawable.icon;
notify.tickerText=“TV Spored++”;
通知。何时=何时;
notify.number=numberOfEvents;
notify.flags |=notify.FLAG_AUTO_CANCEL;
如果(闪光灯){
//加灯
notify.flags |=notify.FLAG_显示_灯光;
notify.ledARGB=Color.CYAN;
notify.ledOnMS=500;
notify.ledOffMS=500;
notify.defaults |=notify.DEFAULT_声音;
}
如果(振动){
notify.vibrate=新长[]{10020020020020020020010002002002002002002001000200};
}
Intent toLaunch=新的Intent(调用者,activityToLaunch);
pendingent intentBack=pendingent.getActivity(调用者,0,toLaunch,0);
notify.setLatestEventInfo(呼叫者、标题、消息、意图返回);
通知者。通知(ID,notify);
}
公共静态无效清除(活动调用方){
NotificationManager通知程序=(NotificationManager)caller.getSystemService(Context.NOTIFICATION\u服务);
notifier.cancelAll();
}
}
无论我做什么(查看时间),调用时始终显示此通知。。。如何设置通知时间


谢谢你的回答

是什么让你认为有一种方法可以创建一个非即时的通知


从中你可以看到一些重要的事情。首先,您正在使用的构造函数已被弃用——您应该改用它。但更重要的是,第三个参数不是“何时显示通知”,而是在通知本身的时间字段中显示的时间。要想象这个。。。打电话但不接听(以引起未接来电通知)。然后下拉通知抽屉,注意右下角显示的时间。

这可能会对您有所帮助

private void createNotification(String contentTitle, String contentText, String tickerText, long millisec){     
        notificationManager = (NotificationManager) this.ctx.getSystemService(Context.NOTIFICATION_SERVICE);        
        note = new Notification(android.R.drawable.btn_star_big_on, tickerText, millisec );
        note.when = millisec;        
        Intent notificationIntent = new Intent(this.ctx, CenteringActivity.class);
        notificationIntent.setAction(Intent.ACTION_MAIN);
        notificationIntent = notificationIntent.setFlags(Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
        contentIntent = PendingIntent.getActivity(this.ctx, 0, notificationIntent, 0);
        note.setLatestEventInfo(this.ctx, contentTitle, contentText, contentIntent);
        note.number = 1;//Just created notification so number=1. Remove this line if you don't want numbers       
        notificationManager.notify(notif_ID, note);
    }    

 private void createStatusBarNotification(final String contentTitle, final String contentText, final String tickerText, final long millisec)
    { 
        //Date date = new Date(System.currentTimeMillis() + (1000 * 60 * 2));
        //long f = System.currentTimeMillis() + (1000 * 60 * 2);
        //super.webView.loadUrl("javascript: alert('::"+millisec+","+f+"')");
        Date date = new Date(millisec);
        Timer timer = new Timer();
        TimerTask timerTask = new TimerTask(){
            @Override
            public void run(){
                createNotification( contentTitle,  contentText,  tickerText,  millisec);
            }
        };
        timer.schedule(timerTask, date, 1000*60);//(1000*60)will repeate the same notification in 1 minute   
    }

您必须使用来安排通知。

谢谢您的回答。。。像许多应用程序一样的通知怎么样。。。类似日历的通知。。。我想做的是为将来的事情做一个提醒(比如明天上午10点)…@M.V.-在它准备好被看到之前,你不能提交通知。我想大多数应用程序都会使用一个AlarmManager来管理它,该AlarmManager计划在下一次通知时启动;此时,他们会提交通知,并确定下一个通知何时发出,并安装新的AlarmManager条目。