【问题标题】:Xamarin | Android | COSU ErrorXamarin |安卓 | COSU 错误
【发布时间】:2019-01-20 11:40:18
【问题描述】:

我正在尝试创建 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: Device owner set to package componentinfo{com.companyname.productname/com.companyname.productname.DeviceAdmin}活动管理员设置为组件 {com.companyname.productname/com.companyname.productename.DeviceAdmin}

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

myDevicePolicyManager.SetLockTaskPackages(mDPM, packages);

它抛出错误 Java.Lang.SecurityException: No active admin ComponentInfo(com.lathem.cumberland/DeviceAdmin

我错过了什么?

【问题讨论】:

    标签: android xamarin xamarin.android cosu


    【解决方案1】:

    从您的 receiver 中删除 &lt;device-admin&gt; 部分,这应该通过单独的 xml 资源引用,或者通过在接收器的清单部分中硬编码 meta-data 部分或通过类属性:

    [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
    

    【讨论】:

    • 我原来有Java.Lang.Class.FromType,但是当事情不起作用时改变了它,我原来在接收器内也没有部分。但是,我最初没有类属性,并且使用 com.companyname.productname/.DeviceAdmin 设置了设备所有者,当我添加属性并将 adb 命令更改为使用 com.companyname.productname/com.companyname.productname .DeviceAdmin 问题已得到纠正,并且有效!非常感谢!
    • 我是 Xamarin.Android 开发新手。那里有完整的 COSU Xamarin.Android 示例吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-21
    • 2015-02-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多