((Android)如何在允许用户更新密码之前进行询问用户当前密码的对话?

问题描述 投票:0回答:1

我想制作一个应用来锁定应用。因此,在用户可以更新密码之前,我希望用户先输入当前密码,然后再允许用户更新新密码以确保安全。

这里是MainActivity.java

public class MainActivity extends AppCompatActivity {

        update_pass.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                AlertDialog.Builder builder = new AlertDialog.Builder(cn);
                builder.setTitle("Enter Current Password");

                // Set up the input
                final EditText input = new EditText(cn);
                // Specify the type of input expected; this, for example, sets the input as a password, and will mask the text
                input.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
                builder.setView(input);

                // Set up the buttons
                builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        m_Text = input.getText().toString();

                        if(m_Text.isEmpty()){

                            Toast.makeText(MainActivity.this,   "password can't be empty", Toast.LENGTH_LONG).show();
                        }

                        else{

                            int zz = db.getAllData(m_Text);
                            db.insertData(m_Text);

                            Toast.makeText(MainActivity.this,   "password updated successfully "  , Toast.LENGTH_LONG).show();
                            startActivity(new Intent(MainActivity.this,MainActivity.class));
                            finish();

                        }






                    }
                });
                builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.cancel();
                    }
                });

                builder.show();




            }

                AlertDialog.Builder builder = new AlertDialog.Builder(cn);
                builder.setTitle("Enter Password");

                // Set up the input
                final EditText input = new EditText(cn);
                // Specify the type of input expected; this, for example, sets the input as a password, and will mask the text
                input.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
                builder.setView(input);

                // Set up the buttons
                builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        m_Text = input.getText().toString();

                        if(m_Text.isEmpty()){

                            Toast.makeText(MainActivity.this,   "password can't be empty", Toast.LENGTH_LONG).show();
                        }

                        else{

                            int zz = db.deleteData(m_Text);
                            db.insertData(m_Text);

                            Toast.makeText(MainActivity.this,   "password updated successfully "  , Toast.LENGTH_LONG).show();
                            startActivity(new Intent(MainActivity.this,MainActivity.class));
                            finish();

                        }






                    }
                });
                builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.cancel();
                    }
                });

                builder.show();




            }
        });





    }



    // check your background services
    private boolean isMyServiceRunning(Class<?> serviceClass) {
        ActivityManager manager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
        for (ActivityManager.RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
            if (serviceClass.getName().equals(service.service.getClassName())) {
                Log.i ("Service status", "Running");
                return true;
            }
        }
        Log.i ("Service status", "Not running");
        return false;
    }



    @RequiresApi(api = Build.VERSION_CODES.KITKAT)
    @Override
    protected void onStart() {
        super.onStart();

        if(!isAccessGranted()){
            new AlertDialog.Builder(this)
                    .setTitle("USAGE_STATS Permission")
                    .setMessage("Allow USAGE_STATS Permission in Setting")
                    .setPositiveButton("Allow", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            // action
                            startActivity(new Intent(Settings.ACTION_USAGE_ACCESS_SETTINGS));
                        }
                    })
                    .setNegativeButton("Abort", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            //
                        }
                    })
                    .show();
        }
        else if(pass.isEmpty()){


            update_pass.setText("Set Password");

            AlertDialog.Builder builder = new AlertDialog.Builder(cn);
            builder.setTitle("Enter Password");

             // Set up the input
            final EditText input = new EditText(cn);
            // Specify the type of input expected; this, for example, sets the input as a password, and will mask the text
            input.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
            builder.setView(input);

            // Set up the buttons
            builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    m_Text = input.getText().toString();

                    if(m_Text.isEmpty()){

                        Toast.makeText(MainActivity.this,   "password can't be empty", Toast.LENGTH_LONG).show();
                    }
                    else{
                      boolean tt =  db.insertData(m_Text);
                      pass.add(m_Text);
                      Toast.makeText(MainActivity.this,   "password added successfully "+m_Text, Toast.LENGTH_LONG).show();
                      update_pass.setText("Update Password");

                    }






                }
            });
            builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.cancel();
                }
            });

            builder.show();



        }

这里是Password_Database.java

public class Password_Database extends SQLiteOpenHelper {


    // this is database class, database is sqlite
    // embedded in android studio

    // database name
    public static final String DATABASE_NAME = "pass_data.db";
    // table name
    public static final String TABLE_NAME = "password_table";

    // columns
    public static final String col1 = "password";


    // constructor
    public Password_Database(Context context) {
        super(context, DATABASE_NAME, null, 1);
        // SQLiteDatabase db = this.getWritableDatabase();

    }

    // create table
    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("CREATE TABLE "  + TABLE_NAME + "( password TEXT  )  ");
    }


    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE IF EXISTS "+TABLE_NAME);
    }


    // insert data into table
    public boolean insertData(String name) {

        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues contentValues = new ContentValues();
        contentValues.put(col1,name);


        long result = db.insert(TABLE_NAME, null ,contentValues);
        db.close();

        if(result == -1){
            return false;
        }
        else{
            return true;
        }

    }


    // read data from table
    public Cursor getAllData(String m_Text){

        SQLiteDatabase db = this.getWritableDatabase();
        Cursor res = db.rawQuery("Select * from "+ TABLE_NAME, null);
        return res;
    }






    // update data in table

    public boolean updateData(String name, String pass){
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues contentValues = new ContentValues();

        db.update(TABLE_NAME,contentValues, col1+" =?", new String[]{name});
        return true;
    }



    // delete data from table
    public Integer deleteData(String name){
        SQLiteDatabase db = this.getWritableDatabase();
        int i = db.delete(TABLE_NAME, col1 +" =?", new String[]{name});
        return i;
    }




}
javascript java android sqlite change-password
1个回答
0
投票

我认为拥有SQL数据库对于这种应用程序来说不是那么好。尝试使用SharedPreferences,然后让用户在EditText中输入他的旧密码并检查它

© www.soinside.com 2019 - 2024. All rights reserved.