如何使用“PackageInstaller"安装/更新/删除APKAndroid L 中的类? [英] How to install/update/remove APK using "PackageInstaller" class in Android L?

查看:29
本文介绍了如何使用“PackageInstaller"安装/更新/删除APKAndroid L 中的类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请检查下面的类&给我如何使用它们的建议https://developer.android.com/reference/android/content/pm/PackageInstaller.htmlhttps://developer.android.com/reference/android/content/pm/PackageInstaller.会话.html

Plz check below classe & give me the suggestion for how to use them https://developer.android.com/reference/android/content/pm/PackageInstaller.html https://developer.android.com/reference/android/content/pm/PackageInstaller.Session.html

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

So please give me an example to install/update/remove app. Can it be possible that the new application will install in device profile owner?

推荐答案

Android M 以上.

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.

A Device Owner can now silently install and uninstall applications using the PackageInstaller APIs, independent of Google Play for Work.

更多内容请访问此链接.

这在 Android 6.0 及更高版本中是可能的.

This is possible from Android 6.0 and up.

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

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

Once your app gets the Device owner permission, we can install, uninstall and update silently without any user intervention.

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 存储库.

这篇关于如何使用“PackageInstaller"安装/更新/删除APKAndroid L 中的类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆