无法禁用主页按钮
Unable to disable Home Button
我正在尝试禁用主页按钮,但收到一个我没有的错误 understand.I 已经尝试了很多代码来禁用主页按钮,但没有成功。此代码似乎更好,但此错误使我的应用程序崩溃。当我的应用程序打开并转到 InputPassword Activity 时,它崩溃了。我的homeLockKey代码如下:
public class HomeKeyLocker {
private OverlayDialog mOverlayDialog;
public void lock(Activity activity) {
if (mOverlayDialog == null) {
mOverlayDialog = new OverlayDialog(activity);
mOverlayDialog.show();
}
}
public void unlock() {
if (mOverlayDialog != null) {
mOverlayDialog.dismiss();
mOverlayDialog = null;
}
}
private static class OverlayDialog extends AlertDialog {
public OverlayDialog(Activity activity) {
super(activity, R.style.OverlayDialog);
WindowManager.LayoutParams params = getWindow().getAttributes();
params.type = TYPE_SYSTEM_ERROR;
params.dimAmount = 0.0F; // transparent
params.width = 0;
params.height = 0;
params.gravity = Gravity.BOTTOM;
getWindow().setAttributes(params);
getWindow().setFlags(FLAG_SHOW_WHEN_LOCKED | FLAG_NOT_TOUCH_MODAL, 0xffffff);
setOwnerActivity(activity);
setCancelable(false);
}
public final boolean dispatchTouchEvent(MotionEvent motionevent) {
return true;
}
protected final void onCreate(Bundle bundle) {
super.onCreate(bundle);
FrameLayout framelayout = new FrameLayout(getContext());
framelayout.setBackgroundColor(0);
setContentView(framelayout);
}
}
我的 activity 是:
public class InputPassword extends AppCompatActivity {
HomeKeyLocker homeKeyLocker;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_input_password);
homeKeyLocker = new HomeKeyLocker();
homeKeyLocker.lock(this);
}
}
我的 logcat 是:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.rutabaazmat.lockpattern/com.example.rutabaazmat.lockpattern.InputPassword}: android.view.WindowManager$BadTokenException: Unable to add window android.view.ViewRootImpl$W@1abfe808 -- permission denied for this window type
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2521)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2595)
at android.app.ActivityThread.access0(ActivityThread.java:178)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1470)
at android.os.Handler.dispatchMessage(Handler.java:111)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5631)
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:959)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:754)
Caused by: android.view.WindowManager$BadTokenException: Unable to add window android.view.ViewRootImpl$W@1abfe808 -- permission denied for this window type
at android.view.ViewRootImpl.setView(ViewRootImpl.java:704)
at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:289)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:85)
at android.app.Dialog.show(Dialog.java:311)
at com.example.rutabaazmat.lockpattern.HomeKeyLocker.lock(HomeKeyLocker.java:27)
at com.example.rutabaazmat.lockpattern.InputPassword.onCreate(InputPassword.java:39)
at android.app.Activity.performCreate(Activity.java:6092)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1112)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2468)
提前感谢您的帮助:)。
尝试在 AndroidManifest 中使用此权限。
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
我正在尝试禁用主页按钮,但收到一个我没有的错误 understand.I 已经尝试了很多代码来禁用主页按钮,但没有成功。此代码似乎更好,但此错误使我的应用程序崩溃。当我的应用程序打开并转到 InputPassword Activity 时,它崩溃了。我的homeLockKey代码如下:
public class HomeKeyLocker {
private OverlayDialog mOverlayDialog;
public void lock(Activity activity) {
if (mOverlayDialog == null) {
mOverlayDialog = new OverlayDialog(activity);
mOverlayDialog.show();
}
}
public void unlock() {
if (mOverlayDialog != null) {
mOverlayDialog.dismiss();
mOverlayDialog = null;
}
}
private static class OverlayDialog extends AlertDialog {
public OverlayDialog(Activity activity) {
super(activity, R.style.OverlayDialog);
WindowManager.LayoutParams params = getWindow().getAttributes();
params.type = TYPE_SYSTEM_ERROR;
params.dimAmount = 0.0F; // transparent
params.width = 0;
params.height = 0;
params.gravity = Gravity.BOTTOM;
getWindow().setAttributes(params);
getWindow().setFlags(FLAG_SHOW_WHEN_LOCKED | FLAG_NOT_TOUCH_MODAL, 0xffffff);
setOwnerActivity(activity);
setCancelable(false);
}
public final boolean dispatchTouchEvent(MotionEvent motionevent) {
return true;
}
protected final void onCreate(Bundle bundle) {
super.onCreate(bundle);
FrameLayout framelayout = new FrameLayout(getContext());
framelayout.setBackgroundColor(0);
setContentView(framelayout);
}
}
我的 activity 是:
public class InputPassword extends AppCompatActivity {
HomeKeyLocker homeKeyLocker;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_input_password);
homeKeyLocker = new HomeKeyLocker();
homeKeyLocker.lock(this);
}
}
我的 logcat 是:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.rutabaazmat.lockpattern/com.example.rutabaazmat.lockpattern.InputPassword}: android.view.WindowManager$BadTokenException: Unable to add window android.view.ViewRootImpl$W@1abfe808 -- permission denied for this window type
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2521)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2595)
at android.app.ActivityThread.access0(ActivityThread.java:178)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1470)
at android.os.Handler.dispatchMessage(Handler.java:111)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5631)
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:959)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:754)
Caused by: android.view.WindowManager$BadTokenException: Unable to add window android.view.ViewRootImpl$W@1abfe808 -- permission denied for this window type
at android.view.ViewRootImpl.setView(ViewRootImpl.java:704)
at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:289)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:85)
at android.app.Dialog.show(Dialog.java:311)
at com.example.rutabaazmat.lockpattern.HomeKeyLocker.lock(HomeKeyLocker.java:27)
at com.example.rutabaazmat.lockpattern.InputPassword.onCreate(InputPassword.java:39)
at android.app.Activity.performCreate(Activity.java:6092)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1112)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2468)
提前感谢您的帮助:)。
尝试在 AndroidManifest 中使用此权限。
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>