Android adds Alertdialog pop-up window (enter password to enter), create system variables

Task

        When inserting usb, you need to enter a password to connect to the PC, and you can reset the password

think

        A Dialog popup needs to be created for verification. Then create a system variable to store the password, and set a developer password (non-resettable) to prevent forgetting the password.

process

        ① Create system variables under the android\frameworks\base\core\java\android\provider\Settings.java file, and fill in the required system variables in the System class

        ② Find the target code file, take the task as an example, under this file is the interface that displays the usb connection

        

       ③ Add the created system variable in the above file (UsbDetailsFragment.class)

         

        Because the new variable added is in the method of the path in the figure below, so to use this variable in this java file, you need to import the variable

      

        ④ Define variables in this class

       

        ⑤ Define the usb connection password verification method, the method of verifying the password before changing the password, and the method of resetting the password

//usb连接密码验证方法
public void UsbConnectionCheck(){
        final EditText editText = new EditText(mContext);
        //明文输入
        editText.setInputType(InputType.TYPE_TEXT_VARIATION_PASSWORD);
        //密文输入
//        editText.setTransformationMethod(PasswordTransformationMethod.getInstance());
        AlertDialog.Builder inputDialog = new AlertDialog.Builder(mContext).setCancelable(false);
        inputDialog.setTitle("Please enter password").setView(editText);
        inputDialog.setPositiveButton("True",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        showToastShort(editText.getText().toString());
                        if(editText.getText().toString().equals(usb_password) || editText.getText().toString().equals("aaa123")){
                            dialog.dismiss();
                        }else{
							finish();
                        }
                    }
                }).setNeutralButton("Reset",new DialogInterface.OnClickListener(){
                    @Override
            public void onClick(DialogInterface dialog, int which) {
                        checkPwd();
                    }
        }).show();
    }
	
	
//重置密码方法
private void resetPassword(){
        final EditText editText = new EditText(mContext);
        AlertDialog.Builder inputDialog = new AlertDialog.Builder(mContext).setCancelable(false);
        inputDialog.setTitle("Please enter new password");
        inputDialog.setView(editText);

        inputDialog.setPositiveButton("Reset",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
						if(editText.getText().toString().equals("")){
							 showToastShort(editText.getText().toString());
							 resetPassword();
						}else{
                        Settings.System.putString(mContext.getContentResolver(),USB_PASSWORD,editText.getText().toString());
						finish();
                        }
					}
                }).show();

    }
	
	
//修改密码前验证密码方法
private void checkPwd(){
        final EditText editText = new EditText(mContext);
        AlertDialog.Builder inputDialog = new AlertDialog.Builder(mContext).setCancelable(false);
        inputDialog.setTitle("Please enter original password");
        inputDialog.setView(editText);
        inputDialog.setPositiveButton("True",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        showToastShort(editText.getText().toString());
                        if(editText.getText().toString().equals(usb_password) || editText.getText().toString().equals("aaa123")){
							resetPassword();
                        }else{
                            dialog.dismiss();
                        }
                    }
                }).setNeutralButton("back",new DialogInterface.OnClickListener(){
            @Override
            public void onClick(DialogInterface dialog, int which) {
                UsbConnectionCheck();
            }
        }).show();

    }
	

//提示框默认显示
private void showToastShort(String str){
        if(str.equals(usb_password) || str.equals("aaa123")){
            Toast.makeText(mContext, "Correct password", Toast.LENGTH_SHORT).show();
        }else if (str.equals("")){
            Toast.makeText(mContext, "Password cannot be empty", Toast.LENGTH_SHORT).show();
        }else{
            Toast.makeText(mContext, "Password error", Toast.LENGTH_SHORT).show();
        }
    }

        

        ⑥ Initialize the password value under the onViewCreated() method, and call the UsbConnectionCheck() method

//此方法一打开界面就会运行
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
	super.onViewCreated(view, savedInstanceState);
	Utils.setActionBarShadowAnimation(getActivity(), getSettingsLifecycle(), getListView());
	usb_password = Settings.System.getString(mContext.getContentResolver(),USB_PASSWORD);
    //设置初始化密码
	if(usb_password==null){
		Settings.System.putString(mContext.getContentResolver(),USB_PASSWORD,"123456");
		usb_password = "123456";
	}
	UsbConnectionCheck();
}

        ⑦Import the required packages

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.content.ContentResolver;
import android.content.DialogInterface;
import android.os.Bundle;
import android.provider.Settings;
//密文输入
import android.text.method.PasswordTransformationMethod;
//明文输入
import android.text.InputType;

import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

running result

Notice:

  1. To write Alertdialog, you need to set the external non-clickable (setCancelable(false)), otherwise the dialog will be invalid.
  2. To end the dialog, you need to use the finish() method
  3. Judgment when reset password is empty
  4. Added system variables need to use the make update-api command to compile
  5. Settings.System.getString(mContext.getContentResolver(),USB_PASSWORD); This method is to get the value of the created variable
  6. Settings.System.putString(mContext.getContentResolver(),USB_PASSWORD,editText.getText().toString()); This method is to modify the value of the created variable

Guess you like

Origin blog.csdn.net/weixin_48426115/article/details/130839727