【问题标题】:Android Notification on specific weekday goes off directly特定工作日的 Android 通知直接关闭
【发布时间】:2013-01-04 12:10:04
【问题描述】:

我正在尝试使用以下代码在星期四和星期五发出通知: 此代码适用于星期四

Calendar updateTime = Calendar.getInstance();
updateTime.set(Calendar.HOUR_OF_DAY, 14);
updateTime.set(Calendar.MINUTE, 44);
updateTime.set(Calendar.SECOND, 0);
updateTime.set(Calendar.DAY_OF_WEEK, 5);
if(updateTime.before(new GregorianCalendar())){
    updateTime.add(GregorianCalendar.DAY_OF_MONTH, 7);
}
Intent intent = new Intent(context, Alarm4.class);
intent.putExtra(ONE_TIME, Boolean.TRUE);
PendingIntent pi = PendingIntent.getBroadcast(context, 0, intent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, updateTime.getTimeInMillis(), pi);

但是,如果时间已经超过通知时间,则此代码无法直接弹出通知。

我错过了什么?

【问题讨论】:

  • 你试过安卓内置的闹钟调度吗?
  • 不,我正在为我的所有警报使用此功能,我将编辑第一篇文章以使其更具体

标签: android eclipse


【解决方案1】:

这样做。

           if (updateTime.getTimeInMillis() < System.currentTimeMillis()

                || updateTime.before(new GregorianCalendar())) {
            updateTime.set(Calendar.HOUR_OF_DAY, hourOfDay + 24);
            updateTime.add(GregorianCalendar.DAY_OF_MONTH, 7);
        }

并创建一个类来设置警报。

        public class setAlarm {

Context context;

public setAlarm(Context c) {
    // TODO Auto-generated constructor stub
    this.context = c;
}

public void settingAlarm(Calendar calendar, int counter) {
    Intent intent = new Intent(context, AlarmReceiver.class);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(context,
            counter, intent, PendingIntent.FLAG_UPDATE_CURRENT);
    AlarmManager alarmManager = (AlarmManager) context
            .getSystemService(Context.ALARM_SERVICE);
    alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,
            calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY,
            pendingIntent);
}

}

并且警报广播接收器是 公共类 AlarmReceiver 扩展 BroadcastReceiver {

private NotificationManager manager;
private int NOTIFICATION_ID;

@Override
public void onReceive(Context context, Intent intent) {
    // TODO Auto-generated method stub
    Toast.makeText(context, "Alarm Received", Toast.LENGTH_LONG).show();
    manager = (NotificationManager) context
            .getSystemService(Context.NOTIFICATION_SERVICE);
    Notification notification = new Notification(R.drawable.ic_launcher,
            "Food Time", System.currentTimeMillis());
    Intent intent2 = new Intent(context, MainActivity.class);
    PendingIntent pendingIntent = PendingIntent.getActivity(context, 0,
            intent2, PendingIntent.FLAG_UPDATE_CURRENT);
    notification.setLatestEventInfo(context, "FoodTime",
            "Click to View your food", pendingIntent);
    notification.flags |= Notification.FLAG_AUTO_CANCEL;

    notification.sound = Uri
            .parse("android.resource://"
                    + "com.technogalax.sixsmallmealaday.alarm" + "/"
                    + R.raw.nsound);

    manager.notify(NOTIFICATION_ID, notification);

}

}

【讨论】:

    【解决方案2】:

    显然,您的代码没问题,但只完成了一半。现在,您必须创建一个 BroadcastReceiver,您必须在其中设置通知。你可以通过这个article,这篇文章会对你有所帮助。我想在这里提一下,它将为用户设置单个警报,而不是重复。对于重复,您必须使用public void setRepeating (int type, long triggerAtMillis, long intervalMillis, PendingIntent operation) 而不是public void set (int type, long triggerAtMillis, PendingIntent operation)。它仍然会定期重复。

    根据我对您特定重复类型的想法,您可以以不同的方式安排它。最初使用public void set (int type, long triggerAtMillis, PendingIntent operation) 设置第二天的闹钟(在检查第二天的闹钟之后)。这将非常简单。当警报将被系统广播并且您的BroadcastReceiver 将被调用。在onRecieve() 你的BroadcastReceiver 中,设置通知并设置第二天的闹钟(在检查第二天的闹钟后)。这样一来,整个循环过程将由您控制,您可以轻松为其设置任何逻辑。

    【讨论】:

    • 难道没有办法像我上面那样做吗,我是 Android 开发的初学者,所以我不能 100% 确定你的意思。
    猜你喜欢
    • 1970-01-01
    • 2012-04-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多