我已经创建了一个Login Screen,并将我的API与它连接在一起,电子邮件和密码的身份验证工作得很好。我现在尝试做的是: 1)在用户点击登录并成功后,将显示一个屏幕,其中显示欢迎并在2-3秒内关闭,用户再次回到主屏幕。2)如果用户再次启动应用程序,此时不会出现欢迎屏幕。它只在您第一次登录时出现,或者如果您是现有用户,但只有在按下登录按钮时才会出现。我是android的新手,所以我会一步一步来。所以我创建了主屏幕。使用了API,这就是我现在正在尝试的。
有人能帮我解决这个问题吗?我正在使用Retrofit库。
登录活动
public class LoginScreen extends AppCompatActivity implements View.OnClickListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
findViewById(R.id.loginBtnLogin).setOnClickListener(this);
findViewById(R.id.btn_createAccount).setOnClickListener(this);
private void userLogin(){
String Loginemail = loginEmail.getEditText().getText().toString().trim();
String Loginpassword = loginPassword.getEditText().getText().toString().trim();
Call<LoginResponse> call = RetrofitClient
.getInstance().getApi().userLogin(Loginemail, Loginpassword);
call.enqueue(new Callback<LoginResponse>() {
@Override
public void onResponse(Call<LoginResponse> call, Response<LoginResponse> response) {
LoginResponse loginResponse = response.body();
if (loginResponse.getObj() != null){
Toast.makeText(LoginScreen.this, "Welcome", Toast.LENGTH_SHORT).show();
Intent intentLogin = new Intent(LoginScreen.this, HomeScreen.class);
startActivity(intentLogin);
}else{
Toast.makeText(LoginScreen.this, "Please Enter A Valid Email", Toast.LENGTH_SHORT).show();
}
}
@Override
public void onFailure(Call<LoginResponse> call, Throwable t) {
}
});
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.loginBtnLogin:
userLogin();
break;
case R.id.btn_createAccount:
Intent intentRegister = new Intent(LoginScreen.this, CreateAccount.class);
startActivity(intentRegister);
break;
}
}
}改装客户端
public class RetrofitClient {
private static final String BASE_URL = "http://example.com/api/";
private static RetrofitClient mInstance;
private Retrofit retrofit;
private RetrofitClient() {
retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
}
public static synchronized RetrofitClient getInstance() {
if (mInstance == null) {
mInstance = new RetrofitClient();
}
return mInstance;
}
public LoginInterface getApi() {
return retrofit.create(LoginInterface.class);
}
}InterfaceApi是完全正确的,登录响应文件也是如此(在android studio中将pojo从json插件转换为pojo)
发布于 2019-06-03 18:44:44
‘在登录按钮上,点击putt a check if user loggedin成功启动闪屏2-3秒,然后从闪屏移到主屏幕。’
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
startActivity(new Intent(SplashScreen.this,
yourHomeActivity.class));
finish();
}
}
},3000);‘此代码将显示欢迎屏幕三秒钟,之后它将移动到主屏幕。’
“希望你能得到解决方案”
发布于 2019-06-03 18:50:05
登录时使用共享首选项管理器修复此错误,单击存储的并选中打开应用程序
public static void isLogin( Context context){
SharedPreferences sharedPreferences = context.getSharedPreferences("mypref",Context.MODE_PRIVATE);
SharedPreferences.Editor preferenceEditor = sharedPreferences.edit();
preferenceEditor.putBoolean("isLogin",true);
preferenceEditor.commit();
}
public static void isLogout( Context context){
SharedPreferences sharedPreferences = context.getSharedPreferences("mypref",Context.MODE_PRIVATE);
SharedPreferences.Editor preferenceEditor = sharedPreferences.edit();
preferenceEditor.putBoolean("isLogin",false);
preferenceEditor.commit();
}在这里检查
public static boolean userLoginCheck(Context context){
SharedPreferences sharedPreferences = context.getSharedPreferences("mypref",Context.MODE_PRIVATE);
Boolean isLogin = sharedPreferences.getBoolean("isLogin",false);
return isLogin;
}https://stackoverflow.com/questions/56425585
复制相似问题