如何设置保持屏幕打开和全屏标志



全屏在我的应用程序中,我编写了以下代码:

  public void onCreate(Bundle savedInstanceState) {
         requestWindowFeature(Window.FEATURE_NO_TITLE);
         setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
           getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                    WindowManager.LayoutParams.FLAG_FULLSCREEN); 
               super.onCreate(savedInstanceState); 
        setContentView(R.layout.main);
                       .
                       .
                       .

并在我的应用程序中保持屏幕打开,我在布局中编写了以下代码:

android:keepScreenOn="true" 

虽然并非总是如此,但有时会像这样错误我:

java.lang.IllegalArgumentException: View not attached to window manager
at android.view.WindowManagerImpl.findViewLocked(WindowManagerImpl.java:381)
at android.view.WindowManagerImpl.removeView(WindowManagerImpl.java:226)
at android.view.Window$LocalWindowManager.removeView(Window.java:432)
at android.app.Dialog.dismissDialog(Dialog.java:278)
at android.app.Dialog.access$000(Dialog.java:71)
at android.app.Dialog$1.run(Dialog.java:111)
at android.app.Dialog.dismiss(Dialog.java:268)
at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:166)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:3691)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:847)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:605)
at dalvik.system.NativeStart.main(Native Method)

我能做什么?我写的是真正的代码?谢谢。。。

super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.main);

super.onCreate 必须首先调用。

您可以通过编程方式执行此操作:

public class ActivityName extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // remove title
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.main);
    }
}

或者你可以通过你的AndroidManifest.xml文件来完成:

<activity android:name=".ActivityName"
    android:label="@string/app_name"
    android:theme="@android:style/Theme.NoTitleBar.Fullscreen">
</activity>

WindowManager.LayoutParams.FLAG_FULLSCREEN 在 API>= 30 (R) 上折旧建议使用此方法进行检查。

@Suppress("DEPRECATION")
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
   window.insetsController?.hide(WindowInsets.Type.statusBars())
} else {
   window.setFlags(
      WindowManager.LayoutParams.FLAG_FULLSCREEN,
      WindowManager.LayoutParams.FLAG_FULLSCREEN
     )
}

在 AndroidMenifest.xml 文件中添加活动

 android:name=".ActivityName"
        android:configChanges="orientation|keyboardHidden"
        android:keepScreenOn="true"
        android:label="@string/app_name"
        android:theme="@style/customTheme" >

并删除以下内容(如果在设置内容视图之前存在)

boolean customTitleSupported = requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);

要启用全屏:

getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
        WindowManager.LayoutParams.FLAG_FULLSCREEN);

要启用屏幕:

getWindow().addFlags( WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON | 
                        WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED | WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);

相关内容

  • 没有找到相关文章

最新更新