我正在尝试创建一个 Android 警报,该警报将在另一个屏幕上完成某个操作时关闭(即用户在数学问题中键入答案)。
我如何能:
- 允许用户使用时间选择器在屏幕上设置每日闹钟
- 实施该警报
- 允许用户通过在另一个屏幕上做数学问题(或任何其他操作)来关闭警报
我尝试使用建议的解决方案遵循此示例:Unable to set alarm time in android
但它不播放铃声。我在该行下方添加了这个
Toast.makeText(BrainBuzzActivity.this, "Alarm has been set.", Toast.LENGTH_LONG).show();
Uri alert = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
if(alert == null){
// alert is null, using backup
alert = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
if(alert == null){
// alert backup is null, using 2nd backup
alert = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE);
}
}
原始主页(创建意图)
public class BrainBuzzActivity extends Activity
{
private Button alarm = null;
private AlarmManager alarmManager = null;
Calendar cal = Calendar.getInstance();
static final int DIALOG_TIME = 0;
alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarm = (Button) findViewById(R.id.alarm);
alarm.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
showDialog(DIALOG_TIME);
}
});
}
protected Dialog onCreateDialog(int id) {
Dialog dialog = null;
switch(id){
case DIALOG_TIME:
dialog = new TimePickerDialog(this, new TimePickerDialog.OnTimeSetListener() {
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
Calendar c = Calendar.getInstance();
c.setTimeInMillis(System.currentTimeMillis());
// c.clear();
//c.set(Calendar.YEAR, 2011);
//c.set(Calendar.MONTH, 6);
//c.set(Calendar.DAY_OF_MONTH, 4);
c.set(Calendar.HOUR_OF_DAY, hourOfDay);
c.set(Calendar.MINUTE, minute);
c.set(Calendar.SECOND, 0);
//c.set(Calendar.MILLISECOND, 0);
Intent intent = new Intent(BrainBuzzActivity.this,AlarmReceiver.class);
PendingIntent pi = PendingIntent.getBroadcast(BrainBuzzActivity.this, 0, intent, 0);
alarmManager.set(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), pi);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), AlarmManager.INTERVAL_FIFTEEN_MINUTES, pi);
Toast.makeText(BrainBuzzActivity.this, "Alarm has been set.", Toast.LENGTH_LONG).show();
Intent i = new Intent(BrainBuzzActivity.this, CloudCapture.class);
startActivity(i);
}
},cal.get(Calendar.HOUR_OF_DAY),cal.get(Calendar.MINUTE),false);
break;
}
return dialog;
}