我的要求是:App A将运行已经位于"system/bin“位置的shell脚本(myshellscript.sh),而shell脚本将安装存储在"sdcard/Download”位置的App B,并启动App 。
所以在继续之前,我想告诉你们
adb shell sh system/bin/myshellscript.sh下面是我的shell脚本:
#!/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 的按钮上以编程方式运行相同的脚本时,除了pm install -r "/sdcard/Download/SampleApplication.apk";代码之外,所有用脚本编写的命令都是工作的,我试图运行的脚本是:
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();
}更新:I捕获了亚行日志,并在异常下运行:
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发布于 2017-12-13 10:35:33
当由亚行shell执行脚本时,它在shell权限下运行。Shell比你的沙箱应用程序拥有更高的权限。
https://stackoverflow.com/questions/47668812
复制相似问题