开发者

Scheduling notification using Timer android

I'm using a timer to schedule a notifcation

        GregorianCalendar date = new GregorianCalendar(2011, 2, 6, 16, 50);
    long task = date.getTimeIn开发者_如何学CMillis() - System.currentTimeMillis();



    Timer timer = new Timer();
    TimerTask timerTask = new TimerTask()
    {
        @Override
        public void run()
        {
            triggerNotification();
        }
    };
    timer.schedule(timerTask, task);

The timer will work if I manually enter a long value in the schedule method but does nothing it seems if I use the task variable. Basically I want to schedule a notification at the stated date and time in the Gregorian Calendar object. What am I doing wrong?


Got this to work by using this instead of the calendar:

    SimpleDateFormat formatter = new SimpleDateFormat("MM/dd/yyyy HH:mm");
    Date date = formatter.parse("02/06/2011 16:28");
timer.schedule(timerTask, date);


If the task you're scheduling is a while off the AlarmManager class might also be worth looking at: http://developer.android.com/reference/android/app/AlarmManager.html


You can do something like the following:

private void createStatusBarNotification(final String contentTitle, final String contentText, final String tickerText, final long millisec)
{ 
    //Date date = new Date(System.currentTimeMillis() + (1000 * 60 * 2));//in next 2 minutes
    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);            
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜