添加pin来验证android应用程序的用户身份



我正在做一个项目,我们希望用户能够添加一个4位数的引脚。我希望能够保存它并让应用程序在启动时检查pin是否设置,以便它询问用户是否设置pin或如果未设置则启动应用程序。我不能在共享首选项中保存此pin。

获得此功能的正确过程是什么?我还会添加指纹扫描仪,所以我需要确保它能正常工作。

您也可以通过共享首选项简单地做到这一点。获取设置密码的活动,使其成为默认活动。应用程序将启动并要求设置密码,如果用户设置密码,然后通过共享偏好存储它,并将它们转发到主活动。下次创建时,检查SetPasswordonCreate(),onStart()中保存的密码。

另一方面,在主活动onStart()中也检查key

在这里我分享一些简单的代码-

<activity android:name=".SetPassword">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".MainActivity"></activity>

向SetPassword类——

public class SetPassword extends AppCompatActivity {
EditText passwordview ;
Button save;
private SharedPreferences prefs;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_set_password);

try {
prefs = PreferenceManager.getDefaultSharedPreferences(this);
String password = prefs.getString("password", "");
if (!password.isEmpty()) {
sendToMain();
}
} catch (Exception e) {
e.printStackTrace();
}
passwordview = findViewById(R.id.password);
save = findViewById(R.id.save);
save.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String password = passwordview.getText().toString();
if (password.isEmpty()) {
passwordview.setError("Password required");
return;
}
if (password.contains(" ")) {
passwordview.setError("Don't support space");
return;
}
if (password.length() < 4 || password.length() >4) {
passwordview.setError("Support 4 digint password");
return;
}
SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
SharedPreferences.Editor editor = pref.edit();
editor.putString("password", password);
editor.commit();
sendToMain();

}
});
}
@Override
protected void onStart() {
super.onStart();
SharedPreferences defPref = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
String password  = defPref.getString("password", "");
if (!password.isEmpty()) {
sendToMain();
}
}
private void sendToMain() {
Intent mainIntent = new Intent(SetPassword.this, MainActivity.class);
startActivity(mainIntent);
finish();
}
}

activity_set_password.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".SetPassword">
<EditText
android:id="@+id/password"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="231dp"
android:hint="password"
android:inputType="numberPassword"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/save"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="143dp"
android:layout_marginLeft="143dp"
android:layout_marginBottom="232dp"
android:text="Button"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

MainActivity类——

public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

}
@Override
protected void onStart() {
super.onStart();
SharedPreferences defPref = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
String password  = defPref.getString("password", "");
if (password.isEmpty()) {
startActivity(new Intent(MainActivity.this, SetPassword.class));
finish();
}
}
}

如果你也考虑加密,那么你可以检查AES加密。

你可以使用SharedPreferences或者你可以保存你的数据使用普通javaInputStream/OutputStream

—在共享首选项的情况下:您可以看到下面的链接https://developer.android.com/training/data-storage/shared-preferences

—对于Java本地类,您可以访问下面的链接https://www.tutorialspoint.com/javaexamples/file_write.htm

最新更新