java Android DevicePolicyManager lockNow()

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6560426/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 16:22:22  来源:igfitidea点击:

Android DevicePolicyManager lockNow()

javaandroidsms

提问by Devmonster

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 the phone (as if the lock period has expired). Kinda like a remote lock.

我是 Android 开发的新手,这就是我碰壁的原因。我希望应用程序作为服务运行,并监控 SMS。如果收到特定的 SMS 消息,它会锁定手机(就像锁定期已过)。有点像遥控锁。

I used the DevicePolicyManagerto invoke the lockNow()method. However, it triggers an error right on the part lockNow()is called.

我使用DevicePolicyManager来调用该lockNow()方法。但是,它会在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.htmlas a reference example.

我查看了http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/app/DeviceAdminSample.html作为参考示例。

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!

谢谢!

采纳答案by Reed

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.

调用设备管理员必须已请求 USES_POLICY_FORCE_LOCK 才能调用此方法;如果没有,则会抛出安全异常。

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