简单说明

这个小工具包含了几个小功能,包括关机,重启,热重启(快速重启),进入fastboot模式,进入recovery模式的功能。

启动应用时会检测一次root,未root的手机会有相关的提示,不足的是已root但未授权会没有提示。

功能都是通过简单的android shell 实现的,而非调用系统api,因为这样太麻烦了。

 

执行过程

用户启动应用

检测root并提示

点击按钮,弹窗二次确认,执行相应动作

 

具体实现

 

检测root

 

public static boolean upgradeRootPermission(String pkgCodePath,Context cc) {
    Process process = null;
    DataOutputStream os = null;
    try {
        String cmd = "chmod 777 " + pkgCodePath;
        process = Runtime.getRuntime().exec("su"); //切换到root帐号
        os = new DataOutputStream(process.getOutputStream());
        os.writeBytes(cmd + "\n");
        os.writeBytes("exit\n");
        os.flush();
        process.waitFor();
        return true;
    } catch (Exception e) {
        return false;
    } finally {
        try {
            if (os != null) {
                os.close();
            }
            process.destroy();
        } catch (Exception e) {
        }

    }
}


点击按钮后弹窗,执行动作(使用公共的方法)

public  static  void   showTips(final Context mContext,final String commandText,String messageText)
{
    new AlertDialog.Builder(mContext)
            .setTitle("提示")
            .setMessage(messageText)
            .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {

                    String cmd =commandText;
                    try {
                        Runtime.getRuntime().exec(cmd);
                    } catch (IOException e) {
                        new AlertDialog.Builder(mContext).setTitle("执行失败").setMessage(
                                e.getMessage()).setPositiveButton("确定", null).show();
                    }
                }
            })
            .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    // 取消当前对话框
                    dialog.cancel();
                }
            }).show();

}

按钮绑定执行动作

Button shutdownBtn = (Button) findViewById(R.id.shutdown);
shutdownBtn.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        showTips(mContext, "su -c reboot -p", "确认关机么?");
    }
});

 

运行截图

Screenshot_2015-12-05-22-03-07Screenshot_2015-12-05-22-03-15

届ける言葉を今は育ててる
最后更新于 2017-10-13