Время уведомления Android - PullRequest
2 голосов
/ 26 мая 2011

У меня есть публичный класс для отправки уведомлений ...

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

Независимо от того, что я делаю (посмотрите когда), это уведомление всегда отображается при вызове ... Как я могу установить время уведомления?

Спасибо за ответ!

Ответы [ 3 ]

3 голосов
/ 26 мая 2011

Что заставляет вас думать, что есть способ создать уведомление, которое не является немедленным?

С http://developer.android.com/reference/android/app/Notification.html вы можете увидеть несколько важных вещей. Во-первых, используемый вами конструктор устарел - вы должны использовать http://developer.android.com/reference/android/app/Notification.Builder.html. Что еще более важно, 3-й параметр - это не «когда показывать уведомление», это время для отображения в поле времени самого уведомления. Чтобы визуализировать это ... позвоните на свой телефон и не отвечайте (чтобы вызвать уведомление о пропущенном звонке). Затем откройте ящик уведомлений и обратите внимание на время, которое отображается в правом нижнем углу.

1 голос
/ 17 ноября 2011

может быть это вам поможет

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   
    }
0 голосов
/ 27 февраля 2012

Вы должны использовать AlarmManager , чтобы запланировать уведомление.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...