使用 WindowManager.LayoutParams.TYPE_KEYGUARD 时,最小目标必须小于 14
Posted
技术标签:
【中文标题】使用 WindowManager.LayoutParams.TYPE_KEYGUARD 时,最小目标必须小于 14【英文标题】:Min target must be less than 14 when WindowManager.LayoutParams.TYPE_KEYGUARD used 【发布时间】:2016-03-21 21:53:12 【问题描述】:我试图阻止状态栏扩展,并且我使用了一些人们试图覆盖主页按钮的示例。所有解决方案都指向使用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;
【问题讨论】:
试试这个帖子:***.com/questions/19920052/… 【参考方案1】:如下图更新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);
【讨论】:
感谢您的回答。我会试试的! 仍然得到 IllegalArgument 异常。它工作了一段时间,然后突然停止工作。 你知道崩溃发生在哪里吗?是在 onCreate() 方法上吗? 它在对话框窗口中崩溃。我从那里删除了那个 Type_Keyguard 并且一切正常。以上是关于使用 WindowManager.LayoutParams.TYPE_KEYGUARD 时,最小目标必须小于 14的主要内容,如果未能解决你的问题,请参考以下文章