重新開放問題 (上一頁[未解]:Shared Preference not holding session (Android K))共享偏好不是在Android的ķ工作,下面
我有它使用共享偏好會話管理的項目。代碼的一切都很好,但真正令人討厭的是,應用程序在Android Lollipop及以上版本中保持會話,但不幸的是,Android Kitkat和以下版本不支持此功能。每當應用程序關閉時,會話都會丟失,您必須重新登錄。以下是代碼,我使用:
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();
}
}
Login.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();
}
});
}
}
這個問題現在讓我感到緊張,因爲應用程序是完全編碼的,請幫助我。提前致謝!
請注意,問題不在於刪除會話。每次我關閉應用程序時,會話都會記錄下來。而這個問題只在Android的奇巧及以下的情況下,工作正常的是Android Lollipop以上
加上'editor.remove (KEY_FNAME); editor.remove(KEY_LNAME); editor.remove(IS_LOGIN);'這在你的'CreateLoginSession'之前'editor.putString(KEY_FNAME,fname);' –
@IntelliJAmiya試過了,沒有任何幫助。問題依然存在 –