這是我的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();
這裏的問題是,我得到兩個吐司「報警設置」和「報警開始」只要我按一下按鈕,設置在達到目標警報前發出警報
當你在這裏放置一個斷點時,'alarmManager.set(AlarmManager.RTC_WAKEUP,calnow.getTimeInMillis(),pendingintent); ','calnow.getTimeInMillis()'的價值是什麼? – codeMagic
什麼是你從哪裏獲得時間的TP? – Opiatefuchs
你如何設定TP的價值?你有沒有檢查過它絕對是未來的一個時間?你看到的結果意味着這是過去或現在的時間。 – user1676075