共享首选项在 Android K 及更低版本中不起作用 [英] Shared Preferences not working in Android K and below

查看:39
本文介绍了共享首选项在 Android K 及更低版本中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

重新打开的问题(上一个[未解决]:共享偏好不保持会话(安卓 K))

Reopened Question (Previous[Unsolved]: Shared Preference not holding session (Android K))

我有一个使用共享首选项进行会话管理的项目.代码一切正常,但真正令人讨厌的是,该应用程序在 Android Lollipop 及更高版本中保持会话,但不幸的是,它在 Android Kitkat 及以下版本中并不相同.只要应用程序关闭,会话就会丢失,您必须再次登录.以下是我正在使用的代码:

I am having a project which uses Shared Preferences for Session Management. Everything is fine with the code but what really annoying is that the app is holding the session in Android Lollipop and above but unfortunately it is not holding the same for Android Kitkat and below. The session is lost whenever the app is closed and you have to lo in again. Following are the codes, I am using:

Session.java

package com.saptak.disputesession;
import android.content.Context;
import android.content.SharedPreferences;
import java.util.HashMap;
/**
 * Created by Saptak Das on 27-02-2017.
 */

public class Session {

    SharedPreferences sharedPreferences;
    SharedPreferences.Editor editor;

    Context context;

    public static String KEY_FNAME="namef";
    public static String KEY_LNAME="namel";
    public static String IS_LOGIN;

    public Session(Context context) {
        this.context = context;
        sharedPreferences=context.getSharedPreferences("userdetails",0);
        editor=sharedPreferences.edit();
    }

    public  void CreateLoginSession(String fname,String lname)
    {
        editor.putString(KEY_FNAME,fname);
        editor.putString(KEY_LNAME,lname);
        editor.putString(IS_LOGIN,"logged");
        editor.commit();
    }

    public HashMap<String,String> getdetails()
    {
        HashMap<String,String> details=new HashMap<>();
        details.put(KEY_FNAME,sharedPreferences.getString(KEY_FNAME,null));
        details.put(KEY_LNAME,sharedPreferences.getString(KEY_LNAME,null));
        return details;
    }

    public boolean loginstatus()
    {
        if(sharedPreferences.getString(IS_LOGIN,"unlogged").equals("logged"))
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    public void logoutac()
    {
        editor.clear();
        editor.commit();
    }
 }

登录.java

package com.saptak.disputesession;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

/**
 * Created by Saptak Das on 27-02-2017.
 */

public class Login extends Activity {

    Button login;
    EditText first,last;

    Session session;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout);
        session=new Session(getApplicationContext());
        login=(Button)findViewById(R.id.log);
        first=(EditText)findViewById(R.id.fname);
        last=(EditText)findViewById(R.id.lname);
        login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                session.CreateLoginSession(first.getText().toString(),last.getText().toString());
                startActivity(new Intent(getApplicationContext(), MainActivity.class));
                finish();
            }
        });
    }
}

MainActivity.java

package com.saptak.disputesession;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import java.util.HashMap;

public class MainActivity extends AppCompatActivity {

    Session session;
    Boolean flag;

    TextView tf,tl;

    Button logout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        session=new Session(getApplicationContext());
        tf=(TextView)findViewById(R.id.xfname);
        tl=(TextView)findViewById(R.id.xlname);
        logout=(Button)findViewById(R.id.xlogout);
        flag=session.loginstatus();
        if(flag==false)
        {
            startActivity(new Intent(getApplicationContext(),Login.class));
            finish();
        }
        HashMap<String,String> details=session.getdetails();
        tf.setText(details.get(Session.KEY_FNAME));
        tl.setText(details.get(Session.KEY_LNAME));
        logout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                session.logoutac();
                startActivity(new Intent(getApplicationContext(),Login.class));
                finish();
            }
        });
    }
}

这个问题现在让我很紧张,因为应用程序的编码很完美,请帮帮我.提前致谢!

This problem is getting on my nerves now as the app is perfectly coded, please help me out. Thanks in advance!

请注意,问题不在于删除会话.每次我关闭应用程序时,会话都会自行注销.并且此问题仅适用于 Android Kitkat 及以下版本,适用于 Android Lollipop 及以上版本

Please note, the problem is not about deleting the session. The session is logging itself out everytime i am closing the app. and this problem is only in case of Android Kitkat and below, works fine for Android Lollipop and above

推荐答案

String IS_LOGIN 在 Session.java 中应该有一个像这样的键值:

String IS_LOGIN in Session.java should have a key value like:

public static String IS_LOGIN="mykeyvalue";

这篇关于共享首选项在 Android K 及更低版本中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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