0

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

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

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

我错过了什么?

4

2 回答 2

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

}

和报警广播接收器是公共类报警接收器扩展广播接收器{

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

}

}

于 2014-05-14T10:54:19.387 回答
0

显然,您的代码还可以,但只完成了一半。现在,您必须创建一个 BroadcastReceiver,您必须在其中设置通知。你可以浏览这篇文章,这篇文章会对你有所帮助。我想在这里提一下,它将为用户设置单个警报,而不是重复。对于重复,您必须使用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中,设置通知并设置第二天的闹钟(在检查第二天的闹钟后)。通过这种方式,整个重复过程将在您的控制之下,您可以轻松地为其设置任何逻辑。

于 2013-01-04T12:41:43.857 回答