Android通知设置为1小时

时间:2015-02-25 09:42:58

标签: android time timer notifications

我试图显示推送通知,提醒用户检查他们选择的东西(做菜,去上班等)。我想将它设置为当前时间+ 1小时的首发。

但是,我尝试以下代码并且它不会更改任何内容(无论我设置的值如何,都会立即弹出通知)

 pushNotifAd.when = System.currentTimeMillis()+10000000;

这里有完整的代码,请指导我如何在最初触发后1小时内拨打电话:

//shows a notif on user tray, just pass the title and text!
public void pushNotif(String title,String text ){
    Context context = MainActivity.this
            .getApplicationContext();
    notificationManager = (NotificationManager) context
            .getSystemService(NOTIFICATION_SERVICE);

    pushNotifAd = new Notification();
    pushNotifAd.icon = R.drawable.icon;
    pushNotifAd.tickerText = "time to go to work!";
    pushNotifAd.when = System.currentTimeMillis()+3600000;//current time + 1 // hr in millisecs


    Uri uri = Uri.parse("work.com");
    Intent intent = new Intent(Intent.ACTION_VIEW, uri);

    notificationIntent = new Intent(Intent.ACTION_VIEW, uri);
    contentIntent = PendingIntent.getActivity(context, 0,
            notificationIntent, 0);


    pushNotifAd.setLatestEventInfo(context, "Go to work!",
            "Time to get stuff done!", contentIntent);

    notificationManager
            .notify(LIST_UPDATE_NOTIFICATION, pushNotifAd);

1 个答案:

答案 0 :(得分:1)

最初设置pushNotifAd.when=System.currentTimeMillis();然后 使用getMinutesDifference方法

if (getMinutesDifference(pushNotifAd.when, System.currentTimeMillis()) > 60) {
    // use notification method here
}

private long getMinutesDifference(long timeStart, long timeStop) {
    long diff = timeStop - timeStart;
    long diffMinutes = diff / (60 * 1000);
    return diffMinutes;
}