How to shutdown an Android mobile programmatically?

前端 未结 6 1445
慢半拍i
慢半拍i 2020-11-28 13:12

Is it possible to shutdown the mobile programmatically. that is with out using su commands..

6条回答
  •  佛祖请我去吃肉
    2020-11-28 13:31

    This is the code i use to perform any system command.

    void shutdown_sys()
    {
        Process chperm;
        try {
            chperm=Runtime.getRuntime().exec("su");
              DataOutputStream os = 
                  new DataOutputStream(chperm.getOutputStream());
    
                  os.writeBytes("shutdown\n");
                  os.flush();
    
                  chperm.waitFor();
    
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    

    Call this function from your android App. It will work if su is functional in your system. Let me know in case it does not work. I dont have an Android base ready to test. But the same works for reboot. So shutdown is also a linux shell command which ithink will be there in Android as well.. All the best

提交回复
热议问题