亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

像“主頁”按鈕一樣覆蓋“電源”按鈕

像“主頁”按鈕一樣覆蓋“電源”按鈕

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 關注
  • 366 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號