Skip to content Skip to sidebar Skip to footer

Lock Android Phone

I am trying to lock a device programmatically when the user presses a button. I am aware that I will need to use deviceAdminReciever and I have done so but my app crashes whenever

Solution 1:

Here is my code that works:

Manifest:

<application
    android:icon="@drawable/ic_power_settings_new_black_48dp"
    android:label="Screen Off"
    android:theme="@android:style/Theme.NoDisplay">
    <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="android.app.admin.DeviceAdminReceiver">
        <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>

MainActivity.java:

package com.example.anhkhoachu.screenoff;

import android.app.Activity;
import android.app.Application;
import android.app.admin.DeviceAdminReceiver;
import android.app.admin.DevicePolicyManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Toast;


public class MainActivity extends Activity {
DevicePolicyManager dpm;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    dpm = (DevicePolicyManager) getSystemService(Context.DEVICE_POLICY_SERVICE);
    try {
        dpm.lockNow();
        finish();
    } catch (SecurityException e) {
        Intent it = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
        it.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, new ComponentName(this, DeviceAdminReceiver.class));
        startActivityForResult(it, 0);
    }
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode == RESULT_OK) {
        dpm.lockNow();
    } else {
        Toast.makeText(this, "Device administrator must be activated to lock the screen", Toast.LENGTH_LONG).show();
    }
    finish();
}}

device_admin.xml:

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


Solution 2:

OK, DeviceAdminReceiver is a BroadcastReceiver, not an Activity. Right now, your manifest declares MainActivity for both components, so one of those declarations is incorrect. MainActivity is a bad name for this class since it is not an Activity, it should probably be MainReceiver or something like that (just for consistency's sake).

Your application is crashing because Android is trying to start MainActivity, which is not an Activity, as the main Activity of your application, which it cannot do.

Also, according to your code, MyActivity is an inner class of this receiver. This is not a paradigm I would recommend sticking with and may be leading to some of your confusion. I would define both of these entities as completely separate classes. If one MUST be an inner class of the other, the BroadcastReceiver will make more sense as an inner class of the Activity.

At the BARE MINIMUM, if you don't refactor any of your Java code, you need to update your manifest to reference the proper elements based on what you've written, which means referencing the actual Activity as an inner class.

<activity
    android:name=".MainActivity$MyActivity"
    android:label="@string/title_activity_main" >
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />

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

<receiver
    android:name=".MainActivity"
    android:permission="android.permission.BIND_DEVICE_ADMIN" >
    <meta-data
        android:name="android.app.device_admin"
        android:resource="@xml/device_admin_sample" />

    <intent-filter>
        <action android:name="android.app.action.DEVICE_ADMIN_ENABLED" />
    </intent-filter>
</receiver>

Perhaps take a moment to review again the Device Administration API Sample in the SDK, which is located at

<SDK location>/samples/<platform-version>/ApiDemos/src/com/example/android/apis/app/DeviceAdminSample.java

on your machine.


Post a Comment for "Lock Android Phone"