我正在尝试创建一个可以更改设备密码的Android应用程序.我读了大约Device Administration Application
我尝试运行this sample,这是我的主要活动代码:
@OverrIDeprotected voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.activity_main); final Activity context = this; final String new_pass = ((EditText)findVIEwByID(R.ID.editext)).getText().toString(); ((button)findVIEwByID(R.ID.button)).setonClickListener(new VIEw.OnClickListener() { @OverrIDe public voID onClick(VIEw vIEw) { DevicePolicyManager devicePolicyManager = (DevicePolicyManager)context.getSystemService(context.DEVICE_POliCY_SERVICE); Componentname demoDeviceadmin = new Componentname(context, MainActivity.class); devicePolicyManager.setPasswordQuality( demoDeviceadmin,DevicePolicyManager.PASSWORD_QUAliTY_UnspecIFIED); devicePolicyManager.setPasswordMinimumLength(demoDeviceadmin, 5); boolean result = devicePolicyManager.resetPassword("123456", DevicePolicyManager.reset_PASSWORD_REQUIRE_ENTRY); Toast.makeText(context, "button_lock_password_device..."+result, Toast.LENGTH_LONG).show(); } });}
我收到了这个错误:
08-26 22:36:51.280 15249-15249/co.rishe.secretpolice.app E/AndroIDRuntime﹕ FATAL EXCEPTION: mainjava.lang.SecurityException: No active admin ComponentInfo{com.example.secretpolice.app/com.example.secretpolice.app.MainActivity} at androID.os.Parcel.readException(Parcel.java:1425) at androID.os.Parcel.readException(Parcel.java:1379) at androID.app.admin.IDevicePolicyManager$Stub$Proxy.setPasswordQuality(IDevicePolicyManager.java:1359) at androID.app.admin.DevicePolicyManager.setPasswordQuality(DevicePolicyManager.java:323) at co.rishe.secretpolice.app.MainActivity.onClick(MainActivity.java:32) at androID.vIEw.VIEw.performClick(VIEw.java:4211) at androID.vIEw.VIEw$PerformClick.run(VIEw.java:17267) at androID.os.Handler.handleCallback(Handler.java:615) at androID.os.Handler.dispatchMessage(Handler.java:92) at androID.os.Looper.loop(Looper.java:137) at androID.app.ActivityThread.main(ActivityThread.java:4898) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:511) at com.androID.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006) at com.androID.internal.os.ZygoteInit.main(ZygoteInit.java:773) at dalvik.system.NativeStart.main(Native Method)
任何人都可以帮助我如何解决它?
解决方法:
引用the documentation that you linked to:
One of the major events a device admin application has to handle is the user enabling the application. The user must explicitly enable the application for the policIEs to be enforced. If the user chooses not to enable the application it will still be present on the device, but its policIEs will not be enforced, and the user will not get any of the application’s benefits.
正如Harvey先生指出的那样,错误消息表示用户尚未启用您的应用程序作为设备管理员.
进一步引用文档:
The process of enabling the application begins when the user performs an action that triggers the
ACTION_ADD_DEVICE_admin
intent.
Here is a sample project演示了如何设置设备管理员.
关键在于LockMeNowActivity:
/*** copyright (c) 2012 CommonsWare, LLC licensed under the Apache license, Version 2.0 (the "license"); you may not use this file except in compliance with the license. You may obtain a copy of the license at http://www.apache.org/licenses/liCENSE-2.0. Unless required by applicable law or agreed to in writing, software distributed under the license is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implIEd. See the license for the specific language governing permissions and limitations under the license. From _The Busy Coder's GuIDe to AndroID Development_ http://commonsware.com/AndroID */package com.commonsware.androID.lockme;import androID.app.Activity;import androID.app.admin.DevicePolicyManager;import androID.content.Componentname;import androID.content.Intent;import androID.os.Bundle;import androID.vIEw.VIEw;public class LockMeNowActivity extends Activity { private DevicePolicyManager mgr=null; private Componentname cn=null; @OverrIDe public voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.main); cn=new Componentname(this, adminReceiver.class); mgr=(DevicePolicyManager)getSystemService(DEVICE_POliCY_SERVICE); } public voID lockMeNow(VIEw v) { if (mgr.isadminActive(cn)) { mgr.lockNow(); } else { Intent intent= new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_admin); intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_admin, cn); intent.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION, getString(R.string.device_admin_explanation)); startActivity(intent); } }}
在这里,当用户点击触发lockMeNow()的按钮时,我会检查我的应用是否是设备管理员,如果不是,我会引导用户到设置应用中的正确位置来决定是否要创建我的应用是设备管理员.
总结以上是内存溢出为你收集整理的java – 如何以编程方式更改密码?全部内容,希望文章能够帮你解决java – 如何以编程方式更改密码?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)