Reputation: 1528
I'm trying to create an alarm app which is supposed to ring after a given period of time. It works when the screen is switched on; I open the app and the alarm goes off after 5 second. Which is exactly what I want.
Now, I want my app to function the same way when the screen is switched off. So, if I open my app and then close the screen, I want the app to switch on the screen after 5 seconds and then ring the alarm.
I tried SCREEN_BRIGHT_WAKE_LOCK
(did put in the required permissions) but it did not work. The alarm rang for like half a second and then then switched off. The screen did not turn on at all. Also, SCREEN_BRIGHT_WAKE_LOCK
has been deprecated and the documentation suggests using FLAG_KEEP_SCREEN_ON
which is useful only if the screen is already switched on and we wan to keep it that way. Not turn it on from off state.
What will I have to do to solve the above problem? Use a service? Also, the screen is locked with number lock.
Here is my code which works only when the screen is switched on:
public class AlarmReceiverActivity extends Activity {
private MediaPlayer mMediaPlayer;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_alarm_receiver);
Button stopAlarm = (Button) findViewById(R.id.stopAlarm);
stopAlarm.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View arg0, MotionEvent arg1) {
mMediaPlayer.stop();
mMediaPlayer.release();
finish();
return false;
}
});
playSound(this, getAlarmUri());
}
private void playSound(Context context, Uri alert) {
mMediaPlayer = new MediaPlayer();
try {
mMediaPlayer.setDataSource(context, alert);
final AudioManager audioManager = (AudioManager) context
.getSystemService(Context.AUDIO_SERVICE);
if (audioManager.getStreamVolume(AudioManager.STREAM_ALARM) != 0) {
mMediaPlayer.setAudioStreamType(AudioManager.STREAM_ALARM);
try {
mMediaPlayer.prepare();
mMediaPlayer.start();
} catch (IOException e) {
System.out.println("Oops");
}
}
} catch (IOException e1) {
System.out.println("OOPS");
}
}
//Get an alarm sound. Try for an alarm. If none set, try notification,
//Otherwise, ringtone.
private Uri getAlarmUri() {
Uri alert = RingtoneManager
.getDefaultUri(RingtoneManager.TYPE_ALARM);
if (alert == null) {
alert = RingtoneManager
.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
if (alert == null) {
alert = RingtoneManager
.getDefaultUri(RingtoneManager.TYPE_RINGTONE);
}
}
return alert;
}
}
Upvotes: 1
Views: 418
Reputation: 1424
I was dealing with this issue recently. this is what worked for me:
public class YourActivity extends Activity {
...
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_view);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON |
WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD |
WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED |
WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
...
}
helpful links:
Android Activity Not Showing When Screen is Woken Up and Lock Screen Not Disabling
http://weimenglee.blogspot.co.il/2013/04/android-tip-waking-up-screen-and.html
Android Galaxy S4 -- Activity that is visible over lock screen
also do NOT use dialog theme for the activity!!!
Upvotes: 1