从onclick监听器

时间:2017-07-24 22:42:05

标签: java android alarmmanager android-alertdialog

我知道这个问题很多,我花了两周的时间搜索并尝试了不同的例子,但似乎没有一个适合我的情况。

我有一个报警接收器,它可以调用一个报警活动,然后显示一个带有贪睡和解除选项的对话框。我已经成功启动了警报,但是当按下其中一个对话框按钮时,我似乎无法停止它。

以下是警报活动/对话框的代码

public class AlarmActive extends Activity {

    @Override
    protected void onCreate(final Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);

        Uri currentRingtoneUri = Uri.parse(RemindMe.getRingtone());
        if (currentRingtoneUri == null) {
            currentRingtoneUri = RingtoneManager.getActualDefaultRingtoneUri(this
                    .getApplicationContext(), RingtoneManager.TYPE_RINGTONE);
        }
        final Uri finalRingtoneUri = currentRingtoneUri;
        this.getIntent();
        long alarmMsgId = getIntent().getLongExtra("alarmMsgId", -1);
        long alarmId = getIntent().getLongExtra("alarmId", -1);

        AlarmMsg alarmMsg = new AlarmMsg(alarmMsgId);
        alarmMsg.setStatus(AlarmMsg.EXPIRED);
        alarmMsg.persist(RemindMe.db);

        Alarm alarm = new Alarm(alarmId);
        alarm.load(RemindMe.db);

        if (RemindMe.isVibrate()) {
            long[] pattern = {500, 1000};
            Vibrator v = (Vibrator) this.getSystemService(Context.VIBRATOR_SERVICE);
            // Vibrate for 500 milliseconds
            v.vibrate(500);
        }
        //if (alarm.getSound()) {

        soundAlarm(true, finalRingtoneUri);
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder
                .setTitle("Alarm Expired")
                .setMessage("Alarm time has passed. Turn Off or Snooze?")
                .setCancelable(false)
                .setPositiveButton("Snooze", new DialogInterface.OnClickListener()
                {
                    public void onClick(DialogInterface dialog, int id)
                    {
                        Alarm alarm = new Alarm();
                        alarm.setName("Snooze");
                        alarm.setSound(true);
                        AlarmTime alarmTime = new AlarmTime();
                        long alarmId = 0;

                        Calendar cal = Calendar.getInstance();
                        alarm.setFromDate(DbHelper.getDateStr(cal.YEAR, cal.MONTH, cal.DAY_OF_MONTH));
                        alarmId = alarm.persist(db);
                        cal.add(Calendar.MINUTE, 1);
                        alarmTime.setAt(DbHelper.getTimeStr(cal.get(Calendar.HOUR), cal.get(Calendar.MINUTE)));
                        alarmTime.setAlarmId(alarmId);
                        alarmTime.persist(db);

                        //play current Ringtone
                        RingtoneManager ringMan = new RingtoneManager(getApplicationContext());
                        soundAlarm(false, finalRingtoneUri);
                        dialog.cancel();
                        finish();

                    }
                })
                .setNegativeButton("Turn Off", new DialogInterface.OnClickListener()
                {
                    public void onClick(DialogInterface dialog, int id)
                    {
                        soundAlarm(false, finalRingtoneUri);
                        dialog.cancel();
                        finish();
                    }
                });

        //}

        AlertDialog alert = builder.create();
        alert.show();

    }

    private void soundAlarm (boolean Play, Uri currentRingtoneUri) {
        if (Play) {
            Ringtone ringtone = RingtoneManager.getRingtone(getApplicationContext(), currentRingtoneUri);
            ringtone.play();
        }
        else {
            Ringtone ringtone = RingtoneManager.getRingtone(getApplicationContext(), currentRingtoneUri);
            ringtone.stop();
        }
    }

}

1 个答案:

答案 0 :(得分:0)

我找到了一个解决方案(另一个工作示例)希望这有助于某人。

public class AlarmActive extends Activity  {
protected static final String LOG_TAG = "MediaPlayerDemo";

MediaPlayer mp = new MediaPlayer();
MediaPlayer player;
int volume_level = 10, volume_incr = 10;
boolean done;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    playAlarm();
}

void playAlarm() {
    AlertDialog dlg = new AlertDialog.Builder(this).create();
    this.getIntent();
    //long alarmMsgId = getIntent().getLongExtra("alarmMsgId", -1);
    final long alarmId = getIntent().getLongExtra("alarmId", -1);
    dlg.setTitle("Wake up time!");
    dlg.setButton(AlertDialog.BUTTON_POSITIVE, "OFF", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            player.setOnCompletionListener(null);
            player.stop();
            player.release();
            done = true;
            finish();

        }
    });
    dlg.setButton(AlertDialog.BUTTON_NEGATIVE, "SNOOZE", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            Calendar cal = Calendar.getInstance();
            Alarm alarm = new Alarm();
            AlarmTime alarmTime = new AlarmTime();
            alarm.setFromDate(DbHelper.getDateStr(cal.YEAR, cal.MONTH, cal.DAY_OF_MONTH));
            cal.add(Calendar.MINUTE, 1);
            alarmTime.setAt(DbHelper.getTimeStr(cal.get(Calendar.HOUR), cal.get(Calendar.MINUTE)));
            alarmTime.setAlarmId(alarmId);
            alarmTime.persist(db);

            player.setOnCompletionListener(null);
            player.stop();
            player.release();
            done = true;
            finish();

        }
    });
    dlg.show();
    player = MediaPlayer.create(this, Uri.parse(RemindMe.getRingtone()));
    player.setVolume(volume_level, volume_level);
    player.start();
    player.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
        @Override
        public void onCompletion(MediaPlayer arg0) {
            volume_level += volume_incr;
            player.setVolume(volume_level, volume_level);
/*try {
 player.prepare();
} catch (IOException e) {
 // Should be a CANTHAPPEN since was previously prepared!
 Log.i(LOG_TAG, "Unexpected IOException " + e);
}*/
            player.start();
        }
    });
}

}