Android 系统进入设置项需要输入密码

进入系统设置输入密码是由客户提到这点,所以这里我定制了这个功能,说一下我的思路:

    进入设置我让客户先进到关于平板的选项里,在关于平板的选项里第一条就是设置管控,点击设备管控会弹出输入密码框的提示,输入正确密码,然后退出关于平板界面,就可以看到设置里所有的选项了~

修改的文件我列出清单:
 1.packages/apps/Settings/AndroidManifest.xml
 2.packages/apps/Settings/res/layout/custom_list.xml
 3.packages/apps/Settings/res/layout/modify_pw.xml
 4.packages/apps/Settings/res/layout/start_dialog.xml
 5.packages/apps/Settings/res/values-zh-rCN/strings.xml
 6.packages/apps/Settings/res/values/dimens.xml
 7.packages/apps/Settings/res/values/strings.xml
 8.packages/apps/Settings/res/xml/device_info_settings.xml
 9.packages/apps/Settings/com/android/settings/DeviceInfoSettings.java
 10.packages/apps/Settings/src/com/android/settings/SecuritySettings.java
 11.packages/apps/Settings/src/com/android/settings/CustomList.java
 12.packages/apps/Settings/com/android/settings/ModifyPassWord.java

下面开始对这些文件一一列出:

1.packages/apps/Settings/AndroidManifest.xml,这个文件里面定义CustomList,并且通过IntentFilter添加了action,

	<activity android:name="com.android.settings.CustomList"
             android:configChanges="orientation|keyboardHidden|screenSize"
             android:clearTaskOnLaunch="true">
        <intent-filter>
            <action android:name="android.intent.custom.control" />
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>

 2,packages/apps/Settings/res/layout/custom_list.xml,这个文件定义了布局文件

<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >

<!--添加screen开始-->
    <PreferenceScreen
        android:key="custom_control_passwd"
        android:title="@string/custom_control_passwd_title" />

    <CheckBoxPreference
            android:key="custom_c_mute"
            android:summary="@string/custom_c_mute_summary"
            android:title="@string/custom_contron" />
    </PreferenceScreen>
<!--添加screen结束-->

3. packages/apps/Settings/res/layout/modify_pw.xml,这个文件定义弹出密码框的布局

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin" >

    <EditText
        android:id="@+id/modify_pw_older"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:hint="@string/mia_modify_pw_older"
        android:inputType="textPassword" />

    <EditText
        android:id="@+id/modify_pw_new"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/modify_pw_older"
        android:hint="@string/mia_modify_pw_new"
        android:inputType="textPassword" />

    <EditText
        android:id="@+id/modify_pw_news"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/modify_pw_new"
        android:hint="@string/custom_modify_pw_news"
        android:inputType="textPassword" />

    <Button
        android:id="@+id/yes"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/modify_pw_news"
        android:text="@string/custom_confirm_bt" />

    <Button
        android:id="@+id/no"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignRight="@id/modify_pw_news"
        android:layout_below="@id/modify_pw_news"
        android:text="@string/custom_cancel_bt"/>

</RelativeLayout>

4.packages/apps/Settings/res/layout/start_dialog.xml,这个文件定义密码输入框的布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
        <EditText
            android:id="@+id/mia_passwd_input"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:inputType="textPassword" />
</LinearLayout>

5.packages/apps/Settings/res/values-zh-rCN/strings.xml,文件定义了中文字符

    <string name="custom_modify_pw">修改管理密码</string>
    <string name="custom_modify_pw_older">请输入老密码</string>
    <string name="custom_modify_pw_new">请输入新密码</string>
    <string name="custom_modify_pw_news">请再次输入新密码</string>
    <string name="custom_pw_no_null">失败:密码不能为空</string>
    <string name="custom_pw_diff">失败:两次输入新密码不一致</string>
    <string name="custom_pw_error">失败:输入的老密码错误</string>
    <string name="custom_pw_success">密码修改成功</string>
    <string name="custom_title_settings">"MIA设置"</string>
    <string name="custom_contron">设备管控</string>
    <string name="custom_contron_passwd">设备管控密码</string>
    <string name="custom_controlWaring">设备管控提醒</string>
    <string name="custom_controlWaring_tv">请输入密码:</string>
    <string name="custom_passwd_error">输入密码错误</string>
    <string name="custom_passwd_input_title">密码</string>
    <string name="custom_c_mute_summary">若勾选,此设备则允许安装、卸载应用程序,并显示其他设置选项.</string>
    <string name="custom_confirm_bt">确认</string>
    <string name="custom_cancel_bt">取消</string>

 6.packages/apps/Settings/res/values/dimens.xml,定义密码输入框的左右边距

    <dimen name="activity_horizontal_margin">16dp</dimen>
    <dimen name="activity_vertical_margin">16dp</dimen>

 7.packages/apps/Settings/res/values/strings.xml,同样定义中文字符

    <string name="custom_modify_pw">修改管理密码</string>
    <string name="custom_modify_pw_older">请输入老密码</string>
    <string name="custom_modify_pw_new">请输入新密码</string>
    <string name="custom_modify_pw_news">请再次输入新密码</string>
    <string name="custom_pw_no_null">失败:密码不能为空</string>
    <string name="custom_pw_diff">失败:两次输入新密码不一致</string>
    <string name="custom_pw_error">失败:输入的老密码错误</string>
    <string name="custom_pw_success">密码修改成功</string>
    <string name="custom_title_settings">"MIA设置"</string>
    <string name="custom_contron">设备管控</string>
    <string name="custom_contron_passwd">设备管控密码</string>
    <string name="custom_controlWaring">设备管控提醒</string>
    <string name="custom_controlWaring_tv">请输入密码:</string>
    <string name="custom_passwd_error">输入密码错误</string>
    <string name="custom_passwd_input_title">密码</string>
    <string name="custom_c_mute_summary">若勾选,此设备则允许安装、卸载应用程序,并显示其他设置选项.</string>
    <string name="custom_confirm_bt">确认</string>
    <string name="custom_cancel_bt">取消</string>

 8.packages/apps/Settings/res/xml/device_info_settings.xml

       <PreferenceScreen
                android:key="custom_control"
                android:summary="@string/custom_contron"
                android:title="@string/custom_contron" >
       </PreferenceScreen>

 9.packages/apps/Settings/com/android/settings/DeviceInfoSettings.java,定义如果key是custom_control就弹出密码框

	  else if (preference.getKey().equals("custom_control")) {
               Intent intent  =  new Intent();
               intent.setAction("android.intent.custom.control");
               intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
               startActivity(intent);
	  }

 10.packages/apps/Settings/src/com/android/settings/SecuritySettings.java.这个文件定义如果没有输入设备管控密码就不会出现安全设置项.

if(android.provider.Settings.System.getInt(getActivity().getContentResolver(), "custom_control_devices",0) == 0){
    getActivity().finish();
}

11.packages/apps/Settings/src/com/android/settings/CustomList.java

package com.lenovo.settings;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.preference.Preference;
import android.preference.Preference.OnPreferenceChangeListener;
import android.preference.Preference.OnPreferenceClickListener;
import android.preference.PreferenceActivity;
import android.preference.PreferenceScreen;
import com.android.settings.R;
import android.provider.Settings;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.preference.CheckBoxPreference;
import android.widget.EditText;
import android.widget.Toast;

public class CustomList extends PreferenceActivity implements
        OnPreferenceChangeListener, OnPreferenceClickListener {

    private PreferenceScreen custom_control_passwd;
    private CheckBoxPreference custom_c_mute;
    private ModifyPassWord mModifyPassWord;

   @SuppressWarnings("deprecation")
   @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.layout.mia_list);
        custom_control_passwd = (PreferenceScreen) findPreference("custom_control_passwd");
        custom_control_passwd.setOnPreferenceClickListener(this);

	custom_c_mute = (CheckBoxPreference) findPreference("custom_c_mute");
	custom_c_mute.setOnPreferenceChangeListener(this);
	mModifyPassWord = new ModifyPassWord(CustomList.this);
    }

    @Override
    public void onResume() {
        super.onResume();
	custom_c_mute.setEnabled(false);
    }

    @Override
    public boolean onPreferenceClick(Preference preference) {
        StartDialog();
        return true;
    }

    private void StartDialog() {
        final View mView = getLayoutInflater().inflate(R.layout.start_dialog,
                null);
        final EditText met = (EditText) mView.findViewById(R.id.custom_passwd_input);
        new AlertDialog.Builder(CustomList.this)
                .setTitle(R.string.custom_passwd_input_title)
                .setView(mView)
                .setPositiveButton(R.string.custom_confirm_bt,
                        new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface arg0, int arg1) {
				if (met != null && met.getText().toString().trim().equals(mModifyPassWord.getPersistentPW())) {
					custom_c_mute.setEnabled(true);
                                } else {
                                    Toast.makeText(getApplicationContext(),
                                            R.string.custom_passwd_error,
                                            Toast.LENGTH_SHORT).show();
                                }
                            }
                        }).setNegativeButton(R.string.custom_cancel_bt, null)
                .setCancelable(false).create().show();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        menu.add(Menu.NONE, Menu.FIRST, 0, R.string.custom_modify_pw)
                .setShowAsAction(MenuItem.SHOW_AS_ACTION_NEVER);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case Menu.FIRST:
            new ModifyPassWord(CustomList.this).show();
            return true;
        default:
            break;
        }
        return super.onOptionsItemSelected(item);
    }


    @Override
    public boolean onPreferenceChange(Preference preference, Object ischeck) {
        final boolean iscontrol = (Boolean) ischeck;
        switch (preference.getTitleRes()) {
        case R.string.custom_contron:
            setCustomControl(iscontrol);
            break;
	}
        return true;
    }

    private void setCustomControl(boolean iscontrol){
	if(iscontrol) {
	   Settings.System.putInt(getContentResolver(), "custom_control_devices",1);
	} else {
	   Settings.System.putInt(getContentResolver(), "custom_control_devices",0);
	}
    }
}

 12.packages/apps/Settings/com/android/settings/ModifyPassWord.java

package com.android.settings;

import android.app.AlertDialog;
import com.android.settings.R;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class ModifyPassWord extends AlertDialog {
    private View mView;
    private Button mBT_YES;
    private Button mBT_NO;
    private EditText PWNew2;
    private EditText PWNew1;
    private EditText PWOlder;
    private Context mContext;
    private Toast mToast;
    private SharedPreferences preferences;

    public ModifyPassWord(Context context) {
        super(context);
        mContext = context;
        preferences = context.getSharedPreferences("custom_control",
                Context.MODE_PRIVATE);
    }

    public String getPersistentPW(){
    	return preferences.getString("custom_control_passwd","123456");
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        mView = getLayoutInflater().inflate(R.layout.modify_pw, null);
        setTitle(R.string.mia_modify_pw);
        mBT_YES = (Button) mView.findViewById(R.id.yes);
        mBT_NO = (Button) mView.findViewById(R.id.no);
        PWNew2 = (EditText) mView.findViewById(R.id.modify_pw_news);
        PWNew1 = (EditText) mView.findViewById(R.id.modify_pw_new);
        PWOlder = (EditText) mView.findViewById(R.id.modify_pw_older);
        mBT_YES.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                switch (checkPassWord()) {
                case 1:
                    preferences.edit().putString("custom_control_passwd",
                            PWNew2.getText().toString().trim()).commit();
                    showToast(R.string.custom_pw_success);
                    dismiss();
                    break;
                case -1:
                    showToast(R.string.custom_pw_no_null);
                    break;
               case -2:
                    showToast(R.string.custom_pw_error);
                    break;
                case -3:
                    showToast(R.string.custom_pw_diff);
                    break;
                }
                PWNew2.setText("");
                PWNew1.setText("");
                PWOlder.setText("");
            }
        });
       mBT_NO.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                dismiss();
            }
        });
        setView(mView);
        setInverseBackgroundForced(false);
        setCanceledOnTouchOutside(false);
        super.onCreate(savedInstanceState);
    }

    private int checkPassWord() {
        String stPWNew2 = PWNew2.getText().toString().trim();
        String stPWNew1 = PWNew1.getText().toString().trim();
        String stPWOlder = PWOlder.getText().toString().trim();
       String persistentPW = getPersistentPW();
        if (stPWNew2.isEmpty() || stPWNew1.isEmpty() || stPWOlder.isEmpty())
            return -1;
        if (!stPWOlder.equals(persistentPW))
            return -2;
        if (!stPWNew2.equals(stPWNew1))
            return -3;
        return 1;
    }

    private void showToast(int resid) {
        if (mToast == null) {
            mToast = Toast.makeText(mContext, "", Toast.LENGTH_SHORT);
        }
        mToast.setText(resid);
        mToast.show();
    }
}

 

  • 2
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值