我目前正在开发一个 Android 应用程序,只需要用户在按start后注册一次。下次用户打开应用程序时,按开始会将用户重定向到主游戏。
我可以使用 SharedPreferences 来做到这一点。注册后,我存储一个名为 currentState 的值,并用它来检查用户是否已经注册。这是代码:
final int currentState = sharedPreferences.getInt(USERSTATE_NUM_KEY,0);
if(currentState == 0 ) {
Intent i = new Intent(MainActivity.this, Main_Screen.class);
startActivity(i);
} else{
// Redirect user to tutorial page, then the map page.
}
Run Code Online (Sandbox Code Playgroud)
现在,我被要求使用 Firebase 再次完成此操作,因为我已经存储了用户数据。
到目前为止,这是我的代码,只是为了尝试它是否会进入onDataChange,但它不会,我现在不知道该怎么做。到目前为止,这是我的代码:
final String currentUsername = sharedPreferences.getString(USERNAME_NUM_KEY, "");
final DatabaseReference userNameRef = rootReference.child("users");
startGame.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
userNameRef.orderByChild("username").equalTo(currentUsername)
.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
if(dataSnapshot.exists()){
Log.v("TESTERR", "HELLO …Run Code Online (Sandbox Code Playgroud)