【问题标题】:Android notification set to 1 hourAndroid 通知设置为 1 小时
【发布时间】:2015-02-25 09:42:57
【问题描述】:

我正在尝试显示推送通知以提醒用户检查他们选择的内容(洗碗、上班等)。对于初学者,我想将其设置为当前时间 + 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);

【问题讨论】:

    标签: android time timer notifications


    【解决方案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;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-12-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多