Android DevicePolicyManager lockNow()

前端 未结 1 1794
清酒与你
清酒与你 2021-02-10 00:00

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 th

1条回答
  •  独厮守ぢ
    2021-02-10 00:41

    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 onResume
    
    boolean 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

    0 讨论(0)
提交回复
热议问题