其他分享
首页 > 其他分享> > Xamarin | Android | COSU错误

Xamarin | Android | COSU错误

作者:互联网

我正在尝试创建一个COSU设备.它将是我公司销售的硬件,它运行由我们开发的单个应用程序.我已经浏览了许多教程,试图为此设置一个概念验证.

我目前有以下内容.

在我的AndroidManaifest.xml中

然后……

<application android:allowBackup="true" android:label="@string/app_name" android:keepScreenOn="true" >
    <receiver   android:name="DeviceAdmin"
                android:label="@string/sample_device_admin"
                android:description="@string/sample_device_admin_description"
                android:permission="android.permission.BIND_DEVICE_ADMIN">
        <intent-filter>
            <action android:name="android.app.action.DEVICE_ADMIN_ENABLED" />
        </intent-filter>
        <device-admin>
            <uses-policies>
                <limit-password />
                <watch-login />
                <reset-password />
                <force-lock />
                <wipe-data />
            </uses-policies>
        </device-admin>
    </receiver>
    <uses-permission android:name="android.permission.MANAGE_DEVICE_ADMINS" />
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.HOME" />
        <category android:name="android.intent.category.DEFAULT" />
    </intent-filter>
</application>

在device_admin_receiver.xml中

<?xml version="1.0" encoding="utf-8"?>
<device-admin xmlns:android="http://schemas.android.com/apk/res/android">
    <uses-policies>
        <limit-password />
        <watch-login />
        <reset-password />
        <force-lock />
        <wipe-data />
    </uses-policies>
</device-admin>

DeviceAdmin.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Android.App;
using Android.App.Admin;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;

namespace CB.App
{
    public class DeviceAdmin : DeviceAdminReceiver
    {

    }
}

最后是MainActivity.cs的OnCreate

protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);
        Window.DecorView.SystemUiVisibility = (StatusBarVisibility)flags;

        // Setup the collection and register implementations
        IServiceCollection coll = new ServiceCollection();
        RegisterDependencies(coll);

        // Build the global services
        ServiceRegistrationHelper.RegisterCoreServices(coll);

        // Register the provide with the GlobalServices
        GlobalServices.RegisterServiceProvider(coll.BuildServiceProvider(new ServiceProviderOptions()));

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

        DevicePolicyManager myDevicePolicyManager = (DevicePolicyManager)this.GetSystemService(Activity.DevicePolicyService);
        // get this app package name
        ComponentName mDPM = new ComponentName(this, typeof(DeviceAdmin).Name);

        if( myDevicePolicyManager.IsDeviceOwnerApp(this.PackageName))
        {
            //myDevicePolicyManager.ClearDeviceOwnerApp(this.PackageName);

            String[] packages = { this.PackageName };
            myDevicePolicyManager.SetLockTaskPackages(mDPM, packages);
            StartLockTask();
            SetUpClock();
            DisplayHome();
        }
        else
        {
            Toast.MakeText(this.ApplicationContext, "Not Owner", ToastLength.Long).Show();
        }
    }

在ADB Shell中,我运行命令dpm set-device-owner com.companyname.productname / .DeviceAdmin并收到Success:设备所有者设置为package componentinfo {com.companyname.productname / com.companyname.productname.DeviceAdmin} Active admin set组件{com.companyname.productname / com.companyname.productename.DeviceAdmin}

它构建和部署但是当它到达生产线时

myDevicePolicyManager.SetLockTaskPackages(mDPM, packages);

它抛出错误Java.Lang.SecurityException:没有活动的admin组件信息(com.lathem.cumberland / DeviceAdmin)

我错过了什么?

解决方法:

删除< device-admin>来自接收器的部分,应通过单独的xml资源引用,或者通过硬编码接收器清单部分中的元数据部分或通过类属性:

[BroadcastReceiver(
    Name = "com.sushihangover.cosu.DeviceAdminReceiver", 
    Label = "StackOverflow",
    Permission = "android.permission.BIND_DEVICE_ADMIN",
    Exported = true
)]
[MetaData("android.app.device_admin", Resource = "@xml/device_admin_receiver")]
[IntentFilter(new[] { 
    "android.intent.action.DEVICE_ADMIN_ENABLE", 
    "android.intent.action.PROFILE_PROVISIONING_COMPLETE", 
    "android.intent.action.BOOT_COMPLETED" 
})]
public class MyDeviceAdminReceiver : DeviceAdminReceiver {}

使用Java类名,而不是C#名:

ComponentName mDPM = new ComponentName(this, Java.Lang.Class.FromType(typeof(DeviceAdminReceiver)));

注意:根据我对DeviceAdminReceiver子类的Java名称的使用,dpm set-device-owner将是:

adb shell dpm set-device-owner com.sushihangover.cosu/com.sushihangover.cosu.DeviceAdminReceiver

标签:android,xamarin,xamarin-android,cosu
来源: https://codeday.me/bug/20190701/1346552.html