首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为什么isLockTaskPermitted标志是假的

为什么isLockTaskPermitted标志是假的
EN

Stack Overflow用户
提问于 2016-04-28 07:56:46
回答 1查看 2.7K关注 0票数 10

我试图以编程方式将屏幕固定在Android上。我的应用程序包含了

代码语言:javascript
复制
<?xml version="1.0" encoding="utf-8"?>
<device-admin xmlns:android="http://schemas.android.com/apk/res/android">
    <uses-policies>
        <force-lock/>
        <watch-login/>
        <disable-camera/>
        <disable-keyguard-features/>
        <encrypted-storage/>
        <expire-password/>
        <limit-password/>
        <reset-password/>
        <set-global-proxy/>
        <wipe-data/>
    </uses-policies>
</device-admin>

我在AndroidManifest文件中安装了带有正确行的应用程序,其中定义了AdminReceiver。几周前一切都很好,但现在当我回到这个项目时,应用程序要求我确认每次应用程序启动时都会弹出"Pin屏幕“。

在调试时,我注意到奇怪的行为,devicePolicyManager.isAdminActive(componentName)是真,devicePolicyManager.isDeviceOwnerApp(getPackageName())是真

devicePolicyManager.isLockTaskPermitted(getPackageName())是假的

无论我是否将app设置为设备所有者,使用亚行推荐dpm set-device-owner ...,还是将device_owner.xml文件推到/data/system/

我的问题是,为什么应用程序没有锁定屏幕的权限?

EN

回答 1

Stack Overflow用户

发布于 2016-08-16 15:44:06

为此,您必须调用setLockTaskPackages()

你的代码应该如下所示。

您的活动应该有

代码语言:javascript
复制
ComponentName mAdminComponentName = DeviceAdminReceiver.getComponentName(this);
//where this will be your Activity

DevicePolicyManager mDevicePolicyManager = (DevicePolicyManager)
                getSystemService(Context.DEVICE_POLICY_SERVICE);

mDevicePolicyManager.setLockTaskPackages(mAdminComponentName,new String[]{getPackageName()});

DeviceAdminReceiver.java

代码语言:javascript
复制
// Copyright 2016 Google Inc.
// 
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// 
//      http://www.apache.org/licenses/LICENSE-2.0
// 
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package com.google.codelabs.cosu;

/**
 * Created by nyfuchs on 4/26/16.
 */

import android.content.ComponentName;
import android.content.Context;

/**
 * Handles events related to the managed profile.
 */
public class DeviceAdminReceiver extends android.app.admin.DeviceAdminReceiver {
    private static final String TAG = "DeviceAdminReceiver";

    /**
     * @param context The context of the application.
     * @return The component name of this component in the given context.
     */
    public static ComponentName getComponentName(Context context) {
        return new ComponentName(context.getApplicationContext(), DeviceAdminReceiver.class);
    }
}

AndroidManifest.xml

代码语言:javascript
复制
    <receiver
        android:name="com.google.codelabs.cosu.DeviceAdminReceiver"
        android:description="@string/app_name"
        android:label="@string/app_name"
        android:permission="android.permission.BIND_DEVICE_ADMIN">
        <meta-data
            android:name="android.app.device_admin"
            android:resource="@xml/device_admin_receiver" />
        <intent-filter>
            <action android:name="android.intent.action.DEVICE_ADMIN_ENABLED"/>
            <action android:name="android.intent.action.PROFILE_PROVISIONING_COMPLETE"/>
            <action android:name="android.intent.action.BOOT_COMPLETED"/>
        </intent-filter>
    </receiver>
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36908595

复制
相关文章

相似问题

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