无法在启动时重新安排 - Android Java

时间:2014-11-04 04:02:26

标签: android alarmmanager reset reboot

我无法在重启手机时安排闹钟。我有Broadcast Receiver我通过代码注册。我有能力在睡眠时唤醒手机并且闹钟设置正常但是当试图使用下面的广播接收器在启动时安排手机并通过代码注册它时我遇到了问题。我无法通过AndroidManifest.xml进行注册,因此请使用下面的代码以及可能需要的任何修改来帮助我这样做。谢谢!

  private void SetTheAlarmForNotifications() {

    try {

        br = new BroadcastReceiver() {
            @Override
            public void onReceive(Context c, Intent i) {

                    //acquire the wakelock and lock the phone from sleeping
                    WakeLocker.acquire(c);

                    //RUN THE NOTIFY EVENT WHEN THE BROADCAST RECEPTION IS RECEIVED
                    NotifyUserOfAddedScheduledItem("", "");
            }
        };

        registerReceiver(br, new IntentFilter("com.kyledenney.staplelist") );

        pi = PendingIntent.getBroadcast( this, 0, new Intent("com.kyledenney.staplelist"),
                0 );
        am = (AlarmManager)(this.getSystemService( Context.ALARM_SERVICE ));

        //SETUP THE ALARM FOR THE ITEMS IF IT IS NOT SET
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(System.currentTimeMillis());
        calendar.set(Calendar.HOUR_OF_DAY, 7);

        //fake test alarm setting (one time reoccurring alarm
        //that only fires 20 seconds after it is set
        //  am.set( AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime() +
        //       2000, pi );

        //another fake test that fires the wakeup every minute as a test - but it is a recurring
        //event instead of firing just once - we would probably only want to do this if it was elapsed from
        //a  certain time
        am.setRepeating(AlarmManager.RTC_WAKEUP, SystemClock.elapsedRealtime(),
               1000 * 60 * 1, pi);

        //real alarm setting
       // am.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pi);
    }
    catch (Exception e)
    {

    }

}

0 个答案:

没有答案