2016-04-28 42 views
6

Sto cercando di appuntare schermata di programmazione su Android L. mia app include file XML conbandiera Perchè isLockTaskPermitted è falso

<?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> 

ho installato app con linee corrette nel file AndroidManifest dove ho definito AdminReceiver. Tutto ha funzionato bene qualche settimana fa, ma al momento quando sono tornato a questo progetto - L'app mi chiede di confermare il popup "Pin screen" ogni volta che l'app inizia.

quando sto debug ho notato un comportamento strano devicePolicyManager.isAdminActive(componentName) vale devicePolicyManager.isDeviceOwnerApp(getPackageName()) vale

ma

devicePolicyManager.isLockTaskPermitted(getPackageName()) è falso

Non importa se ho impostato app come proprietario del dispositivo utilizzando adb raccomandare dpm set-device-owner ... o spingendo device_owner.xml file in/data/system/

La mia domanda è perché app non ha il permesso di bloccare lo schermo?

risposta

2

Per questo è necessario chiamare setLockTaskPackages()

codice Si dovrebbe qualcosa di simile qui di seguito.

tua attività deve avere

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

// 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

<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>