Android 使用共享首选项一次性登录?
这可能是一个重复的问题,但我仍然找不到解决方案,因为我是android的新手,我使用SharedReferences进行登录活动我的要求它不应该显示登录页面,除非我们注销我怎么能做到这一点我已经尝试过了 主要活动:Android 使用共享首选项一次性登录?,android,login,sharedpreferences,Android,Login,Sharedpreferences,这可能是一个重复的问题,但我仍然找不到解决方案,因为我是android的新手,我使用SharedReferences进行登录活动我的要求它不应该显示登录页面,除非我们注销我怎么能做到这一点我已经尝试过了 主要活动: package first.service.precision.servicefirst; import android.app.Activity; import android.app.AlertDialog; import android.content.Context; im
package first.service.precision.servicefirst;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
// public EditText password, userName;
Button login, resister;
ProgressBar progressbar;
public static String s1,s2;
TextView tv;
public static final String pref="login";
String TAG = "Fails";
String url = "http://172.16.7.203/sfAppServices/SF_UserLogin.svc";
private ModelLogin Result;
SharedPreferences sharedPreferences;
private SharedPreferences sharedpreferences;
// public static final String MyPREFERENCES = "MyPrefs" ;
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
SharedPreferences shared=getSharedPreferences(pref,0);
Boolean logins=shared.getBoolean("do",false);
if(logins){
Intent intent = new Intent(getApplicationContext(), Main2Activity.class);
startActivity(intent);
}
setContentView(R.layout.activity_main);
final EditText userName = (EditText) findViewById(R.id.txtEmployeeCode);
final EditText password = (EditText) findViewById(R.id.password);
login = (Button) findViewById(R.id.btnsignin);
userName.setBackgroundResource(R.drawable.colorfoucs);
ConnectivityManager cn = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo nf = cn.getActiveNetworkInfo();
if (nf != null && nf.isConnected() == true) {
Toast.makeText(this, "Network Available", Toast.LENGTH_LONG).show();
}
else
{
showAlertDialog(MainActivity.this, "No Network", "Please Check Your Network Connectivity", true);
}
//}
final ConnectionDetector cd = new ConnectionDetector(getApplicationContext());
login.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v) {
// sharedpreferences = getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);
progressbar = (ProgressBar) findViewById(R.id.progressBar);
progressbar.setVisibility(View.VISIBLE);
s1 = userName.getText().toString();
s2 = password.getText().toString();
SharedPreferences sharedPreferences=getSharedPreferences(pref, 0);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putBoolean("do",true);
editor.putString("yog",s1);
editor.putString("pass",s2);
editor.commit();
if (s1.equals("")) {
userName.setError("Enter Employee Code");
}
if (s2.equals("")) {
password.setError("Enter Password");
}
Intent intent = new Intent(getApplicationContext(), Main2Activity.class);
Toast.makeText(MainActivity.this, "Welcome_" +""+ s1, Toast.LENGTH_SHORT).show();
startActivity(intent);
/* RestAdapter adapter = new RestAdapter.Builder().setEndpoint(url).build();
RetrofitRest retro = adapter.create(RetrofitRest.class);
retro.getResult(s1, s2, new Callback<ModelLogin>() {
@Override
public void success(ModelLogin modelLogin, Response response) {
if (modelLogin.getResult().equals(1)||modelLogin.getModuleID().equals(1)) {
progressbar.setVisibility(View.INVISIBLE);
}
}
@Override
public void failure(RetrofitError error) {
progressbar.setVisibility(View.INVISIBLE);
Toast.makeText(MainActivity.this, "Login Fails", LENGTH_SHORT).show();
}
});*/
}
});
// if(sharedpreferences.contains(s1)) {
//
}
public void showAlertDialog(Context context, String title, String message, Boolean status) {
AlertDialog alertDialog = new AlertDialog.Builder(context).create();
// Setting Dialog Title
alertDialog.setTitle(title);
// Setting Dialog Message
alertDialog.setMessage(message);
// Setting alert dialog icon
// Setting OK Button
alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
}
});
// Showing Alert Message
alertDialog.show();
}
}
public void showAlertDialog(Context context, String title, String message, Boolean status) {
AlertDialog alertDialog = new AlertDialog.Builder(context).create();
// Setting Dialog Title
alertDialog.setTitle(title);
// Setting Dialog Message
alertDialog.setMessage(message);
// Setting alert dialog icon
// Setting OK Button
alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
}
});
// Showing Alert Message
alertDialog.show();
}
}
package first.service.precision.servicefirst;
导入android.app.Activity;
导入android.app.AlertDialog;
导入android.content.Context;
导入android.content.DialogInterface;
导入android.content.Intent;
导入android.content.SharedReferences;
导入android.net.ConnectivityManager;
导入android.net.NetworkInfo;
导入android.os.Bundle;
导入android.view.view;
导入android.widget.Button;
导入android.widget.EditText;
导入android.widget.ProgressBar;
导入android.widget.TextView;
导入android.widget.Toast;
公共类MainActivity扩展了活动{
//公共编辑文本密码、用户名;
按钮登录,电阻;
ProgressBar ProgressBar;
公共静态字符串s1、s2;
文本视图电视;
公共静态最终字符串pref=“login”;
String TAG=“失败”;
字符串url=”http://172.16.7.203/sfAppServices/SF_UserLogin.svc";
私有模型登录结果;
SharedReferences SharedReferences;
私人共享引用共享引用;
//公共静态最终字符串MyPREFERENCES=“MyPrefs”;
创建时受保护的void(最终捆绑包savedInstanceState){
super.onCreate(savedInstanceState);
SharedReferences shared=getSharedReferences(pref,0);
Boolean logins=shared.getBoolean(“do”,false);
如果(登录){
Intent Intent=新的Intent(getApplicationContext(),Main2Activity.class);
星触觉(意向);
}
setContentView(R.layout.activity_main);
最终的EditText用户名=(EditText)findViewById(R.id.txtEmployeeCode);
最终EditText密码=(EditText)findViewById(R.id.password);
login=(按钮)findviewbyd(R.id.btnsignin);
用户名.setBackgroundResource(R.drawable.colorfoucs);
ConnectivityManager cn=(ConnectivityManager)getSystemService(Context.CONNECTIVITY_服务);
NetworkInfo nf=cn.getActiveNetworkInfo();
如果(nf!=null&&nf.isConnected()==true){
Toast.makeText(此“网络可用”,Toast.LENGTH_LONG).show();
}
其他的
{
showAlertDialog(MainActivity.this,“无网络”,“请检查网络连接”,true);
}
//}
最终连接检测器cd=新连接检测器(getApplicationContext());
login.setOnClickListener(新视图.OnClickListener()
{
公共void onClick(视图v){
//SharedReferences=GetSharedReferences(MyPREFERENCES,Context.MODE\u PRIVATE);
progressbar=(progressbar)findViewById(R.id.progressbar);
progressbar.setVisibility(View.VISIBLE);
s1=用户名.getText().toString();
s2=password.getText().toString();
SharedReferences SharedReferences=GetSharedReferences(首选项,0);
SharedReferences.Editor=SharedReferences.edit();
编辑器.putBoolean(“do”,true);
编辑:putString(“yog”,s1);
编辑器.putString(“pass”,s2);
commit();
if(s1.等于(“”){
userName.setError(“输入员工代码”);
}
如果(s2.等于(“”){
密码设置错误(“输入密码”);
}
Intent Intent=新的Intent(getApplicationContext(),Main2Activity.class);
Toast.makeText(MainActivity.this,“Welcome”+“”+s1,Toast.LENGTH\u SHORT.show();
星触觉(意向);
/*RestAdapter adapter=new RestAdapter.Builder().setEndpoint(url.build();
RefunderREST retro=adapter.create(RefunderREST.class);
retro.getResult(s1、s2、新回调(){
@凌驾
public void成功(模型登录、模型登录、响应){
if(modelLogin.getResult().equals(1)| | modelLogin.getModuleID().equals(1)){
progressbar.setVisibility(View.INVISIBLE);
}
}
@凌驾
公共无效失败(错误){
progressbar.setVisibility(View.INVISIBLE);
Toast.makeText(MainActivity.this,“登录失败”,长度较短).show();
}
});*/
}
});
//if(SharedReferences.contains(s1)){
//
}
public void showAlertDialog(上下文上下文、字符串标题、字符串消息、布尔状态){
AlertDialog AlertDialog=新建AlertDialog.Builder(context.create();
//设置对话框标题
alertDialog.setTitle(标题);
//设置对话框消息
alertDialog.setMessage(消息);
//设置警报对话框图标
//设置OK按钮
alertDialog.setButton(“确定”,新的DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog,int which){
}
});
//显示警报消息
alertDialog.show();
}
}
public void showAlertDialog(上下文上下文、字符串标题、字符串消息、布尔状态){
AlertDialog AlertDialog=新建AlertDialog.Builder(context.create();
//设置对话框标题
alertDialog.setTitle(标题);
//设置对话框消息
alertDialog.setMessage(消息);
//设置警报对话框图标
//设置OK按钮
alertDialog.setButton(“确定”,新的DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog,int which){
}
});
//提醒我
public class StartActivity extends AppCompatActivity {
private static final String PREF_LOGIN = "LOGIN_PREF";
private static final String KEY_CREDENTIALS = "LOGIN_CREDENTIALS";
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
SharedPreferences preferences = getSharedPreferences(PREF_LOGIN, MODE_PRIVATE);
Intent intent = null;
if(preferences.contains(KEY_CREDENTIALS)){ //if user is currently logged in;
intent = new Intent(this, MainActivity.class);
}else { //if user is not yet logged in;
intent = new Intent(this, LoginActivity.class);
}
startActivity(intent);
}
}
public class LoginActivity extends AppCompatActivity {
public static final String PREF_LOGIN = "LOGIN_PREF";
public static final String KEY_CREDENTIALS = "LOGIN_CREDENTIALS";
private Button loginButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
//...
loginButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//call login api...
//on your sucess callback; we save the credentials...
/*
SharedPreferences.Editor editor = getSharedPreferences(PREF_LOGIN, MODE_PRIVATE).edit();
editor.putString(KEY_CREDENTIALS, "DUMMY CREDENTIALS");
editor.commit();
*/
//on your failure callback; we clear the credentials...
/*
SharedPreferences.Editor editor = getSharedPreferences(PREF_LOGIN, MODE_PRIVATE).edit();
editor.clear();
editor.commit();
*/
}
});
}
}
SharedPreferences sharedPreferences = getSharedPreferences("MyLogin.txt", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putBoolean("FirstLogin", true);
editor.commit();
SharedPreferences sharedPreferences = getSharedPreferences("MyLogin.txt", Context.MODE_PRIVATE);
Boolean loginCheck = sharedPreferences.getBoolean("FirstLogin", false);
if (loginCheck){
intent = new Intent(getApplicationContext(), Main2Activity.class);
startActivity(intent);
}
SharedPreferences pref= getSharedPreferences(MODE_PRIVATE);
Editor editor= pref.edit();
editor.putBoolean("some key" ,false);
//Now when user logs in :
editor.put("some key", true);
//Now for the next login:
if(pref.getBoolean("some key")==true){
// Do not ask for login
}else{
//Ask for login
}
private static final String PREF_LOGIN = "LOGIN_PREF";
private static final String USER_EMAIL = "EMAIL_ADDRESS";
private static final String USER_PASSWORD = "PASSWORD";
SharedPreferences preferences = getSharedPreferences(PREF_LOGIN, MODE_PRIVATE);
Intent intent = null;
if(preferences.contains(USER_EMAIL)&&preferences.contains(USER_PASSWORD)){
intent = new Intent(WelcomeScreen.this,MainMenu.class);//if login
}else {
intent = new Intent(WelcomeScreen.this,LoginScreen.class);//if not login
}
startActivity(intent);
WelcomeScreen.this.finish()
@Override
public void onResponse(String response) {//supose response from server
if(response.contains("sucess")) {
SharedPreferences.Editor editor = getSharedPreferences(PREF_LOGIN, MODE_PRIVATE).edit();
editor.putString(USER_EMAIL,email);
editor.putString(USER_PASSWORD,passworde);
editor.commit();
startActivity(new Intent(LoginScreen.this,MainMenu.class));
}
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id=item.getItemId();
if(id==R.id.logout)
{
SharedPreferences.Editor editor = getSharedPreferences("LOGIN_PREF",getApplicationContext().MODE_PRIVATE).edit();
editor.clear();
editor.commit();
if (editor.commit()) {
Toast.makeText(getApplicationContext(), "Logout Successfully", Toast.LENGTH_LONG).show();
startActivity(new Intent(MainMenu.this, LoginScreen.class));//open login activity on successful logout
}
}
return true;
}