Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
555 views
in Technique[技术] by (71.8m points)

android - devicePolicyManager.lockNow() is not working for Motorola Tablets

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...