Xamarin MonoAndroid DeviceAdmin [英] Xamarin MonoAndroid DeviceAdmin

查看:122
本文介绍了Xamarin MonoAndroid DeviceAdmin的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Visual Studio 2017来开发Android Single-View应用程序.该应用程序应该是信息亭应用程序.我的目标是在Visual Studio(AVD设备)提供的仿真器上使用API​​ 21.

I am using Visual Studio 2017, to develop an Android Single-View application. The application should be a kiosk app. I am targeting API 21, on an Emulator provided by Visual Studio (AVD Device).

我遇到的问题是我似乎无法制作有效的DeviceAdminReceiver.我使用此链接作为指南: Android信息亭模式 该示例有效,但我的代码无效.该示例适用于Java.

The issue I am having is that I cannot seem to make a valid DeviceAdminReceiver. I used this link as my guide: Android Kiosk Mode The sample works, but my code does not. The sample is for java.

这是我的DeviceAdmiReceiver.cs的方式:

This is how my DeviceAdmiReceiver.cs:

[BroadcastReceiver(Permission = "android.permission.BIND_DEVICE_ADMIN")]
[MetaData("android.app.device_admin", Resource = "@xml/device_admin")]
[IntentFilter(new[] { "android.app.action.DEVICE_ADMIN_ENABLED", Intent.ActionMain})]
public class AdminReceiver : DeviceAdminReceiver
{
    public override void OnEnabled(Context context, Intent intent)
    {
        base.OnEnabled(context, intent);
        Toast.MakeText(context, Resource.String.AdminEnabled, ToastLength.Short).Show();
        Common.BecomeHomeActivity(context);
    }

    public override void OnLockTaskModeEntering(Context context, Intent intent, string pkg)
    {
        Common.ShowToast(context, "[Kiosk Mode enabled]");
    }

    public override void OnLockTaskModeExiting(Context context, Intent intent)
    {
        Common.ShowToast(context, "[Kiosk Mode disabled]");
    }

    public override void OnDisabled(Context context, Intent intent)
    {
        Toast.MakeText(context, Resource.String.AdminDisabled, ToastLength.Short).Show();
    }
}

这是我MainActivity.cs中的代码:

This is the code from my MainActivity.cs:

 protected override void OnCreate(Bundle bundle)
    {
        try
        {
            base.OnCreate(bundle);

            // Set our view from the "main" layout resource
            SetContentView(Resource.Layout.Main);

            _deviceAdmin = new ComponentName(this, Java.Lang.Class.FromType(typeof(AdminReceiver)));
            _devPolicyManager = (DevicePolicyManager)GetSystemService(Context.DevicePolicyService);

            if (!_devPolicyManager.IsAdminActive(_deviceAdmin))
                Common.ShowToast(this, "Admin is not active");

            if (_devPolicyManager.IsDeviceOwnerApp(PackageName))
                _devPolicyManager.SetLockTaskPackages(_deviceAdmin, new string[] { PackageName });
            else
                Common.ShowToast(this, "App is not Device Owner");
            //}
        }
        catch (Exception ex)
        {

        }
    }

我的错误是,当我尝试将应用程序设置为DeviceOwner时,出现错误Error: Unknown admin: ComponentInfo{atp.egg.app/atp.egg.app.AdminReceiver.我猜该错误与AndroidManifest.xml中的内容有关.我认为清单是根据类的属性自动生成的.我错了吗?

The error I have is that when I try to se the App as DeviceOwner, I get an error Error: Unknown admin: ComponentInfo{atp.egg.app/atp.egg.app.AdminReceiver . I guess the error is related to something in the AndroidManifest.xml. I thought that the manifest was auto-generated from the attributes of the classes. Am I wrong?

更新:这是我正在使用的行命令:adb -s model:Android_SDK_built_for_x86 shell dpm set-device-owner atp.egg.app/.AdminReceiver

Update: This is the line command that I am using: adb -s model:Android_SDK_built_for_x86 shell dpm set-device-owner atp.egg.app/.AdminReceiver

这是我的AndroidManifest.xml:

This is my AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" package="atp.egg.app" android:versionCode="1" android:versionName="1.0" android:installLocation="auto">
<uses-sdk android:minSdkVersion="21" android:targetSdkVersion="21" />
<uses-permission android:name="android.permission.BIND_DEVICE_ADMIN" />
<uses-permission android:name="android.permission.INTERNET" />
<application android:label="KioskApp"></application></manifest>

任何建议将不胜感激.甚至可以在Xamarin.Android中完成具有正常DeviceAdminReceiver功能的任何地方的示例.

Any suggestions would be greatly appreciated. Even a sample anywhere that has a functioning DeviceAdminReceiver done in Xamarin.Android is OK.

推荐答案

我找到了原因,并在此发布,以防其他人遇到类似问题.

I found the cause, and I am posting it here, in case anyone else has similar issues.

问题在于Visual Studio正在生成一个AndroidManifest,该AndroidManifest的Activity和Receiver(android:name="md5e47a6bbf1a64ae14eb7c553dec4a7b66.AdminReceiver".

The problem is that Visual Studio was generating an AndroidManifest that had md5 namespaces for the Activity and Receiver (android:name="md5e47a6bbf1a64ae14eb7c553dec4a7b66.AdminReceiver".

这不允许Android OS工具正确地与我的应用一起使用.要解决此问题,我必须将"Name"属性添加到类([BroadcastReceiver(Permission = "android.permission.BIND_DEVICE_ADMIN",Name ="atp.egg.app.AdminReceiver")])中.

This has not allowing Android OS Tools to work with my app correctly. To fix this, I had to add the "Name" attribute to the classes ([BroadcastReceiver(Permission = "android.permission.BIND_DEVICE_ADMIN",Name ="atp.egg.app.AdminReceiver")]) .

完成此操作后,AndroidManifest的类名正确,并且一切正常.

After doing this, the AndroidManifest has the correct names for my classes, and everything worked.

祝你有美好的一天.

这篇关于Xamarin MonoAndroid DeviceAdmin的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆