为什么我的复选框“记住我”用纸制库无法正常工作?

zbdgwd5y  于 2021-06-30  发布在  Java
关注(0)|答案(1)|浏览(315)

我一直在尝试在androidstudio中用java编写代码,让我的应用程序记住我https://github.com/pilgr/paper. 即使我不是自愿注销,我也会被注销。以下是相关活动的代码。请帮助我知道如何解决这个问题。

package com.example.spree;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.Toast;

import com.example.spree.Models.Users;
import com.example.spree.Prevalent.Prevalent;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;
import com.rey.material.drawable.CheckBoxDrawable;
import com.rey.material.widget.CheckBox;

import io.paperdb.Book;
import io.paperdb.Paper;

public class LoginActivity extends AppCompatActivity {

    private EditText phoneNumber, userPassword;
    private Button loginButton;
    private ProgressDialog loadingDialog;
    private String parentDBName = "Users";

    private CheckBox checkBox;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        phoneNumber = findViewById(R.id.login_phone_number_input);
        userPassword = findViewById(R.id.login_password_input);

        loginButton = findViewById(R.id.login_btn);
        loadingDialog = new ProgressDialog(this);

        checkBox = (CheckBox) findViewById(R.id.remember_me_chkb);
        Paper.init(this);

        loginButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                loginUser();
            }
        });

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

            }
        });

    }

    private void loginUser() {

        String phone = phoneNumber.getText().toString();
        String password = userPassword.getText().toString();

         if (TextUtils.isEmpty(phone)) {
            Toast.makeText(this, "Please, enter your mobile number.", Toast.LENGTH_SHORT).show();
        } else if (TextUtils.isEmpty(password)) {
            Toast.makeText(this, "Please, enter your password.", Toast.LENGTH_SHORT).show();
        } else {

             loadingDialog.setTitle("Login Account");
             loadingDialog.setMessage("Please, wait while we are checking the credentials.");
             loadingDialog.setCanceledOnTouchOutside(false);
             loadingDialog.show();

             allowAccessToUser(phone, password);

         }

    }

    private void allowAccessToUser(final String phone, final String password) {

        if(checkBox.isChecked()){
            Paper.book().write(Prevalent.userPassKey, phone);
            Paper.book().write(Prevalent.userPassKey, password);

        }

        final DatabaseReference myRef;
        myRef = FirebaseDatabase.getInstance().getReference();

        myRef.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot snapshot) {
                if(snapshot.child(parentDBName).child(phone).exists()){
                    Users userData = snapshot.child(parentDBName).child(phone).getValue(Users.class);

                    if(userData.getPhone().equals(phone)){
                        if(userData.getPassword().equals(password)){

                            Toast.makeText(LoginActivity.this, "Logged in successfully!", Toast.LENGTH_SHORT).show();
                            loadingDialog.dismiss();

                            Intent intent = new Intent(LoginActivity.this, HomeActivity.class);
                            startActivity(intent);
                        }

                        else
                        {
                            Toast.makeText(LoginActivity.this, "Incorrect password", Toast.LENGTH_SHORT).show();
                            loadingDialog.dismiss();
                        }
                    }
                }
                else
                {
                    Toast.makeText(LoginActivity.this, "Account with this "+phone+ " doesn't exist", Toast.LENGTH_SHORT).show();
                    loadingDialog.dismiss();
                }

            }

            @Override
            public void onCancelled(@NonNull DatabaseError error) {

            }
        });
    }
}
``` `MainActivity` ```
package com.example.spree;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.example.spree.Models.Users;
import com.example.spree.Prevalent.Prevalent;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;

import io.paperdb.Paper;

public class MainActivity extends AppCompatActivity {

    private Button signUp, signIn;
    private ProgressDialog loadingDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        signIn = findViewById(R.id.main_login_btn);
        signUp = findViewById(R.id.main_sign_up_btn);

        Paper.init(this);

        loadingDialog = new ProgressDialog(this);

        signIn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this, LoginActivity.class);
                startActivity(intent);
            }
        });

        signUp.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this, RegisterActivity.class);
                startActivity(intent);
            }
        });

        String phone = Paper.book().read(Prevalent.userPhoneKey);
        String password = Paper.book().read(Prevalent.userPassKey);

        if (phone!="" && password!=""){

            if (!TextUtils.isEmpty(phone) && !TextUtils.isEmpty(password)){
                allowAccess(phone, password);

                loadingDialog.setTitle("Already logged in");
                loadingDialog.setMessage("Please, wait a little while...");
                loadingDialog.setCanceledOnTouchOutside(false);
                loadingDialog.show();
            }
        }
    }

    private void allowAccess(final String phone, final String password) {

        final DatabaseReference myRef;
        myRef = FirebaseDatabase.getInstance().getReference();

        myRef.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot snapshot) {
                if(snapshot.child("Users").child(phone).exists()){
                    Users userData = snapshot.child("Users").child(phone).getValue(Users.class);

                    if(userData.getPhone().equals(phone)){
                        if(userData.getPassword().equals(password)){

                            Toast.makeText(MainActivity.this, "Already logged in!", Toast.LENGTH_SHORT).show();
                            loadingDialog.dismiss();

                            Intent intent = new Intent(MainActivity.this, HomeActivity.class);
                            startActivity(intent);
                        }

                        else
                        {
                            Toast.makeText(MainActivity.this, "Incorrect password", Toast.LENGTH_SHORT).show();
                            loadingDialog.dismiss();
                        }
                    }
                }
                else
                {
                    Toast.makeText(MainActivity.this, "Account with this "+phone+ " doesn't exist", Toast.LENGTH_SHORT).show();
                    loadingDialog.dismiss();
                }

            }

            @Override
            public void onCancelled(@NonNull DatabaseError error) {

            }
        });
    }
}
9avjhtql

9avjhtql1#

你不应该初始化 Paper 每次你用的时候
所以首先你要开始 Paper 在你的 Application() 这样地

class App : Application() {

    override fun onCreate() {
        super.onCreate()
        Paper.init(this)

    }
}

第二件事是把应用程序添加到 AndroidManifest ```
<application
android:name=".common.App"
....

第三件事是创造一个 `CacheManager()` 像这样的事情

class CacheManager {

//keys
private var passKey = "passKey"
private var phoneKey = "phoneKey"

private var pass: String = ""
private var phone: String = ""

init {
    pass = Paper.book().read(passKey, "")
    phone = Paper.book().read(phoneKey, "")

}

//------------ Clear Cache Value ------------//

fun clearPass() {
    pass = ""
    Paper.book().delete(passKey)
}

fun clearPhone() {
    phone = ""
    Paper.book().delete(phoneKey)
}

fun clearAll() {
    pass = ""
    phone = ""

    Paper.book().destroy()
}

//------------ Get Cache Value ------------//

fun getPass(): String = pass
fun getPhone(): String = phone

//------------ Set Cache Value ------------//

fun setPass(pass: String) {
    this.pass = pass
    Paper.book().write(passKey, pass)
}

fun setPhone(phone: String) {
    this.phone = phone
    Paper.book().write(phoneKey, phone)
}

}

当然,在java中(很简单,我只是给你一个想法)

相关问题