Android 12 设备上的设备所有者二维码停止工作,出现“无法设置设备”消息。

3

由于某些原因,将我们的应用程序作为设备所有者安装的QR码在Android 12设备上停止工作(相同的QR码在先前的Android版本上完美运行)。

我们收到的错误消息是:

Can't set up device
Contact your IT admin for help

这是QR码的JSON。
{
  "android.app.extra.PROVISIONING_DEVICE_ADMIN_PACKAGE_DOWNLOAD_LOCATION": "https://someurlthatworkforsure",
  "android.app.extra.PROVISIONING_SKIP_ENCRYPTION": true,
  "android.app.extra.PROVISIONING_DEVICE_ADMIN_PACKAGE_CHECKSUM": "validCheckSumThatWasTestedAndDoesWorkOnAndroidPriodTo12",
  "android.app.extra.PROVISIONING_DEVICE_ADMIN_COMPONENT_NAME": "com.brand.name/com.brand.some.SomeClass"
}

我已经进行了一些搜索,但没有找到我们的QR码有什么问题,

我也尝试将PROVISIONING_SKIP_ENCRYPTION设置为false并将其删除,结果相同。

CHECKSUM是有效的,如果我搞乱它会抛出不同的错误...

提前感谢。


1
也许可以尝试访问 https://dev59.com/WMLra4cB1Zd3GeqPTNtm。 - ToolmakerSteve
1
@AlessandroCaliaro 是的,我会在这里发布它。 - Daniel
3
嗨 @Daniel,你能发表你的解决方案吗? - Miguel Sesma
1
@AlessandroCaliaro 我刚刚发布了完整的解决方案。 - Daniel
2
@MiguelSesma 我刚刚发布了完整的解决方案。 - Daniel
显示剩余2条评论
1个回答

6

好的,我最终以以下方式解决了它

你必须添加另外两个活动来处理新的流程,在这两个活动中,你需要设置结果并完成该活动

setResult(RESULT_OK, intent);
finish();

请注意,我注意到新的方法在安卓12(v31)上可以完美运行,但有时会在安卓11(v30)上失败。因此我对这个解决方案进行了条件限制,只在安卓12+环境下启用。我通过检查以下布尔值provision_mode_compliance_enabled来实现,该值存储在XML资源文件中。
ProvisioningModeActivity.java
package com.my.pkg;

import static android.app.admin.DevicePolicyManager.EXTRA_PROVISIONING_ADMIN_EXTRAS_BUNDLE;

import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.os.PersistableBundle;

import androidx.appcompat.app.AppCompatActivity;
import com.my.pkg.R;

import java.util.List;

public class ProvisioningModeActivity extends AppCompatActivity {

    private String EXTRA_PROVISIONING_ALLOWED_PROVISIONING_MODES = "android.app.extra.PROVISIONING_ALLOWED_PROVISIONING_MODES";
    private int PROVISIONING_MODE_FULLY_MANAGED_DEVICE = 1;
    private int PROVISIONING_MODE_MANAGED_PROFILE = 2;
    private String EXTRA_PROVISIONING_MODE = "android.app.extra.PROVISIONING_MODE";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_get_provisioning_mode);

        Intent intent = getIntent();
        int provisioningMode = PROVISIONING_MODE_FULLY_MANAGED_DEVICE;
        List<Integer> allowedProvisioningModes = intent.getIntegerArrayListExtra(EXTRA_PROVISIONING_ALLOWED_PROVISIONING_MODES);

        if (allowedProvisioningModes != null) {
            if (allowedProvisioningModes.contains(PROVISIONING_MODE_FULLY_MANAGED_DEVICE)) {
                provisioningMode = PROVISIONING_MODE_FULLY_MANAGED_DEVICE;
            } else if (allowedProvisioningModes.contains(PROVISIONING_MODE_MANAGED_PROFILE)) {
                provisioningMode = PROVISIONING_MODE_MANAGED_PROFILE;
            }
        }
        //grab the extras (might contain some needed values from QR code) and pass to AdminPolicyComplianceActivity
        PersistableBundle extras = intent.getParcelableExtra(EXTRA_PROVISIONING_ADMIN_EXTRAS_BUNDLE);
        Intent resultIntent = getIntent();

        if (extras != null) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                resultIntent.putExtra(EXTRA_PROVISIONING_ADMIN_EXTRAS_BUNDLE, extras);
            }
        }
        resultIntent.putExtra(EXTRA_PROVISIONING_MODE, provisioningMode);

        setResult(RESULT_OK, resultIntent);
        finish();
    }
}

and

AdminPolicyComplianceActivity.java

package com.my.pkg;


import android.content.Intent;
import android.os.Bundle;
import com.my.pkg.R;
import androidx.appcompat.app.AppCompatActivity;



public class AdminPolicyComplianceActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_policy_compliance);

        Intent intent = getIntent();

        setResult(RESULT_OK, intent);
        finish();
    }
}

清单条目:请注意,这些活动是有条件启用的(请继续阅读答案直到结束)。

<activity
    android:name="com.communitake.android.lib.deviceadmin.AdminPolicyComplianceActivity"
    android:exported="true"
    android:enabled="@bool/provision_mode_compliance_enabled"
    android:screenOrientation="portrait"
    android:theme="@style/Theme.AppCompat"
    android:permission="android.permission.BIND_DEVICE_ADMIN">
    <intent-filter>
        <action android:name="android.app.action.ADMIN_POLICY_COMPLIANCE"/>
        <category android:name="android.intent.category.DEFAULT" />
    </intent-filter>
</activity>

<activity
    android:name="com.communitake.android.lib.deviceadmin.ProvisioningModeActivity"
    android:screenOrientation="portrait"
    android:exported="true"
    android:enabled="@bool/provision_mode_compliance_enabled"
    android:theme="@style/Theme.AppCompat"
    android:permission="android.permission.BIND_DEVICE_ADMIN">
    <intent-filter>
        <action android:name="android.app.action.GET_PROVISIONING_MODE" />
        <category android:name="android.intent.category.DEFAULT" />
    </intent-filter>
</activity>

activity_get_provisioning_mode.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".GetProvisioningModeActivity">

    <Button
        android:id="@+id/get_provisioning_data"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Get Provisioning Data"/>

</LinearLayout>

activity_policy_compliance.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".PolicyComplianceActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Policy Compliance Screen"/>

</LinearLayout>

最后一件事是将 bools.xml 添加到 valuesvalues-v31 文件夹中,内容如下,注意新的流程更好地启用于 Android 12 及更高版本。

values 文件夹

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <bool name="provision_mode_compliance_enabled">false</bool>
</resources>

values-v31 文件夹

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <bool name="provision_mode_compliance_enabled">true</bool>
</resources>

2
谢谢@Daniel。运行得非常好。 只是评论一下,在活动中没有必要使用setContent行,因为我们不显示任何内容。我们也可以避免布局文件。 - Miguel Sesma
1
你说得没错,但是如果有人想要为用户提供设备所有者/配置文件所有者的选择,那么这将非常有用。 - Daniel

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接