0

I want to show my custom UI when the power off button is long pressed and show the power option ui, Then want to dismiss this ui and show my custom UI. How can I detect power button long press or power menu system ui. How can I detect this system ui appearing and how can I dismiss this, after dismiss I open automatic my app and show a related screen.

My app already have access :-

  1. Administrator access
  2. Enable accessibility service
  3. Ignore battery optimization for running in background with low battery
  4. Enabled App launch or auto start permission

After having this permission, can I dismiss the system generated ui of power option when user click on power button. and after dismiss this dialog ,open my app with related screen.

Image ref. Image 1

1 Answers1

-2

User Broadcast Receiver:

android.intent.action.SCREEN_OFF

<intent-filter>
        <action android:name="android.intent.action.SCREEN_OFF" />
    </intent-filter>

user Broadcast Receiver Class:

 class PowerDownReceiver : BroadcastReceiver() {
    override fun onReceive(context: Context?, intent: Intent?) {
        if (intent?.action == Intent.ACTION_SCREEN_OFF) {
     // Doto user Logic
        }
    }
   }

register Receiver in your Activity:

val intentFilter = IntentFilter(Intent.ACTION_SCREEN_OFF)
    registerReceiver(PowerDownReceiver(), intentFilter)

its 100% work in up-to Android 9

  • This is misleading. [ANDROID_SCREEN_OFF](https://developer.android.com/reference/android/content/Intent#ACTION_SCREEN_OFF) is, according to the docs (and I quote): "This broadcast is sent when the device becomes non-interactive which may have nothing to do with the screen turning off." And while the OP wants this to happen exactly when the power button is pressed, it doesn't mean the power button is LONG-PRESSED as that doesn't set the device in non-interactive mode. Considering Pie/Android 9 was released in 2018, it seems a bit dangerous to create a solution based on 5+ years old software. – Martin Marconcini May 26 '23 at 12:53
  • Yes @MartinMarconcini, you are right this is only for detect screen off, we cannot detect the power button click from this. Is there any solution for detect ? – Surajkaran Meghwanshi May 31 '23 at 08:42
  • Honestly, i do not know. :/ – Martin Marconcini Jun 01 '23 at 09:36