从Shell脚本安装APK并启动应用 [英] Install a APK and Launch App from Shell script
问题描述
我的要求是: App A 将运行已经在系统/bin" 位置的shell脚本(myshellscript.sh),shell脚本将安装 App B 存储在"sdcard/下载" 位置,并将启动应用B .
My Requirement is : App A will Run shell script(myshellscript.sh) that is already in "system/bin" location and shell script will install App B that is stored in "sdcard/Download" location and will launch App B.
所以在继续之前,我想告诉你
So Before continuing further i want to tell you that
- 我的设备已经植根,因为我已在其中刷新了自定义ROM
- App A是具有系统特权的系统应用.
- 当我通过命令运行脚本时,我的脚本可以正常运行:
adb shell sh system/bin/myshellscript.sh
- 我对shell脚本的了解不多.
- my device is already rooted as i have custom ROM flashed in it
- App A is a system app having system privilege.
- My script is working fine as per requirement when i am running my script by command :
adb shell sh system/bin/myshellscript.sh
- I dont have that much knowledge of shell script.
下面是我的shell脚本:
below is my shell script:
#!/bin/bash
echo "Shell script works on Android"
pm install -r "/sdcard/Download/SampleApplication.apk";
echo "Going to sleep for 15 sec"
sleep 15;
echo "woked up after 15 sec"
am start -n "com.aaa.sampleapplication/.MainActivity";
sleep 5;
问题是,当我通过上述命令运行此脚本时,它的工作正常,但是在按 App A 的按钮单击时以编程方式运行相同的脚本时,除pm install -r "/sdcard/Download/SampleApplication.apk";
之外,所有用脚本编写的命令都可以正常工作
我试图运行我的脚本的代码是:
So issue is when i am running this script by command mentioned above its working fine but when running same script programmatically on button click of App A all commands written in script is working except pm install -r "/sdcard/Download/SampleApplication.apk";
Code i am trying to run my script is :
try
{
Runtime rt = Runtime.getRuntime();
Process proc = rt.exec("sh /system/bin/myshellscript.sh");
InputStream is = proc.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String line;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
} catch (Throwable t)
{
t.printStackTrace();
}
更新: 我捕获了adb日志并出现以下异常:
UPDATE: I captured adb log and getting below exception:
AndroidRuntime: Calling main entry com.android.commands.pm.Pm
11-19 00:37:50.867 7887 7887 E Pm : Error
11-19 00:37:50.867 7887 7887 E Pm : java.lang.NullPointerException
11-19 00:37:50.867 7887 7887 E Pm : at android.os.Parcel.readException(Parcel.java:1690)
11-19 00:37:50.867 7887 7887 E Pm : at android.os.Parcel.readException(Parcel.java:1637)
11-19 00:37:50.867 7887 7887 E Pm : at android.content.pm.IPackageInstaller$Stub$Proxy.createSession(IPackageInstaller.java:249)
11-19 00:37:50.867 7887 7887 E Pm : at com.android.commands.pm.Pm.doCreateSession(Pm.java:552)
11-19 00:37:50.867 7887 7887 E Pm : at com.android.commands.pm.Pm.runInstall(Pm.java:392)
11-19 00:37:50.867 7887 7887 E Pm : at com.android.commands.pm.Pm.run(Pm.java:142)
11-19 00:37:50.867 7887 7887 E Pm : at com.android.commands.pm.Pm.main(Pm.java:99)
11-19 00:37:50.867 7887 7887 E Pm : at com.android.internal.os.RuntimeInit.nativeFinishInit(Native Method)
11-19 00:37:50.867 7887 7887 E Pm : at com.android.internal.os.RuntimeInit.main(RuntimeInit.java:277)
11-19 00:37:50.869 7887 7887 I art : System.exit called, status: 1
推荐答案
通过adb shell执行脚本时,该脚本将在shell许可下运行. Shell具有比您的已沙箱化的应用程序更高的权限.
When execute a script by adb shell, it runs under shell permission. Shell has higher permission than you app which is sandboxed.
这篇关于从Shell脚本安装APK并启动应用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!