简体   繁体   English

Android DevicePolicyManager lockNow()

[英]Android DevicePolicyManager lockNow()

I'm new to Android development, that's why I hit a wall.我是 Android 开发的新手,这就是我碰壁的原因。 I want an application to be running as a service, and monitors SMS.我希望应用程序作为服务运行,并监控 SMS。 If a specific SMS message is received, it locks the phone (as if the lock period has expired).如果收到特定的 SMS 消息,它会锁定手机(好像锁定期已过)。 Kinda like a remote lock.有点像遥控锁。

I used the DevicePolicyManager to invoke the lockNow() method.我使用DevicePolicyManager来调用lockNow()方法。 However, it triggers an error right on the part lockNow() is called.但是,它会在调用lockNow()部分时触发错误。

Here's the sample code on the Activity:以下是 Activity 的示例代码:

public class SMSMessagingActivity extends Activity {
    /** Called when the activity is first created. */

public static DevicePolicyManager mDPM;

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

        mDPM = (DevicePolicyManager)getSystemService(Context.DEVICE_POLICY_SERVICE);                    

    }

    public static void LockNow(){
        mDPM.lockNow();
    }

}

I looked at http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/app/DeviceAdminSample.html as a reference example.我查看了http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/app/DeviceAdminSample.ZFC35FDC70D5FC69D2569883A82E作为参考示例。

Can anyone help me?谁能帮我? Show me what's wrong with my code?告诉我我的代码有什么问题? Do I have to tweak something to enable Administrative Rights on the emulator or device?我是否必须进行一些调整才能在模拟器或设备上启用管理权限?

Thanks!谢谢!

Here's something from the docs:这是文档中的一些内容:

The calling device admin must have requested USES_POLICY_FORCE_LOCK to be able to call this method;调用设备管理员必须请求 USES_POLICY_FORCE_LOCK 才能调用此方法; if it has not, a security exception will be thrown.如果没有,将引发安全异常。

Therefore, you should do the following in your oncreate:因此,您应该在 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:这是接收短信的 API 示例:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM