运行命令 Android 终端模拟器。通过代码安装apk
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11559749/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Run commands Android Terminal Emulator. Install apk by code
提问by Ricmcm
I'm trying to run commands on the Android Terminal Emulator. I have my device rooted and I installed superuser.apk.
我正在尝试在 Android 终端模拟器上运行命令。我的设备已植根,并安装了superuser.apk。
I'm doing it like this:
我是这样做的:
try {
Process process = Runtime.getRuntime().exec(cmd);
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line = bufferedReader.readLine();
while ( line != null ) {
Log.i("SHELL", line);
line = bufferedReader.readLine();
}
} catch ( Exception e) {
e.printStackTrace();
}
And also well:
还有:
try {
Process process = Runtime.getRuntime().exec(<b>cmd</b>*);
process.waitFor();
} catch ( Exception e ){
e.printStackTrace();
}
I'm trying to install a. Apk by code. I tried the following:
我正在尝试安装一个。通过代码 Apk。我尝试了以下方法:
cmd=> pm install /mnt/sdcard/app.apk
Without any results.
cmd=>pm install /mnt/sdcard/app.apk
没有任何结果。
cmd=> su -c "pm install /mnt/sdcard/app.apk"
With single quotes, double quoutes and no quotes. Result:
cmd=>su -c "pm install /mnt/sdcard/app.apk"
带单引号、双引号和不带引号。结果:
I/SHELL(1432): Usage: su [options] [LOGIN]
I/SHELL(1432): Options:
I/SHELL(1432): -c, --command COMMAND pass COMMAND to the invoked shell
I/SHELL(1432): -h, --help display this help message and exit
I/SHELL(1432): -, -l, --login make the shell a login shell
I/SHELL(1432): -s, --shell SHELL use SHELL instead of the default in passwd
I/SHELL(1432): -v, --version display version number and exit
I/SHELL(1432): -V display version code and exit. this is
I/SHELL(1432): used almost exclusively by Superuser.apk
Other commands like lsruns fine.
ls等其他命令运行良好。
How I can install an apk located in /mnt/sdcard?
如何安装位于 /mnt/sdcard 中的 apk?
Thanks!
谢谢!
回答by throrin19
try this :
尝试这个 :
su -c "pm install /mnt/sdcard/app.apk" root
But i think, it doesn't work if you have no root your phone
但我认为,如果你的手机没有 root 就行不通
回答by Ricmcm
I found the solution.
我找到了解决方案。
Process p;
try {
// Preform su to get root privledges
p = Runtime.getRuntime().exec("su");
// Attempt to write a file to a root-only
DataOutputStream os = new DataOutputStream(p.getOutputStream());
os.writeBytes(**any command**+"\n");
// Close the terminal
os.writeBytes("exit\n");
os.flush();
try {
p.waitFor();
if (p.exitValue() != 255) {
// Sucess :-)
}
else {
// Fail
}
} catch (InterruptedException e) {
// Fail
}
} catch (IOException e) {
// Fail
}
Thanks all!
谢谢大家!
回答by user370305
I have a doubt that when you are executing a command its not a ADB Shell its your application's shell environment.
我怀疑当您执行命令时,它不是 ADB Shell,而是您的应用程序的 Shell 环境。
From code: (If you want to do it by Intent then)
来自代码:(如果您想通过 Intent 执行此操作)
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(new File(path+"/<application_name>.apk")), "application/vnd.android.package-archive");
startActivity(intent);
And this is the permissions..
这是权限..
<uses-permission android:name="android.permission.INSTALL_PACKAGES" />