1

I want to restart the mobile when I press a button.For that i wrote these..

        PowerManager pm;
        public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.main);        
           pm = (PowerManager) getSystemService(Context.POWER_SERVICE);        
           Button test = (Button) findViewById(R.id.button1);

           test.setOnClickListener(new OnClickListener() {          
        public void onClick(View v) {

            try {
                Process proc = Runtime.getRuntime().exec(new String[] {"su", "-c", "reboot" });

                            int result = proc.waitFor();
                Toast.makeText(ForTestActivity.this, "INt:"+result, Toast.LENGTH_SHORT).show();


            } catch (Exception ex) {
                Log.i("XXXXXXXXXXXX", "Could not reboot", ex);
            }

        }
    });

} 

This is not working in emulator.I'm using android2.3.3 for develop this application. It does not give any errors and nothing happened after I press the button. The toast was displaying result as 1.

After that I tried this code also..

     Process p = Runtime.getRuntime().exec("su");
                DataOutputStream os = new DataOutputStream(p.getOutputStream());            
                os.writeBytes("reboot"+"\n");                             
                os.writeBytes("exit\n");  
                os.flush(); 

Same result.I added necessary permissions also.Is someone having any idea ?

[Somehow I want to restart (or Shutdown) the phone after press a button.]

Anjula
  • 1,690
  • 23
  • 29

2 Answers2

1

A similar question was asked and answered here. You can't reboot an unrooted phone until you have the firmware-key to sign it.

Community
  • 1
  • 1
Ponmalar
  • 6,871
  • 10
  • 50
  • 80
1

Have you tried the above command without the "su -c"?

When I type "adb shell reboot" my phone reboots (but my phone is rooted which is needed in this case) so I think effect should be similar if you just execute "reboot" from code. I think "su -c" is unnecessary as you have root rights already on rooted phone (IIRC emulator is rooted by default).

As other have mentioned, this will not work on unrooted phone.

kajman
  • 1,066
  • 11
  • 24