Android Question Power Off via code

Johan Schoeman

Expert
Licensed User
Longtime User
What I found on the web...
.... it is not possible in non rooted phones, this is deliberate, you don't want to allow third party apps to turn off your device that will create undesired behaviors in your phone if you install the wrong application.....
 
Upvote 0

Galbas

Member
Licensed User
Longtime User
I am using the tablet in an industrial application. This tablet is part of my machine, so I have total control about it. What is rooted phone?
 
Upvote 0

JTmartins

Active Member
Licensed User
Longtime User
You need to have root access to the android Operating System, wich means that you need to be able to run applications as SuperUser. This is not the default in Android. You need a tool to root the device first.

Beware that if you do not know what you are doing, you may end up with a damaged tablet.

Then you can run applications as SuperUser, and you can power down by code with something like :

B4X:
Sub restart
    '**** restart
    Dim ph As Phone
    Dim Command, Runner As String
    Dim StdOut, StdErr As StringBuilder

    StdOut.Initialize
    StdErr.Initialize
    Runner = File.Combine(File.DirInternalCache, "runner")
    Command = File.Combine(File.DirInternalCache, "command")
    File.WriteString(File.DirInternalCache, "runner", "su < " & Command)
    File.WriteString(File.DirInternalCache, "command", "su -c reboot") 
    ph.Shell("sh", Array As String(Runner), StdOut, StdErr)
    '***Restart End
End Sub   
   
Sub ShutDown
    '**** Power Off
    Dim ph As Phone
    Dim Command, Runner As String
    Dim StdOut, StdErr As StringBuilder

    StdOut.Initialize
    StdErr.Initialize
    Runner = File.Combine(File.DirInternalCache, "runner")
    Command = File.Combine(File.DirInternalCache, "command")
    File.WriteString(File.DirInternalCache, "runner", "su < " & Command)
    File.WriteString(File.DirInternalCache, "command", "su -c reboot -p") 
    ph.Shell("sh", Array As String(Runner), StdOut, StdErr)
    '***Power off End
       
End Sub
 
Upvote 0
Top