I want to create an android application to trigger the alarm. I have a button called btnStart. When the user click the button, in 3 second, it should go to AlarmReceiverActivity.java and start play the sound.
the btnStart onclick method look like
btnStart.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
int i = 3;
Intent intent = new Intent (MapPage.this, AlarmReceiverActivity.class);
PendingIntent pending =
PendingIntent.getActivity(MapPage.this, 2, intent,
PendingIntent.FLAG_CANCEL_CURRENT);
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()+(i * 1000), pending);
if (toast != null){
toast.cancel();
}
toast = Toast.makeText(getApplicationContext(), "Alarm for activity is set in : "+ i +" second", toast.LENGTH_SHORT);
toast.show();
}
});
and the AlarmReceiverActivity.java look like this
public class AlarmReceiverActivity extends Activity{
private MediaPlayer mMediaPlayer;
private PowerManager.WakeLock mWakeLock;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.alarm);
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
mWakeLock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK, "My Wake Log");
mWakeLock.acquire();
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN |
WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED |
WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON,
WindowManager.LayoutParams.FLAG_FULLSCREEN |
WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED |
WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
Button stopAlarm = (Button) findViewById(R.id.stopAlarm);
stopAlarm.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
mMediaPlayer.stop();
finish();
}
});
playSound (this, getAlarmUri());
}
to make this short question short, there are some method that I not state above. When I click the button, I got and error. It say that
java.lang.RunTimeException: Unable to start activity ComponentInfo{com.example.splashscreen/com.example.splashscreen.AlarmReceiverActivity}: android.util.AndroidRuntimeException: requestFeature() must be called before adding content
Any idea on how to solve this problem ? Thank you in advance
Chnage the order like this..
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN |
WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED |
WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON,
WindowManager.LayoutParams.FLAG_FULLSCREEN |
WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED |
WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
super.onCreate(savedInstanceState);
setContentView(R.layout.alarm);