Android Lollipop成为设备管理员不起作用 [英] Android Lollipop becoming device admininistrator doesn't work

查看:96
本文介绍了Android Lollipop成为设备管理员不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在按照本指南进行操作我的应用程序请求成为设备管理员。

可以在任何手机的任何4.x android版本上正常运行,但不适用于Lollipop(我曾在Nexus 5和Nexus 5上尝试过7)。

I'm following this guide on how to have my app request to become a device administrator.
This works great on any 4.x android versions with any phone, but doesn't work on Lollipop (I've tried on a Nexus 5 and on a Nexus 7).

Logcat鞋子出现以下错误:

Logcat shoes the following error:

 *11-02 07:37:09.649: W/DeviceAdminAdd(10020): Cannot start ADD_DEVICE_ADMIN as a new task*  

还有其他人遇到过棒棒糖这个问题吗?

我遍历了指南的所有部分,并确保我的XML,清单,接收器等都应有的状态(再说一次,它确实适用于任何

Has anyone else come across this problem with Lollipop? What can be the cause?
I've gone over all parts of the guide and made sure my XML, menifest, receiver, etc. are all as they should be (and again, it does work on any other version of OS or phone I've tried).

如果有问题,出于兼容性原因,我将使用目标API = 17和最低API = 14编译我的应用。

If it matters, I'm compiling my app with target API=17 and min API=14 for compatibility reasons.

添加清单文件:

<?xml version='1.0' encoding='utf-8'?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.my.package"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="14"
        android:targetSdkVersion="17" />

     <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
    <uses-permission android:name="android.permission.WRITE_SYNC_SETTINGS" />
    <uses-permission android:name="com.mycompany.permission.NFProvider_WRITE" />
    <uses-permission android:name="com.mycompany.permission.NFProvider_READ" />
    <uses-permission android:name="android.permission.READ_LOGS" />

    <application
        android:name="com.mycompany.myapp.Application"
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >

        <receiver
            android:name="com.mycompany.myapp.NFAdminReceiver"
            android:permission="android.permission.BIND_DEVICE_ADMIN">
            <meta-data
                android:name="android.app.device_admin"
                android:resource="@xml/admin"/>
            <intent-filter>
                <action android:name="android.app.action.DEVICE_ADMIN_ENABLED"/>
            </intent-filter>
        </receiver>

        <activity
            android:name="com.mycompany.myapp.ConnectMainActivity"
            android:label="@string/title_activity_connect_main"
            android:process="com.mycompany.ui"
            android:launchMode="singleInstance"
            android:screenOrientation="portrait" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>


        <service
            android:name="com.mycompany.myapp.MainService"
            android:enabled="true" />

        <meta-data android:name="applicationVersion" android:value="1.2.0.213" />


    </application>
</manifest>






创建意图的代码:


Code creating the Intent:

ComponentName mComponentName = new ComponentName(activity, NFAdminReceiver.class); 
Intent intent = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);     
intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, mComponentName);
intent.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION, ADMIN_REQUEST_TITLE);
activity.startActivityForResult(intent, ADMIN_INTENT); 

变量活动是ConnectMainActivity的实例

The variable activity is an instance of ConnectMainActivity

推荐答案

似乎是由singleInstance launchMode选项引起的问题,我仍然很想知道为什么它在Lollipop而不是其他版本上引起了问题。

Seems it was the singleInstance launchMode option that caused the problem, I'm still interested to know why it causes the problem on Lollipop and not on other versions.

现在,将launchMode设置为singleTask可以解决此问题,并且似乎仍然与应用程序流程的设计相适应。

For now, setting launchMode to singleTask solves the issue, and seems to still fit with the design of the app's flow.

这篇关于Android Lollipop成为设备管理员不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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