报警管理器使用时钟

时间:2015-12-30 08:59:00

标签: android alarmmanager

class MyHandler extends Handler {

        @Override
        public void handleMessage (Message msg){
            Bundle bundle = msg.getData();
            timeHour = bundle.getInt(MyConstants.HOUR);
            timeMinute = bundle.getInt(MyConstants.MINUTE);
            textView1.setText(timeHour + ":" + timeMinute);
    *        setAlarm();
        }
    }
    @TargetApi(Build.VERSION_CODES.KITKAT)
    private void setAlarm(){
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.HOUR_OF_DAY, timeHour);
        calendar.set(Calendar.MINUTE, timeMinute);
    *    alarmManager.setExact(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
    }

得到错误我将在行

上制作*
   java.lang.NoSuchMethodError: android.app.AlarmManager.setExact 
at jawad.alm.MainActivity.setAlarm(MainActivity.java:87)         
 at jawad.alm.MainActivity.access$400(MainActivity.java:24)      
  at jawad.alm.MainActivity$MyHandler.handleMessage(MainActivity.java:79) 
at android.os.Handler.dispatchMessage(Handler.java:99)

帮帮我,谢谢

1 个答案:

答案 0 :(得分:1)

试试这个:

   if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT) {
       // only for kitkat and newer versions
       alarmManager.setExact(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
    } else {
       alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
    }