问题
I am trying to prevent status bar expansion and I have used some examples where people are trying to override the home button. All solutions point to using WindowManager.LayoutParams.TYPE_KEYGUARD
, I read here, that if the TYPE_KEYGUARD is used, the min target must be less than 14. TYPE_KEYGUARD is deprecated, so I will need a work around for this, as well. It is completely removed from SDK 21.
QUESTION: What is a workaround so I can override the status bar and keep the target build to be 19?
I have a project set up for build like this:
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'
}
}
}
I've tried the advice here to no avail. I would like the targetSdkVersion to be 19, but if I change it to 19, I get the 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)
My onCreate
:
@Override
protected void onCreate(Bundle savedInstanceState) {
requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
...
}
My 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);
}
}
And the code that prevents status bar expansion (adapted from 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:
Update onAttachedToWindow as shown below and try
@Override
public void onAttachedToWindow() {
if (!GlobalVars.testing) {
GlobalVars.preventStatusBarExpansion(this);
}
super.onAttachedToWindow();
}
and add update oncreate with
@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);
...
}
For Api above 14 use
this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD_DIALOG);
来源:https://stackoverflow.com/questions/34300546/min-target-must-be-less-than-14-when-windowmanager-layoutparams-type-keyguard-us