简体   繁体   English

如何以编程方式更改密码?

[英]How to change password programmatically?

I'm trying to create an android application that can change the password of the device. 我正在尝试创建一个可以更改设备密码的Android应用程序。 I read about Device Administration Application I try to run this sample and this is my main activity code: 我阅读了有关设备管理应用程序的信息,我尝试运行此示例 ,这是我的主要活动代码:

@Override
protected 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();
        }
    });
}

and I got this error: 我收到了这个错误:

08-26 22:36:51.280  15249-15249/co.rishe.secretpolice.app E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.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$1.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)

Can any one help me how can I fix it? 任何人都可以帮助我如何解决它?

Quoting 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. 如果用户选择不启用该应用程序,它仍将出现在设备上,但其策略将不会被强制执行,并且用户将无法获得任何应用程序的任何好处。

As Mr. Harvey indicates, the error message means that the user has not enabled your app as a device administrator as yet. 正如Harvey先生指出的那样,错误消息表示用户尚未启用您的应用程序作为设备管理员。

Further quoting the documentation: 进一步引用文档:

The process of enabling the application begins when the user performs an action that triggers the ACTION_ADD_DEVICE_ADMIN intent. 当用户执行触发ACTION_ADD_DEVICE_ADMIN意图的操作时,启动应用程序的过程开始。

Here is a sample project that demonstrates setting up a device administrator. 这是一个示例项目 ,演示如何设置设备管理员。 The key is in the LockMeNowActivity : 关键在于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);
    }
  }
}

Here, when the user clicks a button that triggers lockMeNow() , I check to see if my app is a device administrator, and if not I lead the user to the proper spot in the Settings app to decide whether or not to make my app be a device administrator. 在这里,当用户点击触发lockMeNow()的按钮时,我会检查我的应用是否是设备管理员,如果不是,我会引导用户到设置应用中的正确位置来决定是否要创建我的应用是设备管理员。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM