0
我發現阻止主頁按鈕的代碼,我確實想嘗試一下,但是當我運行它時什麼也沒有發生,即使我的佈局不顯示,並且當我離開時也出現錯誤到主屏幕(見下面的代碼)。我做錯了什麼?在Android中鎖定主頁按鈕
public class LockActivity extends Activity{
private LockscreenUtils mLockscreenUtils;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_lock);
lockHomeButton();
}
public void unLockHomeButton() {
mLockscreenUtils.unlock();
}
public void lockHomeButton() {
mLockscreenUtils.lock(LockActivity.this);
}
}
這是我阻止了home鍵:
public class LockscreenUtils {
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);
}
}
}
這些是我的錯誤:
Caused by: java.lang.NullPointerException
at com.example.amadey.pre_final_version.LockActivity.lockHomeButton(LockActivity.java:46)
at com.example.amadey.pre_final_version.LockActivity.onCreate(LockActivity.java:31)
at android.app.Activity.performCreate(Activity.java:5231)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
at android.app.ActivityThread.access$800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5001)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
我想阻止home鍵 – Steve
那麼不要在KEYCODE_HOME –
@覆蓋 公共布爾的onkeydown(INT的keyCode,KeyEvent的事件){ 返回進制值,如果((的keyCode == KeyEvent.KEYCODE_HOME)) { } –