Shanaz K
Shanaz K

Reputation: 698

How to power off an Android device programmatically

How can you power off an Android device programmatically?

I searched for an answer, some said your phone must be rooted (I don't want to do that), and others said it's impossible.

Is it really impossible?

Upvotes: 1

Views: 5503

Answers (3)

Guillermo Merino
Guillermo Merino

Reputation: 3257

You can find another answers in StackOverflow, 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.

Upvotes: 0

Sebastian Breit
Sebastian Breit

Reputation: 6159

You can't do it unless you have a rooted phone. Then it is as easy as calling the shutdown command for linux:

void shutdown()
{

    try {
          Process m_process=Runtime.getRuntime().exec("su");
          DataOutputStream os = new DataOutputStream(m_process.getOutputStream());
          os.writeBytes("shutdown\n");
          os.flush();
          m_process.waitFor();

    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

Upvotes: 4

grizeldi
grizeldi

Reputation: 180

I'm not sure that this is what you want but try using this: http://www.basic4ppc.com/android/forum/threads/togglelibrary.12388/#post69639

I haven't tested that library but maybe goToSleep method will work in your case.

Upvotes: 0

Related Questions