| 1 | package ro.ieval.unical; |
| 2 | |
| 3 | import java.io.IOException; |
| 4 | |
| 5 | import android.app.Activity; |
| 6 | import android.content.Context; |
| 7 | import android.media.AudioManager; |
| 8 | import android.media.MediaPlayer; |
| 9 | import android.media.RingtoneManager; |
| 10 | import android.net.Uri; |
| 11 | import android.os.Bundle; |
| 12 | import android.view.MotionEvent; |
| 13 | import android.view.View; |
| 14 | import android.view.View.OnTouchListener; |
| 15 | import android.view.Window; |
| 16 | import android.view.WindowManager; |
| 17 | import android.widget.Button; |
| 18 | |
| 19 | public class AlarmReceiverActivity extends Activity { |
| 20 | private MediaPlayer mMediaPlayer; |
| 21 | |
| 22 | @Override |
| 23 | public void onCreate(final Bundle savedInstanceState) { |
| 24 | super.onCreate(savedInstanceState); |
| 25 | this.requestWindowFeature(Window.FEATURE_NO_TITLE); |
| 26 | this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); |
| 27 | setContentView(R.layout.alarm); |
| 28 | |
| 29 | final Button stopAlarm = (Button) findViewById(R.id.stopAlarm); |
| 30 | stopAlarm.setOnTouchListener(new OnTouchListener() { |
| 31 | @Override |
| 32 | public boolean onTouch(final View arg0, final MotionEvent arg1) { |
| 33 | mMediaPlayer.stop(); |
| 34 | finish(); |
| 35 | return false; |
| 36 | } |
| 37 | }); |
| 38 | |
| 39 | playSound(this, getAlarmUri()); |
| 40 | } |
| 41 | |
| 42 | private void playSound(final Context context, final Uri alert) { |
| 43 | mMediaPlayer = new MediaPlayer(); |
| 44 | try { |
| 45 | mMediaPlayer.setDataSource(context, alert); |
| 46 | final AudioManager audioManager = (AudioManager) context |
| 47 | .getSystemService(Context.AUDIO_SERVICE); |
| 48 | if (audioManager.getStreamVolume(AudioManager.STREAM_ALARM) != 0) { |
| 49 | mMediaPlayer.setAudioStreamType(AudioManager.STREAM_ALARM); |
| 50 | mMediaPlayer.prepare(); |
| 51 | mMediaPlayer.start(); |
| 52 | } |
| 53 | } catch (IOException e) { |
| 54 | e.printStackTrace(); |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | private static Uri getAlarmUri() { |
| 59 | Uri alert = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM); |
| 60 | if (alert == null) |
| 61 | alert = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); |
| 62 | if (alert == null) |
| 63 | alert = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE); |
| 64 | |
| 65 | return alert; |
| 66 | } |
| 67 | } |