startLockTask() without request user action

被刻印的时光 ゝ 提交于 2021-02-07 19:44:30

问题


I want to activate startLockTask() without any request for users. So at first i read this Device Administration and create small sample application. In result i have my application in device administrators list but when i call startLockTask() it still execute not in silent mode. When i'm trying to call it so:

    DevicePolicyManager myDevicePolicyManager = (DevicePolicyManager) getSystemService(Context.DEVICE_POLICY_SERVICE);
    String[] packages = {this.getPackageName()};                        
    myDevicePolicyManager.setLockTaskPackages(deviceAdminComponentName, packages);
    startLockTask();

i'm getting next exception :java.lang.SecurityException: Admin ComponentInfo{com.hssoft.deviceadmintest/com.hssoft.deviceadmintest.DeviceAdmin} does not own the device

So i have Device administrator but i'm not device owner. What i'm doing wrong, can somebody help? Here is my full app code:

Manifest file:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.hssoft.deviceadmintest">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <receiver
            android:name="com.hssoft.deviceadmintest.DeviceAdmin"
            android:permission="android.permission.BIND_DEVICE_ADMIN">
            <meta-data android:name="android.app.device_admin"
                       android:resource="@xml/device_admin" />
            <intent-filter>
                <action android:name="android.app.action.PROFILE_PROVISIONING_COMPLETE"/>
            </intent-filter>
        </receiver>
    </application>
</manifest>

xml/device_admin.xml:

<?xml version="1.0" encoding="utf-8"?>
<device-admin>
    <uses-policies>
        <limit-password/>
        <watch-login/>
        <reset-password/>
        <force-lock/>
        <wipe-data/>
        <expire-password/>
        <encrypted-storage/>
        <disable-camera/>
    </uses-policies>
</device-admin>

DeviceAdmin.java:

package com.hssoft.deviceadmintest;

import android.app.admin.DeviceAdminReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

public class DeviceAdmin extends DeviceAdminReceiver {

    @Override
    public void onEnabled(Context context, Intent intent) {
        super.onEnabled(context, intent);
        Log.i("Device Admin: ", "Enabled");
    }

    @Override
    public String onDisableRequested(Context context, Intent intent) {
        return "Admin disable requested";
    }

    @Override
    public void onDisabled(Context context, Intent intent) {
        super.onDisabled(context, intent);
        Log.i("Device Admin: ", "Disabled");
    }

    @Override
    public void onPasswordChanged(Context context, Intent intent) {
        super.onPasswordChanged(context, intent);
        Log.i("Device Admin: ", "Password changed");
    }
}

MainActivity.java:

package com.hssoft.deviceadmintest;

import android.app.Activity;
import android.app.admin.DevicePolicyManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;

public class MainActivity extends AppCompatActivity {

    static final int ACTIVATION_REQUEST = 1;
    ComponentName deviceAdminComponentName;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Intent intent = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
        deviceAdminComponentName = new ComponentName(this, DeviceAdmin.class);
        intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, deviceAdminComponentName);

        startActivityForResult(intent, ACTIVATION_REQUEST);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (requestCode) {
            case ACTIVATION_REQUEST:
                if (resultCode == Activity.RESULT_OK) {
                    Log.i("MainActivity", "Administration enabled!");

                    DevicePolicyManager myDevicePolicyManager = (DevicePolicyManager) getSystemService(Context.DEVICE_POLICY_SERVICE);
                    String mPackageName = this.getPackageName();
                    if (myDevicePolicyManager.isDeviceOwnerApp(mPackageName)) {
                        myDevicePolicyManager.setLockTaskPackages(deviceAdminComponentName, new String[]{mPackageName});
                    }

                    if (myDevicePolicyManager.isLockTaskPermitted(mPackageName)) {
                        startLockTask();
                    }
                } else {
                    Log.i("MainActivity", "Administration enable FAILED!");
                }

                return;
        }

        super.onActivityResult(requestCode, resultCode, data);
    }
}

回答1:


your application has to be a device owner to behave as you want, if you have root access you can run this command to achieve that

adb shell dpm set-device-owner com.hssoft.deviceadmintest/.DeviceAdmin 

this article will help you alot.



来源:https://stackoverflow.com/questions/38395027/startlocktask-without-request-user-action

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!