Skip to content Skip to sidebar Skip to footer

Android Devicepolicymanager Locknow()

I'm new to Android development, that's why I hit a wall. I want an application to be running as a service, and monitors SMS. If a specific SMS message is received, it locks the pho

Solution 1:

Here's something from the docs:

The calling device admin must have requested USES_POLICY_FORCE_LOCK to be able to call this method; if it has not, a security exception will be thrown.

Therefore, you should do the following in your oncreate:

ComponentName devAdminReceiver; // this would have been declared in your class body// then in your onCreate
    mDPM = (DevicePolicyManager)getSystemService(Context.DEVICE_POLICY_SERVICE);
    devAdminReceiver = new ComponentName(context, deviceAdminReceiver.class);
//then in your onResumeboolean admin = mDPM.isAdminActive(devAdminReceiver);
if (admin)
    mDPM.lockNow();
else Log.i(tag,"Not an admin");

On a side note, your example code is an activity. That, and you should just use a broadcast receiver to implement everything and monitor sms.

Here's an API example for receiving sms:

http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/os/SmsMessageReceiver.html

Post a Comment for "Android Devicepolicymanager Locknow()"