我想创建一个 android 应用程序来触发警报。我有一个名为 btnStart 的按钮。当用户单击按钮时,在 3 秒内,它应该转到 AlarmReceiverActivity.java 并开始播放声音。
btnStart onclick 方法看起来像
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();
}
});
和 AlarmReceiverActivity.java 看起来像这样
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());
}
为了使这个简短的问题简短,有一些我没有在上面说明的方法。当我单击按钮时,我得到了错误。它说
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
关于如何解决这个问题的任何想法?先感谢您