Android 安装APK并从Shell脚本启动应用程序

Android 安装APK并从Shell脚本启动应用程序,android,linux,bash,shell,android-scripting,Android,Linux,Bash,Shell,Android Scripting,我的要求是: 应用程序A将运行已经在“系统/bin”位置的shell脚本(myshellscript.sh),shell脚本将安装存储在“SD卡/下载”位置的应用程序B,并将启动应用程序B 在继续之前,我想告诉你 我的设备已经根,因为我有自定义ROM闪存在其中 应用程序A是具有系统权限的系统应用程序 当我通过命令运行我的脚本时,我的脚本按照要求运行良好:adb shell sh system/bin/myshellscript.sh 我对shell脚本不太了解 下面是我的shell脚本: #!/

我的要求是: 应用程序A将运行已经在“系统/bin”位置的shell脚本(myshellscript.sh),shell脚本将安装存储在“SD卡/下载”位置的应用程序B,并将启动应用程序B

在继续之前,我想告诉你

  • 我的设备已经根,因为我有自定义ROM闪存在其中
  • 应用程序A是具有系统权限的系统应用程序
  • 当我通过命令运行我的脚本时,我的脚本按照要求运行良好:
    adb shell sh system/bin/myshellscript.sh
  • 我对shell脚本不太了解
  • 下面是我的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的权限高于沙盒中的应用程序。

    您能否更改
    rt.exec(“sh/system/bin/myshellscript.sh”)
    使用单独的参数调用
    rt.exec(“sh”,“/system/bin/myshellscript.sh”)
    rt.exec()
    API不使用两个字符串作为参数。但我想问题不在于运行脚本,因为我的脚本正在运行,正如我在问题中提到的,脚本中的所有命令都在运行,除了install。