Commit | Line | Data |
---|---|---|
e2332327 PT |
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(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 | Button stopAlarm = (Button) findViewById(R.id.stopAlarm); | |
30 | stopAlarm.setOnTouchListener(new OnTouchListener() { | |
31 | public boolean onTouch(View arg0, MotionEvent arg1) { | |
32 | mMediaPlayer.stop(); | |
33 | finish(); | |
34 | return false; | |
35 | } | |
36 | }); | |
37 | ||
38 | playSound(this, getAlarmUri()); | |
39 | } | |
40 | ||
41 | private void playSound(Context context, Uri alert) { | |
42 | mMediaPlayer = new MediaPlayer(); | |
43 | try { | |
44 | mMediaPlayer.setDataSource(context, alert); | |
45 | final AudioManager audioManager = (AudioManager) context | |
46 | .getSystemService(Context.AUDIO_SERVICE); | |
47 | if (audioManager.getStreamVolume(AudioManager.STREAM_ALARM) != 0) { | |
48 | mMediaPlayer.setAudioStreamType(AudioManager.STREAM_ALARM); | |
49 | mMediaPlayer.prepare(); | |
50 | mMediaPlayer.start(); | |
51 | } | |
52 | } catch (IOException e) { | |
53 | System.out.println("OOPS"); | |
54 | } | |
55 | } | |
56 | ||
57 | //Get an alarm sound. Try for an alarm. If none set, try notification, | |
58 | //Otherwise, ringtone. | |
59 | private Uri getAlarmUri() { | |
60 | Uri alert = RingtoneManager | |
61 | .getDefaultUri(RingtoneManager.TYPE_ALARM); | |
62 | if (alert == null) { | |
63 | alert = RingtoneManager | |
64 | .getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); | |
65 | if (alert == null) { | |
66 | alert = RingtoneManager | |
67 | .getDefaultUri(RingtoneManager.TYPE_RINGTONE); | |
68 | } | |
69 | } | |
70 | return alert; | |
71 | } | |
72 | } |