当日历时间设置为大于当前时间时,为什么没有调用Broadcastreceiver的onReceive()?

时间:2014-05-05 09:44:20

标签: android broadcastreceiver alarmmanager repeat

您好我正在设置Android应用程序中的每日闹钟,我想在每天下午6点设置闹钟。我按照http://developer.android.com/training/scheduling/alarms.html上给出的教程。

以下是我正在使用的代码: -

 public class SampleAlarmReceiver extends WakefulBroadcastReceiver {
    // The app's AlarmManager, which provides access to the system alarm services.
    private AlarmManager alarmMgr;
    // The pending intent that is triggered when the alarm fires.
    private PendingIntent alarmIntent;

    @Override
    public void onReceive(Context context, Intent intent) {   
        Intent service = new Intent(context, SampleSchedulingService.class);

        // Start the service, keeping the device awake while it is launching.
        startWakefulService(context, service);
    }

    public void setAlarm(Context context) {
        alarmMgr = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
        Intent intent = new Intent(context, SampleAlarmReceiver.class);
        alarmIntent = PendingIntent.getBroadcast(context, 0, intent, 0);

        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(System.currentTimeMillis());
        // Set the alarm's trigger time to 8:30 a.m.
        calendar.set(Calendar.HOUR_OF_DAY, 13);
        calendar.set(Calendar.MINUTE, 50);
        Log.i("Hour",""+calendar.get(Calendar.HOUR_OF_DAY));
        Log.i("Min",""+calendar.get(Calendar.MINUTE));

        // Set the alarm to fire at approximately 8:30 a.m., according to the device's
        // clock, and to repeat once a day.
        alarmMgr.setInexactRepeating(AlarmManager.RTC_WAKEUP,  
                calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, alarmIntent);

        // Enable {@code SampleBootReceiver} to automatically restart the alarm when the
        // device is rebooted.
        ComponentName receiver = new ComponentName(context, SampleBootReceiver.class);
        PackageManager pm = context.getPackageManager();

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

    public void cancelAlarm(Context context) {
        // If the alarm has been set, cancel it.
        if (alarmMgr!= null) {
            alarmMgr.cancel(alarmIntent);
        }

        // Disable {@code SampleBootReceiver} so that it doesn't automatically restart the 
        // alarm when the device is rebooted.
        ComponentName receiver = new ComponentName(context, SampleBootReceiver.class);
        PackageManager pm = context.getPackageManager();

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

在上面的代码中,当我将Hour_Of_Day设置为18时(因为我想在下午6点设置闹钟),广播接收器永远不会被接收。但是当我将Hour_Of_Day设置为小于当前的时间时(例如,如果当前Hour_Of_Day为14然后值小于14),则完全调用BroadcastReceiver。 我不知道这里到底发生了什么。有人请告诉我如何解决这个问题? 我已经宣布了一些给定的教程。 请帮帮我!!

0 个答案:

没有答案