这个问题在这里已有答案:
我的要求是:App A将运行已经位于“system / bin”位置的shell脚本(myshellscript.sh),shell脚本将安装存储在“sdcard / Download”位置的App B,并将启动App B.
所以在继续之前我想告诉你
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 A运行相同的脚本时所有用脚本编写的命令都在工作,除了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();
}
更新:我捕获了adb日志并获得以下异常:
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的权限高于您应用的沙盒权限。