-1

我想创建一个 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

关于如何解决这个问题的任何想法?先感谢您

4

2 回答 2

2

把这个:

this.requestWindowFeature(Window.FEATURE_NO_TITLE);

在这之前:

setContentView(R.layout.alarm);

文档中requestWindowFeature(强调添加):

启用扩展屏幕功能。这必须在 setContentView() 之前调用。只要在 setContentView() 之前,就可以根据需要多次调用。如果不调用,则没有扩展功能可用。一旦被请求,您就不能将其关闭。您不能通过 FEATURE_CUSTOM_TITLE 使用其他标题功能。

于 2013-11-04T06:21:18.287 回答
2

像这样更改顺序..

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);
于 2013-11-04T06:22:07.303 回答