Activity doesn't show in full screen

微笑、不失礼 提交于 2019-12-30 12:14:08

问题


I defined a new Activity on my project and I have some trouble with fullScreen.

I defined in the manifest file like this:

<activity android:name=".Test"
     android:launchMode="singleInstance" android:screenOrientation="portrait"
     android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
     .............
>

If I start the activity from another activity, I got the desired full screen. The problem is when I start this activity from a BroadcastReceiver - I need to open this activity inside a BroadcastReceiver something like this:

public void onReceive(Context context, Intent intent) {
     Intent test = new Intent(context, Test.class);
     test.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
     context.startActivity(test);
}

I tried like this too:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
       WindowManager.LayoutParams.FLAG_FULLSCREEN);

    setContentView(R.layout.test);
}

and no full screen if the activity starts from my BroadcastReciever.

Why I don't get full screen on this case? There is any way to request full screen after the Activity is created and visible?


回答1:


I fond the issue. There is a method I omitted to add in question text - I didn't thought it's relevant. Because I want this activity to intercept (do not react) home button press, and for this reason I override onAttachedToWindow() method like this:

public void onAttachedToWindow() {
    getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);
    super.onAttachedToWindow();
}

And here is the issue. Some times, because of this, my activity didn't get full screen. To fix this, I don't know if this is the best way, I added a delay to this code, like this:

public void onAttachedToWindow() {
    handler.sendEmptyMessageDelayed(100,100);
    super.onAttachedToWindow();
}

and the handler:

public boolean handleMessage(Message msg) {
    getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);
}

and this solved my issue. I hope this help someone!



来源:https://stackoverflow.com/questions/9369144/activity-doesnt-show-in-full-screen

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!