有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

基于日期和时间的java调用AlarmManager

如果报警管理器符合条件,我在尝试将其设置为在特定时间运行时遇到了一些问题。对于这部分代码,我正在检查事件日期是否比当前日期早一天:

SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
String currentDate = dateFormat.format(new Date());
for (int count = 0; count < _eventlist.size(); count++) {

        Date alarmDate = null;
        try {
            alarmDate = dateFormat.parse(_eventlist.get(count)
                    .getEventDate());
        } catch (ParseException e) {
            e.printStackTrace();
        }
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(alarmDate);
        calendar.add(Calendar.DATE, -1);
        Date alarmBeforeOneDay = calendar.getTime();
        Log.i("Alarm", dateFormat.format(alarmBeforeOneDay));
        calendar.add(Calendar.DATE, 1);

        if (dateFormat.format(alarmBeforeOneDay).equals(currentDate)) {
            if (!AlarmInitialized(context)) {
                scheduleAlarms(context);
            }
        }
    }

如果满足条件,它将执行ScheduleAllarms()以发送推送通知:

public static void scheduleAlarms(Context context) {
/*  Calendar calendar = Calendar.getInstance();
    long firstRunTime = calendar.getTimeInMillis();*/
    Log.i("Schedule","Alarm");
    Calendar cur_cal = new GregorianCalendar();
    cur_cal.setTimeInMillis(System.currentTimeMillis());//set the current time and date for this calendar

    Calendar cal = new GregorianCalendar();
    cal.add(Calendar.DAY_OF_YEAR, cur_cal.get(Calendar.DAY_OF_YEAR));
    cal.set(Calendar.HOUR_OF_DAY, 00);
    cal.set(Calendar.MINUTE, 30);
    cal.set(Calendar.SECOND, cur_cal.get(Calendar.SECOND));
    cal.set(Calendar.MILLISECOND, cur_cal.get(Calendar.MILLISECOND));
    cal.set(Calendar.DATE, cur_cal.get(Calendar.DATE));
    cal.set(Calendar.MONTH, cur_cal.get(Calendar.MONTH));

    AlarmManager mgr = (AlarmManager) context
            .getSystemService(Context.ALARM_SERVICE);
    Intent notificationIntent = new Intent(context, Alarm.class);
    notificationCount = notificationCount + 1;

    notificationIntent.putExtra("NotifyCount", notificationCount);
    PendingIntent pi = PendingIntent.getBroadcast(context, 1,
            notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    mgr.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), pi);

    ComponentName receiver = new ComponentName(context, BootReceiver.class);
    PackageManager pm = context.getPackageManager();

    pm.setComponentEnabledSetting(receiver,
            PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
            PackageManager.DONT_KILL_APP);
}

引导接收器。课程:

public void onReceive(Context context, Intent i) {
    if (i.getAction().equals(Intent.ACTION_BOOT_COMPLETED)) {
        ProfileUpcomingEvent.scheduleAlarms(context);
    }
}

警报。课程:

public void onReceive(Context context, Intent intent) {
        mNotificationManager = (NotificationManager) context
                .getSystemService(Context.NOTIFICATION_SERVICE);
        PendingIntent contentIntent = PendingIntent.getActivity(context, 1,
                new Intent(), 0);
        notification = new Notification(R.drawable.ic_launcher,
                "Notification", System.currentTimeMillis());
        notification.setLatestEventInfo(context, "Event Reminder",
                "Registered event at WHERE is tomorrow!",
                contentIntent);
        mNotificationManager.notify(
                Integer.parseInt(intent.getExtras().get("NotifyCount")
                        .toString()), notification);
        ProfileUpcomingEvent.updateAlarmLastRun(context);
    }

AndroidManifest。xml:

<uses-permission 安卓:name="安卓.permission.RECEIVE_BOOT_COMPLETED" />
<receiver 安卓:name=".Alarm" >
    </receiver>
    <receiver
        安卓:name=".BootReceiver"
        安卓:enabled="true"
        安卓:exported="false"
        安卓:permission="安卓.permission.RECEIVE_BOOT_COMPLETED" >
        <intent-filter>
            <action 安卓:name="安卓.intent.action.BOOT_COMPLETED" >
            </action>
        </intent-filter>
    </receiver>

所以在这个方法中,我尝试将它设置为在00:30运行,如果它满足条件。然而,假设我将活动日期设置为2014年11月26日。它应该在2014年11月25日00:30之前向我发送推送通知,这比事件日期早一天,但它没有。我想知道为什么它不会运行,因为我已经使用日志检查了日期格式,并且它是正确的

提前谢谢


共 (0) 个答案