我已经创建了一个Login Screen
并将其连接到API
,并且电子邮件和密码的身份验证也可以正常工作。我现在想做的是:
1)用户点击登录并成功登录后,将显示一个屏幕,显示WELCOME(欢迎)并在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中的json插件转换pojo来使它们响应)
答案 0 :(得分:0)
“在登录按钮上,单击以检查用户登录是否成功启动了初始屏幕2-3秒,然后从初始屏幕移到主屏幕”
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
startActivity(new Intent(SplashScreen.this,
yourHomeActivity.class));
finish();
}
}
},3000);
“此代码将显示欢迎屏幕三秒钟,然后它将移至主屏幕”
“希望您能找到解决方案”
答案 1 :(得分:0)
在登录时使用共享的首选项管理器来修复此错误,单击存储的并检查打开应用程序
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;
}