2015-12-24 87 views
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) 

回答

2

我想你一定是想覆蓋你的home鍵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鍵,當你在這段代碼放在同一個活動,你可以不要任何東西。

檢查這隻有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; 
    } 
+0

我想阻止home鍵 – Steve

+0

那麼不要在KEYCODE_HOME –

+0

@覆蓋 公共布爾的onkeydown(INT的keyCode,KeyEvent的事件){ 返回進制值,如果((的keyCode == KeyEvent.KEYCODE_HOME)) { } –