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

Min target must be less than 14 when WindowManager.LayoutParams.TYPE_KEYGUARD used

我试图阻止状态栏扩展,并且我使用了一些人们试图覆盖主页按钮的示例。所有解决方案都指向使用 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();
}

并使用

添加 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);