如何使用PackageInstaller for Profile Owner安装应用程序?

时间:2019-07-02 11:34:13

标签: android android-studio android-enterprise android-enterprise-features

我正在尝试使用Package Installer安装apk文件。我的应用是当前个人资料所有者。该方法成功完成,但是在我的BroadcastReceiver中,我正在获取PackageInstaller.STATUS_PENDING_USER_ACTION。根据android docs https://developer.android.com/reference/android/content/pm/PackageInstaller应用,如果它是个人资料所有者,则应该能够以静默方式安装apk。

     public static boolean installPackage(Context context, InputStream 
   in, 
       String packageName)
        throws IOException {
      final PackageInstaller packageInstaller = 
    context.getPackageManager().getPackageInstaller();
    final PackageInstaller.SessionParams params = new 
     PackageInstaller.SessionParams(
            PackageInstaller.SessionParams.MODE_FULL_INSTALL);
    params.setAppPackageName(packageName);
    // set params
    final int sessionId = packageInstaller.createSession(params);
    final PackageInstaller.Session session = 
   packageInstaller.openSession(sessionId);
    final OutputStream out = session.openWrite("TestDPC", 0, -1);
    final 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(createInstallIntentSender(context, sessionId));
       return true;
    }

1 个答案:

答案 0 :(得分:0)

必须将您的应用设置为设备所有者或关联的配置文件所有者。