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

时间:2015-12-15 22:21:16

标签: android statusbar illegalargumentexception

我正在尝试阻止状态栏扩展,我已经使用了一些人们试图覆盖主页按钮的示例。所有解决方案都指向使用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。 StackTrace:

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;
    }
}

1 个答案:

答案 0 :(得分:6)

更新onAttachedToWindow,如下所示,然后尝试

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

并使用

添加更新oncreate
 @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);