0
這裏我選擇時間使用Edittext並選擇複選框來執行鬧鐘。我選擇的時間不工作,當我選擇複選框時,它開始振鈴當我選擇複選框我的鬧鐘正在運行。它不適用於選定的時間
ca = Calendar.getInstance();
mHour = ca.get(Calendar.HOUR_OF_DAY);
//timeET.getCurrentHour();
mMinute = ca.get(Calendar.MINUTE);
TimePickerDialog timePickerDialog = new TimePickerDialog(this, new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
timeET.setText(hourOfDay +" : "+minute);
}
},mHour,mMinute,false);
timePickerDialog.show();
fixTime = timeET.getText().toString();
}
}
View.OnClickListener checkboxClickListener = new View.OnClickListener() {
@Override
public void onClick(View v) {
boolean checked = ((CheckBox) v).isChecked();
if (checked){
switch (v.getId()){
case R.id.alarm:
Intent myIntent = new Intent(CountryEdit.this,AlarmReceiver.class);
//An pendingIntent specifies an action to take in the future
pendingIntent = PendingIntent.getBroadcast(CountryEdit.this,0,myIntent,0);
//set Alarm Time
alarmManager.set(AlarmManager.RTC,ca.getTimeInMillis(),pendingIntent);
break;
case R.id.remainder:
break;
}
}else{
alarmManager.cancel(pendingIntent);
}
這是我的AlarmReceiver類,我告訴系統響鈴選擇時間。
@Override
public void onReceive(Context context, Intent intent) {
//This will update the UT with message
try{
CountryEdit inst = CountryEdit.instance();
//This will sound my Alarm once if i wish to sound my alarm in loop continuously then use MediaPlayer
Uri alarmUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
if (alarmUri == null){
alarmUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
}
Ringtone ringtone = RingtoneManager.getRingtone(context,alarmUri);
ringtone.play();
}catch (Exception ex){
}
}
我不知道問題出在哪裏。有沒有人請幫助我。我是初學者。
我將ca.getTimeInMillis()更改爲fixTime,但它不起作用 –
好的,您必須有一個變量來表示您希望以毫秒爲單位設置鬧鐘的時間。 1.選擇時間 - 2.將其轉換爲毫秒。 - 讓我們說它的'myAlarmTimeInMillis' 3.調用alarmManager.set(AlarmManager.RTC,myAlarmTimeInMillis,pendingIntent); – Recomer
首先我做一個名字是myAlarmInMilly()的函數。第二我試圖將我的選定時間轉換爲長時間= Long.parseLong(new GregorianCalendar()。getTimeInMillis()+(fixTime));第三,當checkbok檢查並嘗試調用報警時顯示錯誤,alarmManager.set(AlarmManager.RTC,myAlarmInMilly(),pendingIntent); –