gpt4 book ai didi

android - 使用 WindowManager.LayoutParams.TYPE_KEYGUARD 时,最小目标必须小于 14

转载 作者:塔克拉玛干 更新时间:2023-11-02 18:52:09 28 4
gpt4 key购买 nike

我试图阻止状态栏扩展,并且我使用了一些人们试图覆盖主页按钮的示例。所有解决方案都指向使用 WindowManager.LayoutParams.TYPE_KEYGUARD,我读了 here ,如果使用 TYPE_KEYGUARD,则最小目标必须小于 14。不推荐使用 TYPE_KEYGUARD,因此我也需要解决这个问题。它已从 SDK 21 中完全删除。

问题:有什么解决方法可以覆盖状态栏并将目标版本保持为 19?

我有一个像这样构建的项目:

android {
compileSdkVersion 17
buildToolsVersion "21.1.2"

defaultConfig {
applicationId "com.mycompany.ediary"
minSdkVersion 12
targetSdkVersion 12
}

buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
}
}
}

我试过建议 here无济于事。我希望 targetSdkVersion 为 19,但如果我将其更改为 19,则会出现 IllegalArgumentException。堆栈跟踪:

11-26 13:41:57.963  28667-28667/com.assistek.ediary E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.assistek.ediary, PID: 28667
java.lang.IllegalArgumentException: Window type can not be changed after the window is added.
at android.os.Parcel.readException(Parcel.java:1550)
at android.os.Parcel.readException(Parcel.java:1499)
at android.view.IWindowSession$Stub$Proxy.relayout(IWindowSession.java:903)
at android.view.ViewRootImpl.relayoutWindow(ViewRootImpl.java:5301)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1507)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1061)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5885)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:767)
at android.view.Choreographer.doCallbacks(Choreographer.java:580)
at android.view.Choreographer.doFrame(Choreographer.java:550)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:753)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)

我的onCreate:

 @Override
protected void onCreate(Bundle savedInstanceState) {

requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
...
}

我的onAttachedToWindow:

@Override
public void onAttachedToWindow() {
super.onAttachedToWindow();
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);

if (!GlobalVars.testing) {
GlobalVars.preventStatusBarExpansion(this);
}
}

以及防止状态栏扩展的代码(改编自 this post ):

public static void preventStatusBarExpansion(Context context) {
WindowManager manager = ((WindowManager) context.getApplicationContext()
.getSystemService(Context.WINDOW_SERVICE));

WindowManager.LayoutParams localLayoutParams = new WindowManager.LayoutParams();
localLayoutParams.type = WindowManager.LayoutParams.TYPE_SYSTEM_ERROR;
localLayoutParams.gravity = Gravity.TOP;
localLayoutParams.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE|

// this is to enable the notification to recieve touch events
WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL |

// Draws over status bar
WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN;

localLayoutParams.width = WindowManager.LayoutParams.MATCH_PARENT;
localLayoutParams.height = (int) (50 * context.getResources()
.getDisplayMetrics().scaledDensity);
localLayoutParams.format = PixelFormat.TRANSPARENT;

customViewGroup view = new customViewGroup(context);

manager.addView(view, localLayoutParams);
}

public static class customViewGroup extends ViewGroup {

public customViewGroup(Context context) {
super(context);
}

@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
}

@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
Log.v("customViewGroup", "**********Intercepted");
return true;
}
}

最佳答案

如下所示更新onAttachedToWindow并尝试

@Override
public void onAttachedToWindow() {
if (!GlobalVars.testing) {
GlobalVars.preventStatusBarExpansion(this);
}
super.onAttachedToWindow();
}

并在创建时添加更新

 @Override
protected void onCreate(Bundle savedInstanceState) {
requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);
...
}

14以上的API使用

this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD_DIALOG);

关于android - 使用 WindowManager.LayoutParams.TYPE_KEYGUARD 时,最小目标必须小于 14,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34300546/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com