首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Xamarin = Android \ COSU错误

Xamarin = Android \ COSU错误
EN

Stack Overflow用户
提问于 2018-08-13 17:14:28
回答 1查看 334关注 0票数 6

我正在尝试创建一个COSU设备。这将是我的公司出售的硬件,运行一个应用程序,由我们开发。我已经看过许多教程,试图为这个概念建立一个证明。

我现在有以下内容。

在我的AndroidManaifest.xml里

然后以后..。

代码语言:javascript
复制
<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中

代码语言:javascript
复制
<?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

代码语言:javascript
复制
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
    {

    }
}

最后是OnCreate of MainActivity.cs

代码语言:javascript
复制
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();
        }
    }

在亚行Shell中,我运行了命令dpm set -设备所有者com.companyname.productname/..DeviceAdmin:设备所有者设置为将componentinfo{com.companyname.productname/com.companyname.productname.DeviceAdmin}活动管理集打包为{com.companyname.productname/com.companyname.productename.DeviceAdmin}组件

它构建和部署,但当它到达线

代码语言:javascript
复制
myDevicePolicyManager.SetLockTaskPackages(mDPM, packages);

它引发错误Java.Lang.SecurityException:无活动管理Java.Lang.SecurityException

我遗漏了什么?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-08-13 19:03:22

<device-admin>中删除receiver部分,这应该通过单独的xml资源引用,或者通过对接收方清单部分中的meta-data部分进行硬编码,或者通过类属性来引用:

代码语言:javascript
复制
[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#名称:

代码语言:javascript
复制
ComponentName mDPM = new ComponentName(this, Java.Lang.Class.FromType(typeof(DeviceAdminReceiver)));

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

代码语言:javascript
复制
adb shell dpm set-device-owner com.sushihangover.cosu/com.sushihangover.cosu.DeviceAdminReceiver
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51827453

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档