移动应用程序共享偏好保存和调用用户登录

Mobile Application Shared Preferences Saving and Calling User Login(移动应用程序共享偏好保存和调用用户登录)
本文介绍了移动应用程序共享偏好保存和调用用户登录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前我的应用程序正在设备"表中搜索现有设备以启用登录访问.但是,我想在每次需要登录时注册一个新设备,并让他们在每次应用程序运行时绕过登录页面.目前,该应用程序说打开时存在无效凭据,因为用户名和密码未正确保存.我该如何解决?

Currently my application is searching for an existing device in "Device" table to enable login access. However, I want to register a new device every time they're required to log in and have them bypass the login page every time the application runs. Currently, the application says there's invalid credentials upon opening since the username and password aren't being saved properly. How do I solve this?

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

    // declaring variables
    etUsername = (EditText)findViewById(R.id.etUsername);
    etPassword= (EditText)findViewById(R.id.etPassword);
    btnLogin = (Button)findViewById(R.id.btnLogin);
    etIpAddress = (EditText) findViewById(R.id.etIpAddress);
    String username = etUsername.getText().toString();
    String password = etPassword.getText().toString();
    String ipAddress = etIpAddress.getText().toString();

    SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(LoginActivity.this);
    if (sharedPreferences.contains("ip")) {
        performLogin(username, password, sharedPreferences.getString("ip", ipAddress));
    }

    // setting up things for login button
    btnLogin.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            String ipAddress = etIpAddress.getText().toString();

            SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(LoginActivity.this);

            sharedPreferences.edit()
                    .putString("ip", ipAddress)
                    .apply();

            String username = etUsername.getText().toString().trim();
            String password = etPassword.getText().toString().trim();

            performLogin(username, password, ipAddress);
        }
    });
}

private void performLogin(String username, String password, String ipAddress) {
    try {
        Device.login(username, password, ipAddress, this);
    } catch (JSONException e) {
        onLoginFailure(e);
    }
}

推荐答案

为了保存登录凭据,我们始终遵循以下代码:-public static final String MyPREFERENCES = "MyPrefs" ;

For saving login credentials we always follow these code:- public static final String MyPREFERENCES = "MyPrefs" ;

   public static final String Name = "nameKey";
   public static final String Phone = "phoneKey";
   public static final String Email = "ip";
   SharedPreferences.Editor editor = sharedpreferences.edit();

            editor.putString(Name, n);
            editor.putString(Phone, ph);
            editor.putString(ip, e);
            editor.commit();

我认为你必须在你的代码中使用那个东西而不是 apply() meth

I think you must use that thing in your code rather then apply() meth

这篇关于移动应用程序共享偏好保存和调用用户登录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

How to target newer versions in .gitlab-ci.yml using auto devops (java 11 instead of 8 and Android 31 instead of 29)(如何在.gitlab-ci.yml中使用自动开发工具(Java 11而不是8,Android 31而不是29)瞄准较新的版本)
Android + coreLibraryDesugaring: which Java 11 APIs can I expect to work?(Android+core LibraryDesugering:我可以期待哪些Java 11API能够工作?)
How to render something in an if statement React Native(如何在If语句中呈现某些内容Reaction Native)
How can I sync two flatList scroll position in react native(如何在本机Reaction中同步两个平面列表滚动位置)
Using Firebase Firestore in offline only mode(在仅脱机模式下使用Firebase FiRestore)
Crash on Google Play Pre-Launch Report: java.lang.NoSuchMethodError(Google Play发布前崩溃报告:java.lang.NoSuchMethodError)