androidandroid-alertdialogpower-off

Handle alert when shut down device android in rooted app


I want change this message "Your phone is shut down" that appear when shut down the device.enter image description here

Do you have any ideas?

My app download videos in the background, I must warn you not to switch off the device if the download is not yet finished, such as happens when you shut down Windows and is downloading updates.


Solution

  • In AndroidManifest.xml:

    <uses-permission android:name="android.permission.PREVENT_POWER_KEY" />
    <uses-permission android:name="android.permission.DEVICE_POWER"
        tools:ignore="ProtectedPermissions" />
    

    MainActivity.java I intercept the power off button and force close system dialog and create a View custom above it

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_POWER) {
            // Do something here...
            event.startTracking(); // Needed to track long presses
    
            Intent closeDialog = new Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
            sendBroadcast(closeDialog);
            mViewCustom.setVisibility(View.VISIBLE);
    
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
    
    @Override
    public boolean onKeyLongPress(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_POWER) {
    
            return true;
        }
        return super.onKeyLongPress(keyCode, event);
    }