[英]How to change password programmatically?
我正在嘗試創建一個可以更改設備密碼的Android應用程序。 我閱讀了有關設備管理應用程序的信息,我嘗試運行此示例 ,這是我的主要活動代碼:
@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();
}
});
}
我收到了這個錯誤:
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)
任何人都可以幫助我如何解決它?
引用您鏈接到的文檔 :
設備管理應用程序必須處理的主要事件之一是啟用應用程序的用戶。 用戶必須明確啟用應用程序以強制執行策略。 如果用戶選擇不啟用該應用程序,它仍將出現在設備上,但其策略將不會被強制執行,並且用戶將無法獲得任何應用程序的任何好處。
正如Harvey先生指出的那樣,錯誤消息表示用戶尚未啟用您的應用程序作為設備管理員。
進一步引用文檔:
當用戶執行觸發
ACTION_ADD_DEVICE_ADMIN
意圖的操作時,啟動應用程序的過程開始。
這是一個示例項目 ,演示如何設置設備管理員。 關鍵在於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()
的按鈕時,我會檢查我的應用是否是設備管理員,如果不是,我會引導用戶到設置應用中的正確位置來決定是否要創建我的應用是設備管理員。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.