Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/338.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在Android程序中禁用硬件HomeKey和BackKey以及最近的Butoom_Java_Android_Android Studio - Fatal编程技术网

Java 如何在Android程序中禁用硬件HomeKey和BackKey以及最近的Butoom

Java 如何在Android程序中禁用硬件HomeKey和BackKey以及最近的Butoom,java,android,android-studio,Java,Android,Android Studio,我正在设置一个新的应用程序,希望在我的应用程序中禁用硬件密钥,如home、back和recent密钥。我在stackoverflow中找到了一些代码,但都不起作用。 是否可以禁用硬件密钥 public class MainActivity extends AppCompatActivity { protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);

我正在设置一个新的应用程序,希望在我的应用程序中禁用硬件密钥,如home、back和recent密钥。我在stackoverflow中找到了一些代码,但都不起作用。 是否可以禁用硬件密钥

public class MainActivity extends AppCompatActivity {


 protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);

        setContentView(R.layout.activity_main);
 }



public void onAttachedToWindow() {

          this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD_DIALOG);
        KeyguardManager keyguardManager = (KeyguardManager) getSystemService(KEYGUARD_SERVICE);
        KeyguardManager.KeyguardLock lock = keyguardManager.newKeyguardLock(KEYGUARD_SERVICE);
        lock.disableKeyguard();
    }

}
在反压状态下不做任何事情

将此添加到清单中

在你的主要活动中-

@Override
public void onBackPressed() {
  // super.onBackPressed(); commented this line in order to disable back press
  //Write your code here
  Toast.makeText(getApplicationContext(), "Back press disabled!", Toast.LENGTH_SHORT).show();
} 

您不能阻止“最近”和“主页”,但若用户单击“主页”,则可以重新启动“活动”

下面是一个例子

家庭守望者班

OnHomePressedListener接口

在你的主要活动中

HomeWatcher mHomeWatcher = new HomeWatcher(this);
        mHomeWatcher.setOnHomePressedListener(new OnHomePressedListener() {
            @Override
            public void onHomePressed() {
                Log.d("Pressed", "Home Button Pressed");
            }

            @Override
            public void onHomeLongPressed() {
                Intent intent = new Intent(Intent.ACTION_MAIN);
                intent.addCategory(Intent.CATEGORY_HOME);
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(intent);
                Log.d("LongPressed", "Home Long Button Pressed");
            }
        });
        mHomeWatcher.startWatch();

可能是你不能复制的。如果您只想在三星设备中屏蔽这些按键,您可以通过SamsungKnox屏蔽为什么?为什么要取消基本功能?你为什么要剥夺最终用户对自己设备的控制权?其他人呢?在家和最近?其他人呢?家和最近?
@Override
protected void onPause() {
    super.onPause();

    ActivityManager activityManager = (ActivityManager) getApplicationContext()
            .getSystemService(Context.ACTIVITY_SERVICE);

    activityManager.moveTaskToFront(getTaskId(), 0);
 }
@Override
public void onBackPressed() {
  // super.onBackPressed(); commented this line in order to disable back press
  //Write your code here
  Toast.makeText(getApplicationContext(), "Back press disabled!", Toast.LENGTH_SHORT).show();
} 
public class HomeWatcher {

    static final String TAG = "hg";
    private Context mContext;
    private IntentFilter mFilter;
    private OnHomePressedListener mListener;
    private InnerRecevier mRecevier;

    public HomeWatcher(Context context) {
        mContext = context;
        mFilter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
    }

    public void setOnHomePressedListener(OnHomePressedListener listener) {
        mListener = listener;
        mRecevier = new InnerRecevier();
    }

    public void startWatch() {
        if (mRecevier != null) {
            mContext.registerReceiver(mRecevier, mFilter);
        }
    }

    public void stopWatch() {
        if (mRecevier != null) {
            mContext.unregisterReceiver(mRecevier);
        }
    }

    class InnerRecevier extends BroadcastReceiver {
        final String SYSTEM_DIALOG_REASON_KEY = "reason";
        final String SYSTEM_DIALOG_REASON_GLOBAL_ACTIONS = "globalactions";
        final String SYSTEM_DIALOG_REASON_RECENT_APPS = "recentapps";
        final String SYSTEM_DIALOG_REASON_HOME_KEY = "homekey";
        final String SYSTEM_DIALOG_REASON_LONG_PRESS = "assist";
        final String SYSTEM_DIALOG_REASON_VOICE_INTERACTION = "voiceinteraction";

        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if (action.equals(Intent.ACTION_CLOSE_SYSTEM_DIALOGS)) {
                String reason = intent.getStringExtra(SYSTEM_DIALOG_REASON_KEY);
                if (reason != null) {
                    Log.e(TAG, "action:" + action + ",reason:" + reason);
                    if (mListener != null) {
                        if (reason.equals(SYSTEM_DIALOG_REASON_HOME_KEY)) {
                            mListener.onHomePressed();
                        } else if (reason.equals(SYSTEM_DIALOG_REASON_RECENT_APPS)) {
                            mListener.onHomeLongPressed();
                        } else if (reason.equals(SYSTEM_DIALOG_REASON_LONG_PRESS)) {
                            mListener.onHomeLongPressed();
                        } else if (reason.equals(SYSTEM_DIALOG_REASON_VOICE_INTERACTION)) {
                            mListener.onHomeLongPressed();
                        }
                    }
                }
            }
        }
    }
public interface OnHomePressedListener {

    void onHomePressed();

    void onHomeLongPressed();
}
HomeWatcher mHomeWatcher = new HomeWatcher(this);
        mHomeWatcher.setOnHomePressedListener(new OnHomePressedListener() {
            @Override
            public void onHomePressed() {
                Log.d("Pressed", "Home Button Pressed");
            }

            @Override
            public void onHomeLongPressed() {
                Intent intent = new Intent(Intent.ACTION_MAIN);
                intent.addCategory(Intent.CATEGORY_HOME);
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(intent);
                Log.d("LongPressed", "Home Long Button Pressed");
            }
        });
        mHomeWatcher.startWatch();