我是Android开发的新手,这就是我开门的原因.我希望应用程序作为服务运行,并监视SMS.如果收到特定的SMS消息,它将锁定电话(就像锁定期限已过期一样).有点像遥控锁.
我使用DevicePolicyManager来调用lockNow()方法.但是,它会在调用部分lockNow()时触发错误.
以下是活动的示例代码:
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(); }}
我把http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/app/DeviceAdminSample.html作为参考例子.
谁能帮我?告诉我我的代码有什么问题?我是否必须调整某些内容才能在模拟器或设备上启用管理权限?
谢谢!
解决方法:
这是来自文档的东西:
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中执行以下 *** 作:
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 onResumeboolean admin = mDPM.isadminActive(devadminReceiver);if (admin) mDPM.lockNow();else Log.i(tag,"Not an admin");
另外,您的示例代码是一项活动.
那,你应该只使用广播接收器来实现一切并监控短信.
这是接收短信的API示例:
http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/os/SmsMessageReceiver.html
总结以上是内存溢出为你收集整理的java – Android DevicePolicyManager lockNow()全部内容,希望文章能够帮你解决java – Android DevicePolicyManager lockNow()所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)