【问题标题】:how to disable soft home key programatically in android?如何在android中以编程方式禁用软主页键?
【发布时间】:2016-06-08 11:34:51
【问题描述】:

有没有办法以编程方式禁用软 Home 键?

public boolean isSoftKeyAvail(Context context) {
    final boolean[] isSoftkey = {false};
    final View activityRootView = ((Activity) context).getWindow().getDecorView().findViewById(android.R.id.content);
    activityRootView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {
            int rootViewHeight = activityRootView.getRootView().getHeight();
            int viewHeight = activityRootView.getHeight();
            int heightDiff = rootViewHeight - viewHeight;
            if (heightDiff > 100) { // 99% of the time the height diff will be due to a keyboard.
                isSoftkey[0] = true;
            }
        }
    });
    return isSoftkey[0];
}

public int getStatusBarHeight(){
    int result=0;
    int resourceId= mContext.getResources().getIdentifier("status_bar_height", "dimen", "android");
    if(resourceId >0)
        result = mContext.getResources().getDimensionPixelSize(resourceId);

    return result;
}

【问题讨论】:

    标签: java android android-view android-homebutton


    【解决方案1】:
    @Override
    public void onAttachedToWindow() {
        this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD_DIALOG);
        KeyguardManager keyguardManager = (KeyguardManager) getSystemService(KEYGUARD_SERVICE);
        KeyguardLock lock = keyguardManager.newKeyguardLock(KEYGUARD_SERVICE);
        lock.disableKeyguard();
    }
    

    【讨论】:

      【解决方案2】:

      使用这个:

      @Override
      public void onAttachedToWindow() {
          this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);
          super.onAttachedToWindow();
      }
      

      或者将清单设置为:

      <action android:name="android.intent.action.MAIN" />              
      <category android:name="android.intent.category.HOME" />                 
      <category android:name="android.intent.category.DEFAULT" />               
      <category android:name="android.intent.category.MONKEY"/>
      

      【讨论】:

      • 试试这个:@Override public void onAttachedToWindow() { this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD_DIALOG); KeyguardManager keyguardManager = (KeyguardManager) getSystemService(KEYGUARD_SERVICE); KeyguardLock lock = keyguardManager.newKeyguardLock(KEYGUARD_SERVICE); lock.disableKeyguard(); }
      【解决方案3】:

      试试这个,

      InputMethodManager inputManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
      inputManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-10-04
        相关资源
        最近更新 更多