0

我正在尝试在具有完整信息亭模式的 android 模拟器上运行应用程序。所以我写了一个DeviceAdminReceiver:

public class AdminReceiver : DeviceAdminReceiver
{

    public override void OnEnabled(Context context, Intent intent)
    {
        base.OnEnabled(context, intent);
        Log.Info("Device Admin: ", "Enabled");
    }

    public override void OnDisabled(Context context, Intent intent)
    {
        base.OnDisabled(context, intent);
        Log.Info("Device Admin: ", "Disabled");
    }
}

这是主要活动:

[Activity(Theme = "@style/Maui.SplashTheme", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation | ConfigChanges.UiMode | ConfigChanges.ScreenLayout | ConfigChanges.SmallestScreenSize)]
[IntentFilter(new[] { Intent.ActionMain }, Categories = new[] { Intent.CategoryHome, Intent.CategoryDefault })]
public class MainActivity : MauiAppCompatActivity
{
    protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);
        SetFullscreenFlags();
        var devicePolicyManager = (DevicePolicyManager)GetSystemService(Context.DevicePolicyService);
        var deviceAdminComponent = new ComponentName(this, Java.Lang.Class.FromType(typeof(AdminReceiver)));
        var intent = new Intent(DevicePolicyManager.ActionAddDeviceAdmin);
        intent.PutExtra(DevicePolicyManager.ExtraDeviceAdmin, deviceAdminComponent);
        intent.PutExtra(DevicePolicyManager.ExtraAddExplanation, "Device administrator");
        StartActivity(intent);
        
        devicePolicyManager.SetLockTaskPackages(deviceAdminComponent, new[] { PackageName });
        StartLockTask();
       
    }

private void SetFullscreenFlags()
    {
        Window.SetFlags(WindowManagerFlags.Fullscreen, WindowManagerFlags.Fullscreen);

        if (Build.VERSION.SdkInt >= BuildVersionCodes.R)
        {
            Window.SetDecorFitsSystemWindows(true);
            var controller = Window.InsetsController;
            if (controller is not null)
            {
                controller.Hide(WindowInsets.Type.StatusBars() | WindowInsets.Type.NavigationBars());
            }
        }
        else
        {
            Window.DecorView.SystemUiVisibility = (StatusBarVisibility)
                                     (SystemUiFlags.LowProfile
                                     | SystemUiFlags.Fullscreen
                                     | SystemUiFlags.LayoutFullscreen
                                     | SystemUiFlags.HideNavigation
                                     | SystemUiFlags.LayoutHideNavigation
                                     | SystemUiFlags.Immersive
                                     | SystemUiFlags.ImmersiveSticky);
        }
    }

最后是 AndroidManifest.xml:

<uses-sdk android:minSdkVersion="21" android:targetSdkVersion="30" />
<application android:allowBackup="true" android:icon="@mipmap/appicon" android:roundIcon="@mipmap/appicon_round" android:supportsRtl="true">
    <receiver
        android:name=".AdminReceiver"
        android:permission="android.permission.BIND_DEVICE_ADMIN">
        <meta-data
            android:name="android.app.device_admin"
            android:resource="@xml/device_admin" />
        <intent-filter>
            <action android:name="android.app.action.DEVICE_ADMIN_ENABLED" />
            <action android:name="android.app.action.DEVICE_ADMIN_DISABLED" />
        </intent-filter>
    </receiver>
    <receiver android:name=".BootReceiver">
        <intent-filter>
            <action android:name="android.intent.action.PROFILE_PROVISIONING_COMPLETE"/>
            <action android:name="android.intent.action.BOOT_COMPLETED" />
        </intent-filter>
    </receiver>
</application>
>

问题是 MainActivity 中的这一行抛出了这个异常:

devicePolicyManager.SetLockTaskPackages(deviceAdminComponent, new[] { PackageName });

Java.Lang.SecurityException: 'No active admin ComponentInfo{com.companyName.TestApp/crc6463b464086cdcaa12.AdminReceiver}'

我在 resources/xml 中已经有一个带有 device-admin 标签的 device_admin.xml,是否需要更多配置?或者问题出在哪里?或者我正在实例化两个不同的管理员接收器?

注意:我已经完成了允许此应用程序在模拟器上作为设备管理员的程序,并通过 android debug bridge 将其设置为设备所有者运行命令:

dpm set-device-owner com.companyName.TestApp/.AdminReceiver
4

1 回答 1

1

您能否确保您实际上是BroadcastReceiver在类上注册您的属性,而不是手动修改 AndroidManifest.xml。我认为让你失望的是,如果你不注册接收器,它将被命名为<md5 sum>.AdminReceiver. 但是,当您.AdminReceiver在 AndroidManifest.xml 中添加接收方时,Android 会将其膨胀为<package name>.AdminReciever. 所以你的 AdminReceiver 不会被调用。

所以尝试类似:

[BroadcastReceiver(
    Name = "your.packagename.AdminReceiver",
    Description = "@string/device_owner_description",
    Label = "My Device Admin Receiver",
    Permission = Manifest.Permission.BindDeviceAdmin)]
[MetaData("android.app.device_admin", Resource = "@xml/device_admin")]
public class AdminReceiver : DeviceAdminReceiver
{
}

然后,您应该删除清单中的条目。

如果您需要过滤特定的意图,您也可以添加[IntentFilter]属性来添加它们,但设备管理员不需要这样做。

于 2021-09-02T17:42:55.053 回答