在报警管理器中使用set()和我拥有的值

时间:2016-05-02 09:29:38

标签: android alarmmanager android-pendingintent alarm

我如何使用我所拥有的两个值(小时和分钟)来设置和报警?我不幸的是没有形成这样的值,所以如果小时是> 12它不会去一个(它以milatary时间格式)。我是否正确设置了未决意图?

public void setUpAlarm(int hour,int minute) {

    long newTime;

    Intent alarm = Intent(setAlarmList.this, AlarmReciever.class);
    AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
    PendingIntent waiting = PendingIntent.getBroadcast(setAlarmList.this, 0, alarm, 0);
    alarmManager.setExact(AlarmManager.RTC_WAKEUP, hour.);

1 个答案:

答案 0 :(得分:0)

使用以下代码

AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);


                    Calendar cal = Calendar.getInstance();
                    cal.set(Calendar.HOUR_OF_DAY, h); // h->hour in 24 hrs system
                    cal.set(Calendar.MINUTE, m); // m->minutes

Intent intent = new Intent(context, AlarmReceiver.class);
alarmIntent = PendingIntent.getBroadcast(context, 0, intent, 0);

                    if (android.os.Build.VERSION.SDK_INT>16)
                    {
                        alarmManager.setExact(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), broadcast);
                    }else
                    {
                        alarmManager.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), alarmIntent);
                    }   //alarmIntent is your PendingIntent

阅读this文档