最近在移植rom模块到 7.0 时, 发现之前静默安装的功能无法实现了,会报出错误:
java.lang.SecurityException: runInstallCreate from pm command asks to run as user ….. requires android.permission.INTERACT_ACROSS_USERS_FULL
然后 我就很情愿的按照他的错误, 去我的app 里面加上了这个权限。以为可以成功,加了之后一样错误不行, 然后就百度了一下, 也基本没有发现什么解决方案
相关一些 pm 等的东西,后面再发
那可能之前的安装apk代码都是以下几种:
1. Process p = Runtime.getRuntime().exec(“pm install -r apkpath”)
p.waitFor();
2. Process p = Runtime.getRuntime().exec( new String[]{ "pm", "install", "-r", apk})
p.waitFor();
3. private void SilentInstall(Context mContext,final String mypackageName, String path) {
Uri uri = Uri.fromFile(new File(path));
//Log.d(TAG, "SilentInstall path="+path);
//Log.d(TAG, "SilentInstall uri="+uri);
//Log.d(TAG, "SilentInstall packageName="+packageName);
int installFlags = 0;
PackageManager pm = mContext.getPackageManager();
try {
PackageInfo packageInfo = pm.getPackageInfo(mypackageName,
PackageManager.GET_UNINSTALLED_PACKAGES);
if (packageInfo != null) {
installFlags |= PackageManager.INSTALL_REPLACE_EXISTING;
}
} catch (NameNotFoundException e) {
//Log.e(TAG, "NameNotFoundException = " + e.getMessage());
}
pm.installPackage(uri, new IPackageInstallObserver.Stub() {
@Override
public void packageInstalled(String packageName, int returnCode) {
// Looper.prepare();
if (returnCode == 1) {
Log.d(TAG, "install apk Success packageName:"+ packageName);
} else {
Log.d(TAG, "install fail!");
}
// Looper.loop();
}
}, installFlags, mypackageName);
}
然后就去源码查到了一些 7.0 的用法 ,送给遇到问题的小伙伴(用法参数里面都有解释有些是英文)
pm path [--user USER_ID] PACKAGE //查看apk安装后位置
pm dump PACKAGE //打印给定的包的系统状态
DUMP OF SERVICE package 打印服务信息
DUMP OF SERVICE activity 打印activity信息
DUMP OF SERVICE meminfo 打印当前内存使用信息
DUMP OF SERVICE procstats 打印系统内存使用与一段时间内存汇总
DUMP OF SERVICE usagestats 打印服务器使用状态信息
DUMP OF SERVICE batterystats 打印电池状态信息
pm install [-lrtsfd] [-i PACKAGE] [--user USER_ID] [PATH] //安装应用 ---- 比较重要常用的
pm install-create [-lrtsfdp] [-i PACKAGE] [-S BYTES]
[--install-location 0/1/2]
[--force-uuid internal|UUID]
pm install-write [-S BYTES] SESSION_ID SPLIT_NAME [PATH]
pm install-commit SESSION_ID
pm install-abandon SESSION_ID
pm uninstall [-k] [--user USER_ID] PACKAGE // 卸载应用 ---- 比较重要常用的
pm set-installer PACKAGE INSTALLER
pm move-package PACKAGE [internal|UUID]
pm move-primary-storage [internal|UUID]
pm clear [--user USER_ID] PACKAGE
pm enable [--user USER_ID] PACKAGE_OR_COMPONENT //使package或component可用。(如:pm enable "package/class")
pm disable [--user USER_ID] PACKAGE_OR_COMPONENT //使package或component不可用。(如:pm disable "package/class")(disable了指定的package,但是getComponentEnabledSetting该package里的components依然是enable状态的。disable-user一样原理。)
pm disable-user [--user USER_ID] PACKAGE_OR_COMPONENT
pm disable-until-used [--user USER_ID] PACKAGE_OR_COMPONENT
pm default-state [--user USER_ID] PACKAGE_OR_COMPONENT
pm hide [--user USER_ID] PACKAGE_OR_COMPONENT //隐藏package或component
pm unhide [--user USER_ID] PACKAGE_OR_COMPONENT //恢复可见package或component
pm grant [--user USER_ID] PACKAGE PERMISSION //授予权限 授予应用权限许可。必需android6.0(API级别23)以上的设备
pm revoke [--user USER_ID] PACKAGE PERMISSION //撤销权限 必需android6.0(API级别23)以上的设备
pm reset-permissions
pm set-app-link [--user USER_ID] PACKAGE {always|ask|never|undefined}
pm get-app-link [--user USER_ID] PACKAGE
pm set-install-location [0/auto] [1/internal] [2/external] //设置安装位置
pm get-install-location
pm set-permission-enforced PERMISSION [true|false] //使指定权限生效 或 失败
pm trim-caches DESIRED_FREE_SPACE [internal|UUID]
pm create-user [--profileOf USER_ID] [--managed] [--restricted] [--ephemeral] [--guest] USER_NAME
pm remove-user USER_ID //根据用户id删除用户
pm remove-user: //remove the user with the given USER_IDENTIFIER,
//deleting all data associated with that user
pm get-max-users // (Maximum supported users: 4) 备所支持的最大USER数
pm path PACKAGE: //print the path to the .apk of the given PACKAGE.
pm dump PACKAGE: //print system state associated with the given PACKAGE.
pm install PACKAGE //: install a single legacy package
pm install-create: create an install session
-l: forward lock application 锁定应用程序 - 用的少
-r: replace existing application
-t: allow test packages
-i: specify the installer package name
-s: install application on sdcard
-f: install application on internal flash
-d: allow version code downgrade (debuggable packages only)
-p: partial application install
-g: grant all runtime permissions; // :授权给应用。
-S: size in bytes of entire session
pm install-write: write a package into existing session; path maybe '-' to read from stdin
-S: size in bytes of package, required for stdin
pm install-commit: perform install of fully staged session
pm install-abandon: abandon session
pm set-installer: set installer package name
pm uninstall [options] : removes a package from the system. Options:
-k: keep the data and cache directories around after package removal.
pm clear : deletes all data associated with a package. 对指定的包进行删除
//pm enable, disable, disable-user, disable-until-used, default-state:these commands change the enabled state of a given package or
//component (written as \"package/class\").
//pm grant, revoke: these commands either grant or revoke permissions to apps.
//The permissions must be declared as used in the app's
//manifest, be runtime permissions (protection level dangerous),
//and the app targeting SDK greater than Lollipop MR1.
pm reset-permissions: revert all runtime permissions to their default state.
pm get-install-location: returns the current install location. //得到安装位置
0 [auto]: Let system decide the best location
1 [internal]: Install on internal device storage
2 [external]: Install on external media
pm set-install-location: changes the default install location.
//NOTE: this is only intended for debugging; using this can cause
//applications to break and other undersireable behavior.
0 [auto]: Let system decide the best location
1 [internal]: Install on internal device storage
2 [external]: Install on external media
pm trim-caches: trim cache files to reach the given free space.
pm create-user: create a new user with the given USER_NAME,
//printing the new user identifier of the user.
//NOTE: 'pm list' commands have moved! Run 'adb shell cmd package to display the new commands. 这个是在7.0 mtk 源码中
pm list users //查看当前user
Users:
UserInfo{0:机主:13} running
"string resource"
android:icon="drable resource"
android:label="string resource"
android:name="string"
android:permissionGroup="string"
android:protectionLevel=["normal"|"dangerous"|"signature"|"signatureOrSystem"]/>
protectionLevel
normal 表示权限是低风险的,不会对系统,用户或其他应用程序造成危害
dangerous 表示权限是高风险的,系统将可能要球用户输入相关信息,才会授予此权限
signature 表示只有当应用程序所用数字签名与声明引用权限的应用程序所用签名相同时,才能将权限授予给它
signatureOrSystem 需要签名或者系统级应用(放置在/system/app目录下)才能赋予权限
system 系统级应用(放置在/system/app目录下)才能赋予权限
自定义权限 应用自行定义的权限
pm list permission-groups :打印所有已知的权限群组。
pm list permissions [options] pm list permissions –g -d -u
-g 按组进行列出权限
-f 打印所有信息
-s 简短的摘要
-d 只有危险的权限列表
-u 只有权限的用户将看到列表
用户自定义权限
pm list features //设备特性。硬件之类的性能。
pm list libraries //当前设备支持的libs。
pm list package // 显示所有已经安装的包名。
pm list package -f //也显示associated文件所在目录(即保存的APK文件)
补充
偶然发现也有人在解决这个问题, 也可以参考这个。 http://blog.csdn.net/cgxzll/article/details/55224747