0

I am working on a project and still thinking if it can work to make an android application that should turn the phone completely off when the user has passed a certain deadline. and even if the user reset his settings the application still working. is it possible to do something like this ?

Thanks.

user3821852
  • 41
  • 1
  • 1
  • 4

1 Answers1

0

It will work (if rooted)...

call

runRootCmd("reboot -p"); // "-p" will power off

method:

public static void runRootCmd(String cmd) {
        if (TextUtils.isEmpty(cmd)) {
            return;
        }
        Process process;
        try {
            process = Runtime.getRuntime().exec("su");
            DataOutputStream os = new DataOutputStream(
                    process.getOutputStream());
            os.writeBytes(cmd + " ;\n");
            os.flush();

            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            int read;
            InputStream errIs = process.getErrorStream();
            while ((read = errIs.read()) != -1) {
                baos.write(read);
            }
            baos.write('\n');

            InputStream inIs = process.getInputStream();
            while ((read = inIs.read()) != -1) {
                baos.write(read);
            }

            byte[] data = baos.toByteArray();
            String result = new String(data);

            Log.d(TAG, "runRootCmd result: " + result);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
Martin Pfeffer
  • 12,471
  • 9
  • 59
  • 68
  • Well, I am new to this, but the application should also prevent the user from turning the phone on or turning it on with an activation key or something ! – user3821852 Nov 15 '15 at 13:24