23

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

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

找到了这些,但没有帮助:How to install/update/remove APK using "PackageInstaller" class in Android L? Lollipop 上的“PackageInstaller”类是什么,以及如何使用它?

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

提前致谢。

4

2 回答 2

11

这可以从 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中完成设备所有者演示应用程序。

于 2016-05-16T09:57:11.577 回答
4

想通了,代码如下:

    try
    {
        PackageInstaller pi = app.getPackageManager().getPackageInstaller();
        int sessId          = pi.createSession(new PackageInstaller.SessionParams(PackageInstaller.SessionParams.MODE_FULL_INSTALL));

        PackageInstaller.Session session = pi.openSession(sessId);

        // .. write updated APK file to out


        long sizeBytes = 0;
        final File file = new File(filepathApk);
        if (file.isFile())
        {
            sizeBytes = file.length();
        }

        InputStream in = null;
        OutputStream out = null;

        in = new FileInputStream(filepathApk);
        out = session.openWrite("my_app_session", 0, sizeBytes);

        int total = 0;
        byte[] buffer = new byte[65536];
        int c;
        while ((c = in.read(buffer)) != -1)
        {
            total += c;
            out.write(buffer, 0, c);
        }
        session.fsync(out);
        in.close();
        out.close();

        System.out.println("InstallApkViaPackageInstaller - Success: streamed apk " + total + " bytes");

        // fake intent
        Context app = this;
        Intent intent = new Intent(app, AlarmReceiver.class);
        PendingIntent alarmtest = PendingIntent.getBroadcast(app,
                1337111117, intent, PendingIntent.FLAG_UPDATE_CURRENT);

        session.commit(alarmtest.getIntentSender());
        session.close();

    }
    catch (Exception ex)
    {
        ex.printStackTrace();
    }
于 2015-09-21T05:33:56.683 回答