How to provide USES_POLICY_FORCE_LOCK permissions in Android Studio?

3

I have an application that should block the device after a certain time, I found that it can be done with the lockNow() instruction, but when I put it and execute it, I get the following error.

  

E / AndroidRuntime: FATAL EXCEPTION: Thread-7                     Process: com.example.software.appvideos, PID: 11915                     java.lang.SecurityException: Do not activate admin owned by uid 10151 for policy # 3                         at android.os.Parcel.readException (Parcel.java:1704)                         at android.os.Parcel.readException (Parcel.java:1654)                         at android.app.admin.IDevicePolicyManager $ Stub $ Proxy.lockNow (IDevicePolicyManager.java:5262)                         at android.app.admin.DevicePolicyManager.lockNow (DevicePolicyManager.java:2533)                         at com.example.software.appvideos.MainActivity $ 11.run (MainActivity.java:256)

Because of what little I understood when searching, permission is needed to execute the instruction if you could help me with this little problem I would be very grateful. Annex my manifest.

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.software.appvideos">
    <uses-permission android:name="android.permission.USES_POLICY_FORCE_LOCK" />
    <uses-permission-sdk-23 android:name="android.permission.WAKE_LOCK"/>
    <uses-permission android:name="android.permission.WAKE_LOCK"/>
    <application
        android:allowBackup="true"

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

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

And the code where I declare the lockNow ().

DevicePolicyManager  pm = (DevicePolicyManager) this.getSystemService(Context.DEVICE_POLICY_SERVICE);

public void apagar(){
    Thread t = new Thread(){

        public void run() {
            while(true) {
                try {
                    Thread.sleep(10000);
                    pm.lockNow();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    };
    t.start();
}

Thank you in advance.

    
asked by Alfredo 17.09.2018 в 23:42
source

1 answer

2

Actually pm.lockNow() will not be allowed without first inhabiting this policy, it causes an error of SecurityException since the calling application does not have an active administrator that uses DeviceAdminInfo.USES_POLICY_FORCE_LOCK to do this I add a tutorial.

How to block screen in Android (Screen lock)

Create a class that extends from DeviceAdminReceiver :

import android.app.admin.DeviceAdminReceiver;

public class AdminReceiver extends DeviceAdminReceiver {

}

In your files AndroidManifest.xml registers the receiver class:

<application>
  ...
  ...

        <receiver
            android:name=".AdminReceiver"
            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.DEVICE_ADMIN_ENABLED"/>
            </intent-filter>
        </receiver>

 </application>

Within /res create the directory /xml and inside create the file device_admin.xml that will be where you will define the policy that will allow closing the device:

<device-admin xmlns:android="http://schemas.android.com/apk/res/android">

    <uses-policies>
        <force-lock/>
    </uses-policies>

</device-admin>

This would be the example of your Activity:

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

public class MainActivity extends AppCompatActivity {


    private static final String TAG = "MainActivity";
    protected static final int REQUEST_ENABLE = 0;


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


        //Throws
        //SecurityException if the calling application does not own an active administrator that uses DeviceAdminInfo.USES_POLICY_FORCE_LOCK

        //https://developer.android.com/reference/android/app/admin/DeviceAdminInfo#USES_POLICY_FORCE_LOCK

        //A type of policy that this device admin can use: able to force the device to lock
        //Un tipo de política que el administrador de este dispositivo puede usar: capaz de forzar el bloqueo del dispositivo

        ComponentName cn = new ComponentName(this, AdminReceiver.class);
        DevicePolicyManager mgr = (DevicePolicyManager)getSystemService(DEVICE_POLICY_SERVICE);

        if (mgr.isAdminActive(cn)) {
            Log.i(TAG, "User is an admin!");
            mgr.lockNow();
        }else{
            Log.e(TAG, "User is NOT an admin!");
            Intent intent = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
            intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, cn);
            startActivityForResult(intent, REQUEST_ENABLE);

        }


    }
}

If the device administrator is not activated, the intent will be responsible for opening the configuration screen, so that the user can determine how and when the screen lock is performed:

Here you can download a complete example.

link

    
answered by 18.09.2018 / 02:45
source