使用设备策略控制器

时间:2017-10-30 16:54:37

标签: android device-policy-manager android-6.0.1-marshmallow

我有一个工作的DPC应用程序,它是设备所有者。我在两个不同的Android 6.0.1设备上尝试过此操作,以排除任何设备/制造商问题。

我使用adb shell dpm set-device-owner com.example.dpc/.DpcDeviceAdminReceiver让我的DPC-app成为所有者。在将其作为所有者之后,它可以正确地将COSU权限授予另一个应用程序,这使我确信这已经奏效。该命令返回响应:

Success: Device owner set to package com.example.dpc
Active admin set to component {com.examplem.dpc/com.example.dpc.DpcDeviceAdminReceiver}

我想使用此应用安装和升级其他应用,无需用户干预(例如Google Play)。

我正在使用以下概念证明代码:

void upgrade() {
    String apkFileName = "app_debug_2_0_0";

    PackageManager packageManger = getPackageManager();
    PackageInstaller packageInstaller = packageManger.getPackageInstaller();
    PackageInstaller.SessionParams params = new PackageInstaller.SessionParams(
            PackageInstaller.SessionParams.MODE_FULL_INSTALL);
    params.setAppPackageName("com.example.dummy");
    try {
        Log.e(TAG, "apkFileName " + apkFileName);

        InputStream ins = getResources().openRawResource(
                getResources().getIdentifier(apkFileName,
                        "raw", getPackageName()));

        int sessionId = packageInstaller.createSession(params);
        PackageInstaller.Session session = packageInstaller.openSession(sessionId);
        OutputStream out = session.openWrite(apkFileName, 0, -1);

        final int bufsize = 4096;
        byte[] bytes = new byte[bufsize];

        int len = 1; // any value > 0
        int tot = 0;
        while (len > 0) {
            len = ins.read(bytes, 0, bufsize);
            Log.d(TAG, "len: " + len);
            if (len < 1) break;
            out.write(bytes, 0, len);
            tot += len;
            Log.d(TAG, "copied: " + tot);
        }
        ins.close();

        session.fsync(out);
        out.close();

        Log.e(TAG, "about to commit ");
        session.commit(PendingIntent.getBroadcast(this, sessionId,
                new Intent("com.example.dpc.intent.UPDATE"), 0).getIntentSender());
        Log.e(TAG, "committed ");
    } catch (IOException e) {
        Log.e(TAG, "Error installing package " + apkFileName, e);
    }
}

使用上面的代码和变体,我收到一个包含错误的com.example.dpc.intent.UPDATE意图:

Intent { 
    act=com.example.dpc.intent.UPDATE 
    flg=0x10 
    cmp=com.example.dpc/.DpcUpdateReceiver 
    bqHint=4 
    (has extras) 
}
Bundle[
    android.content.pm.extra.STATUS=4, 
    android.content.pm.extra.PACKAGE_NAME=com.example.dummy,
    android.content.pm.extra.SESSION_ID=1055214117, 
    android.content.pm.extra.LEGACY_STATUS=-15, 
    android.content.pm.extra.STATUS_MESSAGE=INSTALL_FAILED_TEST_ONLY: installPackageLI
]

Logcat正确报告正在流入apk流的session.openWrite的大小。

我已经看过了:

我做错了什么?

1 个答案:

答案 0 :(得分:0)

错误来自frameworks/base/services/core/java/com/android/server/pm/PackageManagerService.java中的以下Android 6.0.1代码。

if ((pkg.applicationInfo.flags&ApplicationInfo.FLAG_TEST_ONLY) != 0) {
    if ((installFlags & PackageManager.INSTALL_ALLOW_TEST) == 0) {
        res.setError(INSTALL_FAILED_TEST_ONLY, "installPackageLI");
        return;
    }
}

我检查了我尝试安装的应用,发现FLAG_TEST_ONLY位已设置。 Why is Android Studio 3.0.0 setting FLAG_TEST_ONLY on APKs?