Simulating power button press to display switch off dialog box

Other Approach

adb shell input keyevent --longpress KEYCODE_POWER


Sixth approach:

$ adb shell input keyevent 26

works!

Seventh approach:

If you want the long press

$ adb shell <<!
> sendevent /dev/input/event5 1 107 1
> sleep 1
> sendevent /dev/input/event5 1 107 0
> exit
> !

You device may use a different input device than /dev/input/event5

Eighth approach

This approach is a bit more clever and uses sendevent or input keyevent depending on API level.

#! /usr/bin/env python

from com.dtmilano.android.adb.adbclient import *

AdbClient(serialno='your-serial-number-here').longPress('POWER')

The advantage here is that this is a generic approach and can be used to send other keys too. AdbClient is a python implementation of adb and is distributed with AndroidViewClient/culebra.