我正在尝试使用常规程序在 android 7(三星和索尼)中安装 APK Runtime.getRuntime.exec()
。安装失败,logcat 中出现以下异常:
09-04 14:14:33.932 16623-16623/? D/AndroidRuntime: Calling main entry com.android.commands.pm.Pm
09-04 14:14:33.939 3695-3876/? D/PackageInstaller: installation of android.content.pm.PackageInstaller$SessionParams@a4d2f0e for non-container user 0
09-04 14:14:33.940 16623-16623/? E/Pm: Error
java.lang.NullPointerException
at android.os.Parcel.readException(Parcel.java:1699)
at android.os.Parcel.readException(Parcel.java:1646)
at android.content.pm.IPackageInstaller$Stub$Proxy.createSession(IPackageInstaller.java:249)
at com.android.commands.pm.Pm.doCreateSession(Pm.java:530)
at com.android.commands.pm.Pm.runInstall(Pm.java:369)
at com.android.commands.pm.Pm.run(Pm.java:142)
at com.android.commands.pm.Pm.main(Pm.java:99)
at com.android.internal.os.RuntimeInit.nativeFinishInit(Native Method)
at com.android.internal.os.RuntimeInit.main(RuntimeInit.java:301)
09-04 14:14:33.941 16623-16623/? I/art: System.exit called, status: 1
09-04 14:14:33.941 16623-16623/? I/AndroidRuntime: VM exiting with result code 1
我尝试这样实现:
Process proc = Runtime.getRuntime().exec("pm install /sdcard/imo.apk");
所以:
Process proc = Runtime.getRuntime().exec(new String[]{"/system/bin/sh","-c","/system/bin/pm install /sdcard/imo.apk"});
但在这两种情况下都会出现异常。
在小于 7 的机器人中它可以工作。
权限是:
declared permissions:
android.permission.INTERACT_ACROSS_USERS_FULL: prot=normal
requested permissions:
android.permission.BIND_NOTIFICATION_LISTENER_SERVICE
android.permission.READ_EXTERNAL_STORAGE
android.permission.WRITE_EXTERNAL_STORAGE
android.permission.READ_LOGS
User 0: ceDataInode=393939 installed=true hidden=false suspended=false stopped=false notLaunched=false enabled=0
gids=[1007]
runtime permissions:
android.permission.CLEAR_APP_USER_DATA: granted=true
android.permission.INSTALL_PACKAGES: granted=true
android.permission.READ_EXTERNAL_STORAGE: granted=true
android.permission.INTERACT_ACROSS_USERS_FULL: granted=true
android.permission.READ_LOGS: granted=true
android.permission.CLEAR_APP_CACHE: granted=true
android.permission.WRITE_EXTERNAL_STORAGE: granted=true
安装确实通过 shell 成功。提前致谢