我正在尝试在具有完整信息亭模式的 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