0

I am trying below code to reboot my mobile

 bt_viewpakageroot.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                try {
                    // clearing app data
                    //String packageName = getApplicationContext().getPackageName();
                    Runtime runtime = Runtime.getRuntime();
                    runtime.exec("reboot"); //pmclear
                    runtime.wait();
                    //runtime.exec("pm start "+packageName);
                    //runtime.wait();
                } catch (Exception e) {
                    Log.e("packageexeception", "&&&&     " + e.toString());
                    Toast.makeText(MainActivity.this, "print errors     :      " + e.toString(), Toast.LENGTH_SHORT).show();
                    e.printStackTrace();
                }
            }
        });

But i getting below error

java.lang.IllegalMonitorStateException: object not locked by thread before wait() 06-2

I already google it..,i did some research but i did get any anwer

java.lang.IllegalMonitorStateException: object not locked by thread before wait()?

Android java.lang.IllegalMonitorStateException: object not locked by thread before wait()

I want to resolve this error,,i don't want alternative solution for this due do some reason.

But what i did mistake i don't know please any one help me

demo
  • 672
  • 3
  • 9
  • 34

1 Answers1

0

wait() will not work for your purpose. wait() is called for synchronization tasks and should be enclosed in a synchronized(runtime){} block. In your case runtime.exec("reboot"); is executed in a separated process, so you will need to read the Process documentation in order to correctly "wait" for process completion and manage the results.

You can use proc.waitFor(), but reboot will require a rooted phone in order to work. This question has been answered many times, you can read this: Runtime.exec() : Reboot in Android?

Raymond Arteaga
  • 4,355
  • 19
  • 35