为了账号安全,请及时绑定邮箱和手机立即绑定

像“主页”按钮一样覆盖“电源”按钮

像“主页”按钮一样覆盖“电源”按钮

DIEA 2019-10-17 10:26:12
好吧,我正在做一些事情,我想禁用设备的所有硬按钮。硬按钮,例如电源,主页,提高音量,降低音量,搜索,返回。我已经成功覆盖了此处几乎所有按钮(电源除外)。因此,我只希望大家看到并请分享一些想法,以便我能摆脱它。我正在以中长按 Power 键事件onDispatchKeyEvent(),这与我想捕捉到同一事件的快捷方式相同。此外,在按下电源时,我还尝试通过获取of 来停止Screen,我成功接收了它,但我无法处理它。BroadcastSCREEN_OFF谢谢。然后,我创建了一个ReceiverScreen来接收屏幕开/关的广播ReceiverScreen.javapublic class ReceiverScreen extends BroadcastReceiver {    public static boolean wasScreenOn = true;    @Override    public void onReceive(Context context, Intent intent) {        if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {            // do whatever you need to do here            wasScreenOn = false;        } else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {            // and do whatever you need to do here            wasScreenOn = true;        }    }}DisableHardButton.javapublic class DisableHardButton extends Activity {@Overridepublic void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentView(R.layout.main);    IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);    filter.addAction(Intent.ACTION_SCREEN_OFF);    BroadcastReceiver mReceiver = new ReceiverScreen();    registerReceiver(mReceiver, filter);    }@Override    protected void onPause() {        // when the screen is about to turn off        if (ScreenReceiver.wasScreenOn) {            // this is the case when onPause() is called by the system due to a screen state change            System.out.println("SCREEN TURNED OFF");    } else {        // this is when onPause() is called when the screen state has not changed    }    super.onPause();}@Overrideprotected void onResume() {    // only when screen turns on    if (!ScreenReceiver.wasScreenOn) {        // this is when onResume() is called due to a screen state change        System.out.println("SCREEN TURNED ON");    } else {        // this is when onResume() is called when the screen state has not changed    }    super.onResume();}}
查看完整描述

3 回答

?
忽然笑

TA贡献1806条经验 获得超5个赞

与所有答案相反..检查一下:


@Override

public boolean dispatchKeyEvent(KeyEvent event) {

        if (event.getKeyCode() == KeyEvent.KEYCODE_POWER) {

                Log.i("", "Dispath event power");

                Intent closeDialog = new Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);

                sendBroadcast(closeDialog);

                return true;

        }


        return super.dispatchKeyEvent(event);

}

它能做什么:


每当用户按下电源按钮时。我们取消了对话,甚至看不到它!:) 这对我有用。(在note2上测试)


查看完整回答
反对 回复 2019-10-17
?
莫回无

TA贡献1865条经验 获得超7个赞

这是我所做的:


(1)创建一个接收器类:


public class ScreenReceiver extends BroadcastReceiver {


    @Override

    public void onReceive(Context context, Intent intent) {

        String action = intent.getAction();

        switch (action) {

            case Intent.ACTION_SCREEN_OFF:

                BaseActivity.unlockScreen();

                break;


            case Intent.ACTION_SCREEN_ON:

                // and do whatever you need to do here

                BaseActivity.clearScreen();

        }

    }

}

(2)上面调用的两个方法如下所示:


public static void unlockScreen () {

    if (current == null) return;


    Log.i( Constants.TAG, "Turning on screen ... " );


    Window window = current.getWindow();

    window.addFlags( WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD );

    window.addFlags( WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED );

    window.addFlags( WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON   );

}


public static void clearScreen () {

    if (current == null) return;


    Log.i( Constants.TAG, "Clearing screen flag when on ... " );


    Window window = current.getWindow();

    window.clearFlags( WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD );

    window.clearFlags( WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED );

    window.clearFlags( WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON   );

}


private static BaseActivity current;


@Override

protected void onResume () {

    current = this;

}

(3)在主要活动类onCreate()方法中注册接收者:


    IntentFilter filter = new IntentFilter( Intent.ACTION_SCREEN_ON );

    filter.addAction( Intent.ACTION_SCREEN_OFF );

    registerReceiver(new ScreenReceiver(), filter);


查看完整回答
反对 回复 2019-10-17
  • 3 回答
  • 0 关注
  • 346 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信