devicePolicyManager.lockNow() is not working for Motorola Tablets
Asked Answered
H

2

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.....?

Hoofbound answered 24/1, 2012 at 13:45 Comment(2)
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.Florindaflorine
yes, i've the permission USES_POLICY_FORCE_LOCKHoofbound
A
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.

Ankney answered 7/2, 2012 at 15:55 Comment(7)
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?Hoofbound
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.Ankney
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.Hoofbound
@VVK, this will work but is there any other way to directly make Admin Active of ComponentName without fire intent.Floc
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.Alcoran
Worked. Very Thanks.Fragmental
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??Elwood
Z
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

Zip answered 3/6, 2021 at 6:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.