这是我的onclick()函数。这将设置目标警报
SA=(Button)findViewById(R.id.button1); SA.setOnClickListener(new OnClickListener() { @SuppressWarnings("deprecation") @Override public void onClick(View v) { showDialog(id); } }); } @Override protected Dialog onCreateDialog(int id1) { switch (id1) { case id: // set time picker as current time return new TimePickerDialog(this, timePickerListener, hour, min,false); } return null; } private TimePickerDialog.OnTimeSetListener timePickerListener = new TimePickerDialog.OnTimeSetListener() { public void onTimeSet(TimePicker view, int selectedHour, int selectedMinute) { Calendar calnow=Calendar.getInstance(); calnow.setTimeInMillis(System.currentTimeMillis()); calnow.set(Calendar.HOUR_OF_DAY,selectedHour); calnow.set(Calendar.MINUTE,selectedMinute); calnow.set(Calendar.SECOND, 0); Intent intent=new Intent(getBaseContext(),alarm.class); PendingIntent pendingintent= PendingIntent.getBroadcast(getBaseContext(),0, intent, 0); AlarmManager alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE); alarmManager.set(AlarmManager.RTC_WAKEUP, calnow.getTimeInMillis(),pendingintent); Toast.makeText(getBaseContext(), "alarm set", Toast.LENGTH_SHORT).show(); } };
public void onReceive(Context arg0, Intent arg1) { AlarmManager mgr = (AlarmManager)arg0.getSystemService(Context.ALARM_SERVICE); Toast.makeText(arg0,"Alarm Started.....", Toast.LENGTH_LONG).show();
这里的问题是,一旦我点击按钮在达到目标警报之前设置警报,我就会同时发出“警报设置”和“警报启动”。
答案 0 :(得分:0)
这里的问题是,TP的值似乎是当前时间。如果您显示更多代码,将会很有帮助。让我举个例子来设置延迟5秒的闹钟时间。请试试这个,这是一种肮脏的方式,我只是想解释一下。这就是你所做的:
calnow.set(Calendar.HOUR_OF_DAY,TP.getCurrentHour());
calnow.set(Calendar.MINUTE,TP.getCurrentMinute());
alarmManager.set(AlarmManager.RTC_WAKEUP, calnow.getTimeInMillis(),pendingintent);
延迟五秒钟,将其更改为
alarmManager.set(AlarmManager.RTC_WAKEUP, calnow.getTimeInMillis()+5000,pendingintent);
像我说的那样,这只是为了显示你必须设置为alarmManager的值。当您想要启动警报时,它必须是以毫秒为单位的时间。为此,您必须确保从您的TP中获取正确的值。所以,如果您希望我们帮助您,那么向我们展示完整的代码将是一个很好的方式