private void startAlarm() { if (alarm.getAlarmTonePath() != "") { mediaPlayer = new MediaPlayer(); if (alarm.getVibrate()) { vibrator = (Vibrator) getSystemService(VIBRATOR_SERVICE); long[] pattern = {1000, 200, 200, 200}; vibrator.vibrate(pattern, 0); } try { mediaPlayer.setVolume(1.0f, 1.0f); mediaPlayer.setDataSource(this, Uri.parse(alarm.getAlarmTonePath())); mediaPlayer.setAudioStreamType(AudioManager.STREAM_ALARM); mediaPlayer.setLooping(true); mediaPlayer.prepare(); mediaPlayer.start(); } catch (Exception e) { mediaPlayer.release(); alarmActive = false; } } }
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); final Window window = getWindow(); window.addFlags( WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED | WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD); window.addFlags( WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON | WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON); setContentView(R.layout.alarm_alert); Bundle bundle = this.getIntent().getExtras(); alarm = (Alarm) bundle.getSerializable("alarm"); this.setTitle(alarm.getAlarmName()); switch (alarm.getDifficulty()) { case EASY: mathProblem = new MathProblem(3); break; case MEDIUM: mathProblem = new MathProblem(4); break; case HARD: mathProblem = new MathProblem(5); break; } answerString = String.valueOf(mathProblem.getAnswer()); if (answerString.endsWith(".0")) { answerString = answerString.substring(0, answerString.length() - 2); } problemView = (TextView) findViewById(R.id.textView1); problemView.setText(mathProblem.toString()); answerView = (TextView) findViewById(R.id.textView2); answerView.setText("= ?"); ((Button) findViewById(R.id.Button0)).setOnClickListener(this); ((Button) findViewById(R.id.Button1)).setOnClickListener(this); ((Button) findViewById(R.id.Button2)).setOnClickListener(this); ((Button) findViewById(R.id.Button3)).setOnClickListener(this); ((Button) findViewById(R.id.Button4)).setOnClickListener(this); ((Button) findViewById(R.id.Button5)).setOnClickListener(this); ((Button) findViewById(R.id.Button6)).setOnClickListener(this); ((Button) findViewById(R.id.Button7)).setOnClickListener(this); ((Button) findViewById(R.id.Button8)).setOnClickListener(this); ((Button) findViewById(R.id.Button9)).setOnClickListener(this); ((Button) findViewById(R.id.Button_clear)).setOnClickListener(this); ((Button) findViewById(R.id.Button_decimal)).setOnClickListener(this); ((Button) findViewById(R.id.Button_minus)).setOnClickListener(this); TelephonyManager telephonyManager = (TelephonyManager) this.getSystemService(Context.TELEPHONY_SERVICE); PhoneStateListener phoneStateListener = new PhoneStateListener() { @Override public void onCallStateChanged(int state, String incomingNumber) { switch (state) { case TelephonyManager.CALL_STATE_RINGING: Log.d(getClass().getSimpleName(), "Incoming call: " + incomingNumber); try { mediaPlayer.pause(); } catch (IllegalStateException e) { } break; case TelephonyManager.CALL_STATE_IDLE: Log.d(getClass().getSimpleName(), "Call State Idle"); try { mediaPlayer.start(); } catch (IllegalStateException e) { } break; } super.onCallStateChanged(state, incomingNumber); } }; telephonyManager.listen(phoneStateListener, PhoneStateListener.LISTEN_CALL_STATE); // Toast.makeText(this, answerString, Toast.LENGTH_LONG).show(); startAlarm(); }