1

I want to lock screen (actually to trigger long click to show system dialog "turn off the phone?") via click button. Is it possible ? I found some examples like:

KeyguardManager keyguardManager = (KeyguardManager)getSystemService(Activity.KEYGUARD_SERVICE); 
KeyguardLock lock = keyguardManager.newKeyguardLock(KEYGUARD_SERVICE); 

But they don't work. Maybe I can switch phone off programmatically in other way? I found information that it's impossible so I'm trying to implement it like long click on lock button.

UPD: I found this:

 try {
                Process proc = Runtime.getRuntime()
                        .exec(new String[]{ "su", "-c", "reboot -p" });
                proc.waitFor();
            } catch (Exception ex) {
                ex.printStackTrace();
            }

But it also doesn't work. I'm testing it on emulator, will it work on real phone?

Thanks everyone for answers in advance !

Ening
  • 455
  • 3
  • 19
  • Did you tried? Intent i = new Intent("android.intent.action.ACTION_REQUEST_SHUTDOWN"); i.putExtra("android.intent.extra.KEY_CONFIRM", true); startActivity(i); – Tatsuya Mar 05 '18 at 16:35
  • In android SDK documents, It is clearly stated that the ACTION_SHUTDOWN and ACTION_REBOOT are protected intents that can only be sent by the system". You don't have the privilege to use this intent to reboot the device for security reason. – Ening Mar 05 '18 at 16:46
  • And this? https://stackoverflow.com/questions/10411650/how-to-shutdown-an-android-mobile-programatically – Tatsuya Mar 05 '18 at 16:50
  • Why do you want to do that? – Eselfar Mar 05 '18 at 16:55
  • please check the updated question. – Ening Mar 05 '18 at 17:05

1 Answers1

0

Here is the most usual way of requesting shutdown:

Intent i = new Intent("android.intent.action.ACTION_REQUEST_SHUTDOWN");
i.putExtra("android.intent.extra.KEY_CONFIRM", true);
startActivity(i);

Other methods you mentioned don't work because a regular app does not have the permission to run those (for obvious security reasons).

SoroushA
  • 2,043
  • 1
  • 13
  • 29