我是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();
}
}
谁能帮我?告诉我我的代码有什么问题?我是否必须调整某些内容才能在模拟器或设备上启用管理权限?
谢谢!
解决方法:
这是来自文档的东西:
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 onResume
boolean admin = mDPM.isAdminActive(devAdminReceiver);
if (admin)
mDPM.lockNow();
else Log.i(tag,"Not an admin");
另外,您的示例代码是一项活动.
那,你应该只使用广播接收器来实现一切并监控短信.
这是接收短信的API示例:
标签:java,android,sms
来源: https://codeday.me/bug/20190630/1338255.html