mercredi 15 juin 2016

How to Hide / disable Power Button Options on Key Long Press

I know the subject has been discussed on different posts but I tried many of them and they don't work.

I am on Android Lollipop 5.0.2. I try to avoid the power button long press to display the Power Options dialog (Shutdown and Airplane options).

1) Using onWindowsFocusChanged in my Activity. It works, but it displays the dialog during a short time. It's not clean.

2) Using AccessibilityEvent capturing getEventType(event) = "TYPE_VIEW_FOCUSED". I couldn't make it work.

3) Using Key_Power_Button : short press and longPress do nothing in my case. I couldn't make it work

4) Using a Receiver with CLOSE_SYSTEM_DIALOGS intent : That seems to be the good solution but in my case, the Broadcast Receiver is looping constantly in background, it consumes lots of battery and creates a crash.

==> I Want to capture only the Power Options dialog (Shutdown and Airplane options) <==

The solution of My God seems to be the good answer but I couldn't comment his post due to my low reputation : How to dismiss system dialog in Android?

I don't know how to adapt the reason_Key to "power_options". If you have any idea to help me to fix it using any kind of solution. Thanks a lot

Working example of My God -

Android Manifest-

<receiver android:name=".SystemDialogReceiver">
        <intent-filter>
            <action android:name="android.intent.action.CLOSE_SYSTEM_DIALOGS" />
        </intent-filter>
</receiver>

Class file-

class SystemDialogReceiver extends BroadcastReceiver {

    private static final String SYSTEM_DIALOG_REASON_KEY = "reason";
    private static final String SYSTEM_DIALOG_REASON_RECENT_APPS = "recentapps";

    @Override
    public void onReceive(Context context, Intent intent) {

     if(intent.getAction().equals(Intent.ACTION_CLOSE_SYSTEM_DIALOGS)){
            String dialogType=intent.getStringExtra(SYSTEM_DIALOG_REASON_KEY);

     if(dialogType != null && dialogType.equals(SYSTEM_DIALOG_REASON_RECENT_APPS)){
                Intent closeDialog = new Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
                context.sendBroadcast(closeDialog);

            }
        }
    }

}

Aucun commentaire:

Enregistrer un commentaire