javascript - (Android) 在允许用户更新密码之前,如何进行询问用户当前密码的对话?
问题描述
我想制作一个应用程序来锁定应用程序。因此,在用户可以更新密码之前,我希望用户先输入当前密码,然后再让用户更新新密码以确保安全。
这是 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;
}
}
解决方案
我认为对于这种应用程序来说,拥有一个 SQL 数据库并不是那么好。尝试使用 SharedPreferences 代替,然后让用户在 EditText 中输入他的旧密码并检查它
推荐阅读
- php - 如何使用 laravel 填写选择 html 表单
- python - Python程序,assign_add,结果可能有点混乱
- asp.net-mvc-4 - 重定向 ASP.NET MVC 4 时忽略目录
- json - 如何在 Grails 3 中使用 json 视图时设置响应代码
- python - npzeros + itertuples 替代方案
- python - 将浮点数组转换为日期时间
- python - 用PyInstaller编译后是否还需要安装python项目依赖?
- c# - 使用 Daper.Net 和 NPGSQL 将数据插入 PostgreSQL jsonb 列
- amazon-web-services - 限制 aws api 对特定 ip 的访问
- swift - 将用户个人资料图片保存到 Firebase