9
public final static void lockDevice()
    {
        try
        {
            if (devicePolicyManager.isAdminActive(adminComponent))
            {
                devicePolicyManager.lockNow();
            }
        }
        catch (final Exception ex)
        {
            ...
        }
    }

The above code does not throw any exception nor it locks the screen for motorola xoom tablets only. (Both Homeycomb and Icecream Sandwitch) The same code works perfectly on other Homeycomb and ICS tablets.

I googled, but did not get any solution. Any Ideas.....?

PC.
  • 6,870
  • 5
  • 36
  • 71
  • Just curious did you use the USES_POLICY_FORCE_LOCK I'm sure you did just asking? I've also read that the locknow() is bugged for motorola devices but wasn't about tablets was about DroidX. – Sergey Benner Feb 13 '12 at 13:32
  • yes, i've the permission `USES_POLICY_FORCE_LOCK` – PC. Feb 13 '12 at 15:55

2 Answers2

24

Possible reasons for this problem

1) I think there is some problem with receiver's meta-data in your AndroidManifest.xml

2) You haven't added the correct class(extended with DeviceAdminReceiver) to either adminComponent OR to android:name property of receiver.

After spending lot of time on this i have created the code.


Code for main Activity

public class LockerTest extends Activity {
    protected static final int REQUEST_ENABLE = 0;
    DevicePolicyManager devicePolicyManager;
    ComponentName adminComponent;

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

        Button button = (Button) findViewById(R.id.btn);
        button.setOnClickListener(btnListener);

    }

    Button.OnClickListener btnListener = new Button.OnClickListener() {
        public void onClick(View v) {
            adminComponent = new ComponentName(LockerTest.this, Darclass.class);
            devicePolicyManager = (DevicePolicyManager) getSystemService(Context.DEVICE_POLICY_SERVICE);

            if (!devicePolicyManager.isAdminActive(adminComponent)) {

                Intent intent = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
                intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, adminComponent);
                startActivityForResult(intent, REQUEST_ENABLE);
            } else {
                devicePolicyManager.lockNow();
            }

        }
    };

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (REQUEST_ENABLE == requestCode) {
            super.onActivityResult(requestCode, resultCode, data);
        }
    }

}


Create a new class - Darclass - code

import android.app.admin.DeviceAdminReceiver;

public class Darclass extends DeviceAdminReceiver{

}


Create a folder 'xml' in 'res'. Then create my_admin.xml file in 'xml' folder. Code for my_admin.xml. Note add this receiver after </activity> and before </application>

<device-admin xmlns:android="http://schemas.android.com/apk/res/android">
    <uses-policies>
        <limit-password />
        <watch-login />
        <reset-password />
        <force-lock />
        <wipe-data />
    </uses-policies>
</device-admin>


Finally add the receiver given bellow to your AndroidManifest.xml

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

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

It should work on your device.

Vivek
  • 11,938
  • 19
  • 92
  • 127
  • thanks for the effort. but, as mentioned in my question, my code works on all the other devices (tested on HTC 2.2, wildfire, Xperia 2.3, Galaxy Tab 3.1, 3.2) other than motorola 3.1+. I do not have a motorola device, but tested your code on emulators XOOM2 3.2, XOOM2ME 3.2, and it does not work :(. I appreciate your effort though. This is possible a broken API for Motorola, and I'm looking for a workaround. Have you tested your code on Motorola Tablet? – PC. Feb 08 '12 at 03:38
  • I don't have a Motorola device. But i think the code will work on Motorola device. Emulators.has some restrictions and may have problem executing code but the code will work on actual device. I'm pretty sure aboit it. – Vivek Feb 08 '12 at 09:23
  • i hope what u say is right. but sadly its not. multiple clients have reported this issue and the only thing common among all of them is Motorola Tablet. – PC. Feb 08 '12 at 11:08
  • @VVK, this will work but is there any other way to directly make Admin Active of ComponentName without fire intent. – TejaDroid Oct 22 '15 at 12:17
  • This is not working on my rooted samsung galaxy s3 android 5.1.1 cyanogenmod. It passes thru all the codes, but my screen remains unlocked. – F0r3v3r-A-N00b Dec 12 '16 at 07:32
  • Worked. Very Thanks. – Limitless isa May 04 '17 at 15:33
  • 2
    yeah, your code is working, but i have one issue, its locking device as device administrator lock , means i have to enter pin code all the time to unlock, in this case my fingerprint unlock is not working, its telling for enter pincode, can you tell me any suggestion?? – Ramkesh Yadav Jul 24 '17 at 13:14
0

You would need 3 thing to do :

  1. add <receiver> to AndroidManifest
  2. make new xml file for <device-admin>
  3. make new .kt file for DeviceAdminReceiver

Here is a Kotlin answer I did (with code) : stackoverflow.com

QuartZ
  • 154
  • 3
  • 12