静默更新设备所有者托管的Android应用程序(在自助服务终端模式下)

时间:2017-11-10 13:39:12

标签: android kiosk-mode device-policy-manager

我创建了一个设备所有者应用。它可以将已经安装的应用程序转换为Kiosk模式,我试图远程更新自助服务终端应用程序,而不是从Playstore更新,但它没有得到更新。有人可以帮我指点一下吗?

1 个答案:

答案 0 :(得分:1)

您必须在您的服务器上托管您的apk,然后根据您指定的任何条件下载它。然后,您可以使用此代码更新您的应用。

public static boolean InstallAPK(Context context, String apk_file_name) {
        try {

            File apkfile = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + apk_file_name);
            if (apkfile.exists()) {

                FileInputStream in = new FileInputStream(apkfile);

                PackageInstaller packageInstaller = context.getPackageManager().getPackageInstaller();
                PackageInstaller.SessionParams params = new PackageInstaller.SessionParams(
                        PackageInstaller.SessionParams.MODE_FULL_INSTALL);
                params.setAppPackageName(context.getPackageName());
                // 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;
            } else
                return false;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

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