FewWords
FewWords

Reputation: 675

How to perform a long press on power button using Accessibility Services

I want to show the turn off/reboot dialog without actually long pressing the power button. Every single source I have found says that this is not possible unless the phone is rooted. However, I found an app called Tasker in the google play store which have the possibility to show the turn off/reboot dialog without the use of the power button or being rooted. This means that it must be possible somehow. The app is using Accessibility Services but I can not figure out how to simulate a long press on the power button. Maybe someone here can help me or point me in the right direction.

I have setup an AccessibilityService which receives events when I press a normal button:

@Override
public void onAccessibilityEvent(AccessibilityEvent accessibilityEvent) {
    switch (accessibilityEvent.getEventType()){
        case AccessibilityEvent.TYPE_VIEW_CLICKED:
            //What should be here ???
            break;
    }
}

This works but I do not know what to do next.

Upvotes: 1

Views: 2604

Answers (1)

Larry Schiefer
Larry Schiefer

Reputation: 15775

Use the global actions triggered by something in your app when you want to show the power dialog. This could be done by your Activity sending an Intent to your accessibility service or even a custom binder interface. Within the service call:

performGlobalAction(GLOBAL_ACTION_POWER_DIALOG);

This requires API 21+, though.

Upvotes: 3

Related Questions