好吧,我是 android 编程的新手,我正在制作一个根应用程序以使用按钮将特定文件推送到/system/framework
我怎样才能做到这一点?我尝试了命令样式,但没有一个起作用
最佳答案
要做到这一点,您需要执行许多步骤。
首先(当然)需要对设备进行 root。您可以通过多种方式进行检查。
以下代码将检查“su”命令是否返回命令未找到错误(su 二进制文件存在)以及是否安装了 super 用户应用程序以在您请求权限后授予权限。
private boolean isDeviceRooted() {
// check for SU command in shell
if ((new ExecShell().executeCommand(ExecShell.SHELL_COMMAND.su_check) != null) && (appInstalled("eu.chainfire.supersu.nonag") || appInstalled("eu.chainfire.supersu") || appInstalled("com.noshufou.android.su") || appInstalled("com.koushikdutta.superuser"))) {
Log.i(TAG, "Device Rooted");
return true;
}
// check for SU application installed
if (appInstalled("eu.chainfire.supersu.nonag") || appInstalled("eu.chainfire.supersu") || appInstalled("com.noshufou.android.su") || appInstalled("com.koushikdutta.superuser")) {
Log.i(TAG, "Device Rooted");
return true;
}
Log.i(TAG, "Device Not Rooted");
return false;
}
private boolean appInstalled(String uri) {
PackageManager pm = getPackageManager();
boolean app_installed = false;
try {
pm.getPackageInfo(uri, PackageManager.GET_ACTIVITIES);
app_installed = true;
} catch (PackageManager.NameNotFoundException e) {
app_installed = false;
}
return app_installed;
}
如果此代码返回 false,您可以设置标志或显示错误,否则继续。
然后,一旦您知道设备已获得 root 权限,您就想要执行必要的 root 命令来执行您需要的操作。
以下代码将命令的String[]作为输入,并以root身份依次执行。
public boolean RunAsRoot(String[] cmds) {
Process p;
try {
p = Runtime.getRuntime().exec("su");
DataOutputStream os = new DataOutputStream(p.getOutputStream());
try {
for (String tmpCmd : cmds) {
os.writeBytes(tmpCmd + "\n");
}
os.writeBytes("exit\n");
os.flush();
return true;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return false;
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return false;
}
}
在您的情况下,您首先要将/system 挂载为 rw。网上有很多信息可以帮助您找到所需的命令,但它看起来像
mount -o remount rw/system mount -o remount rw/system
然后您想要使用 mv
或 cp
移动您想要的文件。
使用 root 命令的一个例子是
String[] cmds = {"mount -o remount rw /system mount -o remount rw /system", "cp /sdcard/myfile /system/framework/myfile"};
if(!RunAsRoot(cmds)){
//Commands failed to run, show an error/retry
}
这涵盖了作为根功能的“硬”部分。
可以找到按钮的简单教程 here .
程序流程可以是
onCreate(){
checkIsRooted();
Button x = (Button) findViewById(R.id.x);
x.setOnClickListener(onClickListener());
}
onClickListener(){
onClick(){
String[] cmds = {...};
if(!runAsRoot(cmds))
AlertDialog.Builder.makeText(...).show();
}
}
请注意,这是伪代码,您无法复制和粘贴此代码以使其正常工作,您需要自己正确地执行此操作!
关于android - 需要帮助应用程序使用按钮推送文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19017748/