问题
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).
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.
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();
}
}
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)
{
}
}
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?
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
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>
Any suggestions would be greatly appreciated. Even a sample anywhere that has a functioning DeviceAdminReceiver done in Xamarin.Android is OK.
回答1:
I found the cause, and I am posting it here, in case anyone else has similar issues.
The problem is that Visual Studio was generating an AndroidManifest that had md5 namespaces for the Activity and Receiver (android:name="md5e47a6bbf1a64ae14eb7c553dec4a7b66.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")]
) .
After doing this, the AndroidManifest has the correct names for my classes, and everything worked.
Have a nice day.
来源:https://stackoverflow.com/questions/43679322/xamarin-monoandroid-deviceadmin