报警触发功能调用

时间:2018-07-18 12:10:32

标签: android android-studio alarmmanager android-pendingintent android-broadcastreceiver

我试图设置默认的警报时间表。它也可以精确地工作。但是我总是在调用该函数来触发警报,而没有设置时间。

这是我尝试的代码BroadcastReceiver,pendingIntent和Alarmmanager。

   public void DefaultAlert(){
    int broadcast=0;
    List<Time> times = new ArrayList<>();
    times.add(new Time(7, 00));
    times.add(new Time(9, 00));
    times.add(new Time(11, 30));
    times.add(new Time(13, 30));
    times.add(new Time(15, 00));
    times.add(new Time(17, 00));
    times.add(new Time(20, 00));
    times.add(new Time(22, 00));

    Intent intent=new Intent(MainActivity.this,MyBroadcastReceiver.class);
    AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);

    for (Time time : times){
        PendingIntent pendingIntent=PendingIntent.getBroadcast(this.getApplicationContext(),broadcast++,intent,0);
        Calendar cal_alarm=Calendar.getInstance();
        cal_alarm.set(Calendar.HOUR_OF_DAY,time.hour);
        cal_alarm.set(Calendar.MINUTE,time.minute);
        cal_alarm.set(Calendar.SECOND,00);
        Log.v(TAG,"Set Alarm For : "+time.hour+":"+time.minute);

//            mOutputText.setText(TextUtils.join("\n",));
            if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT){
                alarmManager.setExact(AlarmManager.RTC_WAKEUP,cal_alarm.getTimeInMillis(),pendingIntent);
                alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, cal_alarm.getTimeInMillis(),
                        AlarmManager.INTERVAL_DAY, pendingIntent);
            }
            if(Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT){
                alarmManager.set(AlarmManager.RTC_WAKEUP,cal_alarm.getTimeInMillis(),pendingIntent);
                alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, cal_alarm.getTimeInMillis(),
                        AlarmManager.INTERVAL_DAY, pendingIntent);
            }
            mOutputText.append(broadcast+". "+time.hour+":"+time.minute+"\n");
        }
    }

这是什么原因?并给出解决方案。

谢谢

0 个答案:

没有答案