java登录会话共享首选项
我能够使用共享偏好。我可以从共享首选项文件夹中存储和检索数据。但我无法在注销时将共享首选项设置为null。请指导我如何在登录中维护会话
后勤活动。爪哇:- 我正在处理登录表单。。比较电子邮件id和密码后,数据存储在共享首选项中。我也能够检索数据。但如何维持会话
package com.kezinking.nupur.kezinking;
import 安卓.animation.ObjectAnimator;
import 安卓.app.ProgressDialog;
import 安卓.content.Context;
import 安卓.content.Intent;
import 安卓.content.SharedPreferences;
import 安卓.os.AsyncTask;
import 安卓.os.Bundle;
import 安卓.support.v7.app.AppCompatActivity;
import 安卓.util.Log;
import 安卓.view.Menu;
import 安卓.view.MenuItem;
import 安卓.view.MotionEvent;
import 安卓.view.View;
import 安卓.view.ViewGroup;
import 安卓.widget.Button;
import 安卓.widget.EditText;
import 安卓.widget.ImageButton;
import 安卓.widget.Space;
import 安卓.widget.TextView;
import 安卓.widget.Toast;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import static 安卓.R.attr.password;
/**
* Created by Nupur on 8/2/2016.
*/
public class LoginActivity extends AppCompatActivity {
public static final String MY_JSON = "MY_JSON";
private static final String JSON_URL = "http://kezinking.com/AndroidLogin";
EditText input_email, input_password;
Button btn_login, btnContactUs, btnWorkhr, btnDeltime, btnMoneyBack;
TextView link_signup, textViewJSON, txtcheck,value;
private static Boolean flag = false;
public static final String MyPREFERENCES = "MyPrefs" ;
public static final String Name = "nameKey";
public static final String Phone = "phoneKey";
public static final String emailkey = "emailKey";
public static final String UserId = "user_id";
SharedPreferences sharedpreferences;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
btn_login = (Button) findViewById(R.id.btn_login);
link_signup = (TextView) findViewById(R.id.link_signup);
input_email = (EditText) findViewById(R.id.input_email);
input_password = (EditText) findViewById(R.id.input_password);
textViewJSON = (TextView) findViewById(R.id.textViewJSON);
// value=(TextView)findViewById(R.id.value);
txtcheck = (TextView) findViewById(R.id.txtcheck);
value = (TextView) findViewById(R.id.value);
//shared preference folder
sharedpreferences = getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);
//onClick Event on login Button
btn_login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
login();
}
});
link_signup.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(LoginActivity.this, SignupActivity.class);
startActivity(intent);
LoginActivity.this.getIntent();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public void login() {
if (!validate()) {
onLoginFailed();
return;
} else {
getJSON(JSON_URL);
}
}
public void getJSON(String url) {
class GetJSON extends AsyncTask<String, Void, String> {
ProgressDialog loading;
@Override
protected void onPreExecute() {
super.onPreExecute();
loading = ProgressDialog.show(LoginActivity.this, "Please Wait...", null, true, true);
}
@Override
protected String doInBackground(String... params) {
String uri = params[0];
BufferedReader bufferedReader = null;
try {
URL url = new URL(uri);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
StringBuilder sb = new StringBuilder();
bufferedReader = new BufferedReader(new InputStreamReader(con.getInputStream()));
//String result=null;
String json;
while ((json = bufferedReader.readLine()) != null) {
sb.append(json + "\n");
}
return sb.toString().trim();
} catch (Exception e) {
return null;
}
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
loading.dismiss();
textViewJSON.setText(s);
String jsonString = textViewJSON.getText().toString();
String email = input_email.getText().toString();
String password = input_password.getText().toString();
/** md5(password); // Enrcypting the given password **/
// final String myResult = (email) + (password);
// String value = (String.valueOf(email + password));
// System.out.println(value);
// String value=(input_email.getText().toString()+input_password.getText().toString());
//String input_email = "";
//String input_password = "";
try {
JSONObject jsonObject = new JSONObject(jsonString);
JSONArray result = jsonObject.getJSONArray("result");
//System.out.println("result " + result);
//iterate through json array and check if id is same with your search
//System.out.println("length " + result.length());
for (int i = 0; i < result.length(); i++) {
// System.out.println(i);
JSONObject item = result.getJSONObject(i);
String Email = item.getString("email");
String Password = item.getString("password");
//System.out.println("Email from json "+ Email);
//System.out.println("Password from json " + Password);
if (Email.equals(email) && Password.equals(password)) {
//System.out.println("Email from json"+ Email);
// System.out.println("Password from json" + Password);
String username=item.getString("username");
String userid=item.getString("user_id");
String phone_number=item.getString("con_no");
String emailSaved = item.getString("email");
// System.out.println("Email from json "+ Email);
// System.out.println("Password from json " + Password);
// System.out.println("username from json " + username);
// System.out.println("userid from json " + userid);
// System.out.println("phone_number from json " + phone_number);
SharedPreferences.Editor editor = sharedpreferences.edit();
// Store data in shared preference
editor.putString(Name, username);
editor.putString(Phone, phone_number);
editor.putString(UserId,userid);
editor.putString(emailkey, emailSaved);
editor.commit();
flag=true;
// Retrieve data from shared Preference folder
SharedPreferences prefs = getSharedPreferences(MyPREFERENCES, MODE_PRIVATE);
String show_name = prefs.getString("Name", username); // getting data-name from shared preference
String show_email=prefs.getString("emailkey", emailSaved); // retrieving data from shared preference folder
// Checking the values are coming correct from shared preference folder or not
System.out.println("Shared_preference-email " + show_email);
System.out.println("Shared_preference-username " + show_name);
// value.setText(restoredText);
onLoginSuccess();
break;
}
}
if(!flag){
onLoginFailed();
}
} catch (JSONException e) {
e.printStackTrace();
}
//here you can show the result
Log.v(this.getClass().getSimpleName(), "email = " + email);
Log.v(this.getClass().getSimpleName(), "password = " + password);
}
}
GetJSON gj = new GetJSON();
gj.execute(url);
}
/** private String md5(String password) {
MessageDigest digest;
try {
digest = MessageDigest.getInstance("MD5");
digest.reset();
digest.update(password.getBytes());
byte[] a = digest.digest();
int len = a.length;
StringBuilder sb = new StringBuilder(len << 1);
for (int i = 0; i < len; i++) {
sb.append(Character.forDigit((a[i] & 0xf0) >> 4, 16));
sb.append(Character.forDigit(a[i] & 0x0f, 16));
}
return sb.toString();
} catch (NoSuchAlgorithmException e)
{ e.printStackTrace(); }
System.out.print(password);
return password;
} **/
@Override
public void onBackPressed() {
// disable going back to the MainActivity
moveTaskToBack(true);
}
public void onLoginSuccess() {
btn_login.setEnabled(true);
if(sharedpreferences!=null){
Intent intent = new Intent(LoginActivity.this, MainActivity.class);
startActivity(intent);
LoginActivity.this.getIntent();
}
else
{
Toast.makeText(getBaseContext(), "Shared preference is null", Toast.LENGTH_LONG).show();
}
// finish();
}
public void onLoginFailed() {
Toast.makeText(getBaseContext(), "Login failed", Toast.LENGTH_LONG).show();
// btn_login.setEnabled(true);
}
public boolean validate() {
boolean valid = true;
String email = input_email.getText().toString();
String password = input_password.getText().toString();
if (email.isEmpty() || !安卓.util.Patterns.EMAIL_ADDRESS.matcher(email).matches()) {
input_email.setError("enter a valid email address");
valid = false;
} else {
input_email.setError(null);
}
if (password.isEmpty() || password.length() < 4 || password.length() > 10) {
input_password.setError("between 4 and 10 alphanumeric characters");
valid = false;
} else {
input_password.setError(null);
}
return valid;
}
}
共享首选项SharedReferences是全局变量,所以我也可以在其他java类中使用这个变量?我想在注销java类中使用它。如何在其他java类中使用它
如何将用户名和电子邮件设置为文本视图。 例如,如果我的文本视图是show_customername。然后我要设置show_customername。setText(显示名称)。 我试着这样做:-
show_customername.setText(show_name);
但这是行不通的。 请指导我如何维护登录会话。提前谢谢。希望我的问题也能帮助别人
# 1 楼答案
您可以按如下方式初始化共享首选项:
假设您在LoginActivity中将登录首选项设置为true
当您获得登录设置首选项时,如下所示:
您正在LogoutActiviy中设置注销首选项。 无论何时退出,请按以下方式设置首选项:
并检查您的第一页用户是否已登录,如下图所示:
希望对你有帮助
有关更多参考,您也可以参考以下链接:
Android login/registration with shared preferences
# 2 楼答案
在共享首选项中存储一个布尔值当您成功登录时,以及下次发送登录活动意图之前,请确认该布尔值是否为真。如果为true,则跳过登录屏幕,否则显示登录屏幕。如果应用程序中有注销选项,则将布尔值设置为false,并再次将其保存在共享首选项中
# 3 楼答案
您只需要在编辑器类对象上调用这两个方法
如果需要进一步帮助,您可以回复。祝你好运