无法以编程方式在Android Nougat中安装应用程序

时间:2017-05-23 07:19:05

标签: java android apk android-package-managers

我一直试图让Android系统应用程序自动安装apk。它在Marshmallow工作得很好。

我的应用程序是平台签名和android.uid.system

以下是代码:

private 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);
    logg("extra: " + Intent.EXTRA_REFERRER);
    params.setReferrerUri(Uri.parse(Intent.EXTRA_REFERRER));
    params.setOriginatingUri(Uri.parse(Intent.EXTRA_ORIGINATING_URI));
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
        params.setOriginatingUid(1000);
    }
    // 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();
}

0 个答案:

没有答案