Activity doesn't show in full screen

2019-02-20 05:45发布

问题:

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!