gpt4 book ai didi

android - 如何在 Android L 中使用 "PackageInstaller"类安装/更新/删除 APK?

转载 作者:IT老高 更新时间:2023-10-28 22:17:21 26 4
gpt4 key购买 nike

请检查下面的类并给我有关如何使用它们的建议 https://developer.android.com/reference/android/content/pm/PackageInstaller.html https://developer.android.com/reference/android/content/pm/PackageInstaller.Session.html

所以请给我一个安装/更新/删除应用程序的示例。新应用程序是否有可能安装在设备配置文件所有者中?

最佳答案

没有来自 Android M onwards 的系统权限是可能的.

if ((mPm.checkUidPermission(android.Manifest.permission.INSTALL_PACKAGES, installerUid)
== PackageManager.PERMISSION_GRANTED)
|| (installerUid == Process.ROOT_UID)
|| mIsInstallerDeviceOwner) {
mPermissionsAccepted = true;
} else {
mPermissionsAccepted = false;
}

设备所有者静默安装和卸载应用程序:

设备所有者现在可以使用 PackageInstaller API 静默安装和卸载应用程序,独立于 Google Play for Work。

More in this link.


这可以从 Android 6.0 及更高版本开始。

  • 让您的应用成为设备所有者。

一旦您的应用获得设备所有者权限,我们就可以静默安装、卸载和更新,无需任何用户干预。

public static boolean installPackage(Context context, InputStream in, String packageName)
throws IOException {
PackageInstaller packageInstaller = context.getPackageManager().getPackageInstaller();
PackageInstaller.SessionParams params = new PackageInstaller.SessionParams(
PackageInstaller.SessionParams.MODE_FULL_INSTALL);
params.setAppPackageName(packageName);
// set params
int sessionId = packageInstaller.createSession(params);
PackageInstaller.Session session = packageInstaller.openSession(sessionId);
OutputStream out = session.openWrite("COSU", 0, -1);
byte[] buffer = new byte[65536];
int c;
while ((c = in.read(buffer)) != -1) {
out.write(buffer, 0, c);
}
session.fsync(out);
in.close();
out.close();

session.commit(createIntentSender(context, sessionId));
return true;
}



private static IntentSender createIntentSender(Context context, int sessionId) {
PendingIntent pendingIntent = PendingIntent.getBroadcast(
context,
sessionId,
new Intent(ACTION_INSTALL_COMPLETE),
0);
return pendingIntent.getIntentSender();
}

卸载:

String appPackage = "com.your.app.package";
Intent intent = new Intent(getActivity(), getActivity().getClass());
PendingIntent sender = PendingIntent.getActivity(getActivity(), 0, intent, 0);
PackageInstaller mPackageInstaller = getActivity().getPackageManager().getPackageInstaller();
mPackageInstaller.uninstall(appPackage, sender.getIntentSender());

Git repo here .

关于android - 如何在 Android L 中使用 "PackageInstaller"类安装/更新/删除 APK?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26884956/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com