1

我正在尝试创建一个 Android 警报,该警报将在另一个屏幕上完成某个操作时关闭(即用户在数学问题中键入答案)。

我如何能:

  1. 允许用户使用时间选择器在屏幕上设置每日闹钟
  2. 实施该警报
  3. 允许用户通过在另一个屏幕上做数学问题(或任何其他操作)来关闭警报

我尝试使用建议的解决方案遵循此示例: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;
}
4

1 回答 1

0

创建警报时,使用AlarmManager方法

setInexactRepeating(int type, long triggerAtTime, long interval, PendingIntent operation)

因此您可以计算现在和您希望它响起时之间的差异,然后指定直到每个后续警报的长度。

要取消警报,您必须创建一个相同类型的 PendingIntent,在文档中定义为

相同的操作、相同的 Intent 操作、数据、类别和组件以及相同的标志

然后使用

cancel(PendingIntent operation)

于 2011-12-06T19:00:13.643 回答