0

我的应用程序旨在在特定时间启动服务。

我使用timePicker来接收时间

 time.setOnTimeChangedListener(new TimePicker.OnTimeChangedListener() {

        public void onTimeChanged(TimePicker view, int hourOfDay, int min) {
            hour = hourOfDay;
            minute = min;
        }
    });

您可以按一个按钮来激活警报管理器。Intent 应该在这个时候开始。

public void onClick(View v) {
            cal = Calendar.getInstance();
            int _hour = hour;
            int tmpHour = cal.get(Calendar.HOUR_OF_DAY);
            int tmpMinute = cal.get(Calendar.MINUTE);
            // Alarm is today?
            if (hour > tmpHour || (tmpHour == hour && tmpMinute < minute)){

                tmpHour *= 60;
                _hour *= 60;
            }
            **// Add 24Hours to get difference.
            else {

                tmpHour *= 60;
                _hour *= (_hour + 24) * 60;
            }**
            int minutesToAdd = (_hour + minute) - (tmpHour + tmpMinute);
            Log.d("AlarmExample", "Need to add " + minutesToAdd + " minutes");
            cal.add(Calendar.MINUTE, minutesToAdd);


            cal.set(Calendar.SECOND, 0);
            cal.set(Calendar.MILLISECOND, 0);

...

//alarmmanager

            AlarmManager amanag = (AlarmManager) getSystemService(ALARM_SERVICE);
            amanag .set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), sender);

我在这里有一个问题:当您在上午 12:00 和上午 12:59 之间选择时间时, _hour *= (_hour + 24) * 60; 使警报在您触摸按钮时立即启动。

我试图将其更改为 _hour = (_hour + 24) * 60;* 但它没有用。

你知道如何使它可靠吗?

真诚的,沃尔芬

4

1 回答 1

0
AlarmManager alarmMgr = (AlarmManager)getSystemService(Context.ALARM_SERVICE);     

    Intent intent = new Intent(this, OnAlarmReceiver.class);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 1, intent, 0);               
    Calendar timeOff = Calendar.getInstance();  
//time 12.19AM
    timeOff.set(Calendar.HOUR_OF_DAY, 0);
    timeOff.set(Calendar.MINUTE, 19);
    timeOff.set(Calendar.SECOND, 0);      


 alarmMgr.set(AlarmManager.RTC, timeOff.getTimeInMillis(), pendingIntent);

我使用了实现 Broadcast reciver 的 onAlermReciver 将其设置为 intent ,然后警报关闭了对 OnAlermReciver 的调用,它对我有用,
你可以使用

 alarmMgr.set(AlarmManager.RTC, timeOff.getTimeInMillis(), sender);
于 2012-05-30T08:50:19.627 回答