Android的DevicePolicyManager lockNow() [英] Android DevicePolicyManager lockNow()
问题描述
我是新的Android开发,这就是为什么我碰了壁。我想要一个应用程序运行作为一种服务,和显示器的短信。如果接收到特定的SMS消息时,它锁定在电话(好象锁定期限已过)。有点像一个远程锁定。
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.
我用 DevicePolicyManager
来调用 lockNow()
方法。但是,它触发对上一部分 lockNow()
被称为一个错误。
I used the DevicePolicyManager
to invoke the lockNow()
method. However, it triggers an error right on the part lockNow()
is called.
下面是对活动的样本code:
Here's the sample code on the 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();
}
}
我看了<一个href=\"http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/app/DeviceAdminSample.html\" rel=\"nofollow\">http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/app/DeviceAdminSample.html作为参考例。
谁能帮我?告诉我什么地方错了我的code?我需要一些调整,以使在模拟器或设备管理权限?
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?
谢谢!
推荐答案
下面是从文档的内容:
主叫设备管理员必须已请求USES_POLICY_FORCE_LOCK才能够调用此方法;如果没有,安全异常将被抛出。
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.
因此,你应该做你的OnCreate以下内容:
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");
在一个侧面说明,你的榜样code是一个活动。结果
也就是说,你应该只使用一个广播接收器来实现的一切,监视短信。
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.
下面是用于接收短信的API例如:
Here's an API example for receiving sms:
<一个href=\"http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/os/SmsMessageReceiver.html\" rel=\"nofollow\">http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/os/SmsMessageReceiver.html
这篇关于Android的DevicePolicyManager lockNow()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!