如何使用记得我功能我的Andr​​oid登录应用 [英] How to use remember me function in my android login application

查看:109
本文介绍了如何使用记得我功能我的Andr​​oid登录应用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在用的是$ C $下记得我的用户名和密码。我已经尝试了很多时间为$ C $下记住我的用户名名和密码,但我不能成功的。

I am using the code for remember my username and password. i have tried a lot of hours for the code for remember my username name and password but i cant success in it.

下面是我的code我从网上下载,但它并没有为我工作。

Here is my code which i have download from internet but it didn't work for me.

我宣布这个变量的延伸活动。

I have declared this variable in the extends activity.

    public static final String PREFS_NAME = "MyPrefsFile";
public static final String PREFS_USER = "prefsUsername";
public static final String PREFS_PASS = "prefsPassword";

在那之后我也声明不同的变量如下图所示。

after that I have also declare different variable like below.

    public String PREFS_USERS;
public String PREFS_PASS;


String username;
String upass;

和点击监听,我给下面的code。

and in the click listener i have given the following code.

    SharedPreferences pref = getSharedPreferences(PREFS_NAME,MODE_PRIVATE);   

                username = etUsername.getText().toString();
                upass = etPassword.getText().toString();

                getSharedPreferences(PREFS_NAME, MODE_PRIVATE)
                .edit()
                .putString(PREFS_USERS, username)
                .putString(PREFS_PASS, upass)
                .commit();

和在时间重试我有返回以下code。在的OnCreate 活动重试我的用户名和密码。

and at the time at retry i have return the following code in the oncreate activity to retry my user name and password.

     SharedPreferences pref = getSharedPreferences(PREFS_NAME, MODE_PRIVATE);

     username = pref.getString(PREFS_USERS, "");
     upass = pref.getString(PREFS_PASS, "");

但是当我运行该应用程序我不能得到的用户名和password.First时候,我加载我检查应用程序的记得我的日志后复选框和恢复应用程序注销并关闭应用程序,然后回来时, it.it未保存我。

But when I run the application i cant get the username and password.First time when i load the application i checked the remember me check box after log in and log out back from the application and close the application and when come back to it.it was not saved for me.

推荐答案

现在的问题是在声明变量

The problem is in declaring the variables

您已经声明

public static final String PREFS_USER = "prefsUsername";
public static final String PREFS_PASS = "prefsPassword";
public String PREFS_USERS;
public String PREFS_PASS; //assigned null here

.putString(preFS_PASS,upass) //这里的preFS_PASS值为null

.putString(PREFS_PASS, upass) //here value of PREFS_PASS is null

使用以下code

做的更好,如如下因素

public String PREFS_USERNAME= "prefsUsername";
    public String PREFS_PASSWORD="prefsPassword";

由于使用下面的code存储数据。

Store the data as using following code

SharedPreferences pref = getSharedPreferences(PREFS_NAME,MODE_PRIVATE);   

        passwordInString = password.getText().toString();
        userNameInString = username.getText().toString();

        getSharedPreferences(PREFS_NAME, MODE_PRIVATE)
        .edit()
        .putString(PREFS_USERNAME, passwordInString)
        .putString(PREFS_PASSWORD, userNameInString)
        .commit();

检索数据,如以下

retrieve the data like the following

SharedPreferences pref = getSharedPreferences(PREFS_NAME, MODE_PRIVATE);

    String usernameName = pref.getString(PREFS_USERNAME, "");
    String upassWord = pref.getString(PREFS_PASSWORD, "");

谢谢 迪帕克

Thanks Deepak

这篇关于如何使用记得我功能我的Andr​​oid登录应用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆