在Android

时间:2015-12-24 02:32:49

标签: android home-button

我发现阻止主页按钮的代码,我确实想尝试一下,但是当我运行它时没有任何反应,甚至我的布局都没有显示,而且当我回家时我也有错误屏幕(见下面的代码)。我做错了什么?

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

这是我阻止主页按钮的地方:

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) 

1 个答案:

答案 0 :(得分:2)

我认为您必须尝试覆盖Android应用程序中的主页按钮。 在Android活动中尝试此代码。

    @Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if ((keyCode == KeyEvent.KEYCODE_HOME)) {
        Toast.makeText(this, "You pressed the home button!", Toast.LENGTH_LONG).show();                     
        return true;
    }
    return super.onKeyDown(keyCode, event);
}

在这里,当您处于放置此代码的同一活动时,按下主页按钮时,您可以做任何事情。

检查一下,只有home方法不会覆盖。

@Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if ((keyCode == KeyEvent.KEYCODE_HOME)) {
            Log.i("home pressed", String.valueOf(keyCode));
            return true;
        }
        if ((keyCode == KeyEvent.KEYCODE_BACK)) {
              Log.i("back pressed", String.valueOf(keyCode));
            return true;
        }
        if ((keyCode == KeyEvent.KEYCODE_MENU)) {
            Log.i("menu pressed", String.valueOf(keyCode));
            return true;
        }
        return false;
    }