Android:在会话中保留用户名直到注销 [英] Android: Keep username in session until logout

查看:23
本文介绍了Android:在会话中保留用户名直到注销的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很难弄清楚如何使用 SharedPreferences 将用户名存储在手机中并保持会话直到注销.我还需要知道它如何同时在会话中将用户名与用户在列表视图中单击的数据一起发送出去.以下是我正在使用的代码,我想知道为了实现这一点,应该把代码放在哪里和放什么代码.登录时,它将转到菜单并停留在菜单上(避免后退按钮返回登录屏幕)每个菜单都有一个注销项,它将返回登录屏幕.

I'm having hard time trying to figure out how to use SharedPreferences to store the username in the phone and stay in session until logout. I also need to know how at the same time its in session it will send the username out with the data the user click on within the listview. Below are the codes I am using and would like to know where and what code to put in order to acheive this. As login it will go to the menu and stay at the menu (avoid the backbutton from going back to the login screen) Every menu have a logout item which will go back to the login screen.

我之前看过的问题是 thisthis

The previous questions i've looked at are this and this

Logcat 详细信息

Logcat Detail

05-26 10:42:19.146: WARN/KeyCharacterMap(26071): Using default keymap: /system/usr/keychars/qwerty.kcm.bin
05-26 10:42:23.926: DEBUG/AndroidRuntime(26071): Shutting down VM
05-26 10:42:23.926: WARN/dalvikvm(26071): threadid=3: thread exiting with uncaught exception (group=0x40013140)
05-26 10:42:23.926: ERROR/AndroidRuntime(26071): Uncaught handler: thread main exiting due to uncaught exception
05-26 10:42:23.936: ERROR/AndroidRuntime(26071): java.lang.NullPointerException
05-26 10:42:23.936: ERROR/AndroidRuntime(26071):     at com.merrill2.Login$1.onClick(Login.java:42)
05-26 10:42:23.936: ERROR/AndroidRuntime(26071):     at android.view.View.performClick(View.java:2232)
05-26 10:42:23.936: ERROR/AndroidRuntime(26071):     at android.view.View.onTouchEvent(View.java:3905)
05-26 10:42:23.936: ERROR/AndroidRuntime(26071):     at android.widget.TextView.onTouchEvent(TextView.java:6414)
05-26 10:42:23.936: ERROR/AndroidRuntime(26071):     at android.view.View.dispatchTouchEvent(View.java:3421)
05-26 10:42:23.936: ERROR/AndroidRuntime(26071):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:906)
05-26 10:42:23.936: ERROR/AndroidRuntime(26071):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:906)
05-26 10:42:23.936: ERROR/AndroidRuntime(26071):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:906)
05-26 10:42:23.936: ERROR/AndroidRuntime(26071):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:906)
05-26 10:42:23.936: ERROR/AndroidRuntime(26071):     at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1707)
05-26 10:42:23.936: ERROR/AndroidRuntime(26071):     at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1197)
05-26 10:42:23.936: ERROR/AndroidRuntime(26071):     at android.app.Activity.dispatchTouchEvent(Activity.java:1993)
05-26 10:42:23.936: ERROR/AndroidRuntime(26071):     at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1691)
05-26 10:42:23.936: ERROR/AndroidRuntime(26071):     at android.view.ViewRoot.handleMessage(ViewRoot.java:1533)
05-26 10:42:23.936: ERROR/AndroidRuntime(26071):     at android.os.Handler.dispatchMessage(Handler.java:99)
05-26 10:42:23.936: ERROR/AndroidRuntime(26071):     at android.os.Looper.loop(Looper.java:123)
05-26 10:42:23.936: ERROR/AndroidRuntime(26071):     at android.app.ActivityThread.main(ActivityThread.java:3992)
05-26 10:42:23.936: ERROR/AndroidRuntime(26071):     at java.lang.reflect.Method.invokeNative(Native Method)
05-26 10:42:23.936: ERROR/AndroidRuntime(26071):     at java.lang.reflect.Method.invoke(Method.java:521)
05-26 10:42:23.936: ERROR/AndroidRuntime(26071):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:782)
05-26 10:42:23.936: ERROR/AndroidRuntime(26071):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:540)
05-26 10:42:23.936: ERROR/AndroidRuntime(26071):     at dalvik.system.NativeStart.main(Native Method)
05-26 10:42:23.946: INFO/Process(899): Sending signal. PID: 26071 SIG: 3
05-26 10:42:23.956: INFO/dalvikvm(26071): threadid=7: reacting to signal 3
05-26 10:42:23.966: INFO/dalvikvm(26071): Wrote stack trace to '/data/anr/traces.txt'
05-26 10:42:25.646: DEBUG/dalvikvm(25722): GC freed 973 objects / 63040 bytes in 93ms
05-26 10:42:27.777: DEBUG/DispatchService(945): Handled message = TIMED_SERVICE_UNMASK
05-26 10:42:29.856: DEBUG/DispatchService(945): DISPATCH SERVICE getIdenPacketDataState: returning: 4
05-26 10:42:29.876: INFO/iDENWAPReceiver(953): Received a android.intent.action.ANY_DATA_STATE

登录.java

public class Login extends Activity {
    protected static final SharedPreferences settings = null;
    private EditText etUsername;
    private Button btnLogin;
    private Button btnCancel;
    private TextView lblResult;
    /** Called when the activity is first created. */
    //@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login);

        etUsername = (EditText)findViewById(R.id.username);
        btnLogin = (Button)findViewById(R.id.login_button);
        btnCancel = (Button)findViewById(R.id.cancel_button);
        lblResult = (TextView)findViewById(R.id.result);

        btnLogin.setOnClickListener(new OnClickListener() {
            //@Override
            public void onClick(View v) {
            // Check Login
            String username = etUsername.getText().toString();

            SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
            SharedPreferences.Editor editor = settings.edit();
            editor.putString("username", username);

            if(username.equals("1111")){
                lblResult.setText("Login successful.");

                //startActivity(new Intent(this, Activity2.class));


                Intent i = new Intent(getApplicationContext(), Customer.class);
                startActivity(i);

如果我去掉下面的一行:

if i take out the line below:

protected static final SharedPreferences settings = null;

我把这个:

btnLogin.setOnClickListener(new OnClickListener() {
            private SharedPreferences settings;

'settings' 出错,所以我不得不使用上面提到的那些行之一

'settings' is giving error so i had to use one of those line mentioned above

推荐答案

添加用户只需:

import android.preference.PreferenceManager;
....
SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
SharedPreferences.Editor editor = settings.edit();
editor.putString("username", username);

并在注销时删除它:

SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
SharedPreferences.Editor editor = settings.edit();
editor.remove("username");

这样可以确保用户只持续一个会话,直到它注销.当您打开应用程序时,您应该检查一下(以防万一),如果发生错误,您是否已经在之前的会话中设置了用户名.

This way you make sure that the user only lasts for one session, until it logs out. You should make a check (just in case), when you open your app, to see if there is an username already set from a previous session if something wrong happened.

至于在 ListView 中传递数据,您必须手动完成.SharedPreference 框架负责处理其他所有事情.

And as for passing the data in a ListView, you have to do it by hand. SharedPreference framework takes care of everything else.

这篇关于Android:在会话中保留用户名直到注销的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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