gpt4 book ai didi

silent-installer - PackageInstaller "Silent install and uninstall of apps by Device Owner” - Android M 预览版

转载 作者:行者123 更新时间:2023-12-02 01:20:59 34 4
gpt4 key购买 nike

PackageInstaller ( https://developer.android.com/reference/android/content/pm/PackageInstaller.html ) 似乎是从 API 21 (Lollipop) 开始添加的,但是我还没有找到任何关于如何通过此 API 安装 APK 的可靠代码示例。任何代码帮助将不胜感激。

我正在研究适用于 Android M Preview 的 COSU/Kiosk 应用,并尝试通过 PackageInstaller API 实现“设备所有者静默安装和卸载应用”( https://developer.android.com/preview/api-overview.html#afw ) 新功能。

找到了这些,但没有帮助:How to install/update/remove APK using "PackageInstaller" class in Android L? What's "PackageInstaller" class on Lollipop, and how to use it?

也没有找到任何 Android 示例应用程序。

提前致谢。

最佳答案

从 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());

完整的设备所有者演示应用程序在此 repo .

关于silent-installer - PackageInstaller "Silent install and uninstall of apps by Device Owner” - Android M 预览版,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32473158/

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