基本上我想使用用户登录的电子邮件ID来存储和使用任何活动来从服务器获取数据。请帮帮我。
第一项活动 Sign_In
public class Sign_In extends AppCompatActivity {
public static final String LOGIN_URL="url";
public static final String KEY_EMAIL="email";
public static final String KEY_PASSWORD="password";
public static final String LOGIN_SUCCESS="success";
public static final String SHARED_PREF_NAME="tech";
public static final String EMAIL_SHARED_PREF="email";
public static final String LOGGEDIN_SHARED_PREF="loggedin";
private EditText editTextEmail;
private EditText editTextPassword;
private Button BtnLogin;
private boolean loggedIn=false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sign__in);
editTextEmail=(EditText)findViewById(R.id.editText3);
editTextPassword=(EditText)findViewById(R.id.editText2);
BtnLogin=(Button)findViewById(R.id.button);
BtnLogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
login();
}
});
}
private void login() {
final String email = editTextEmail.getText().toString().trim().toLowerCase();
final String password = editTextPassword.getText().toString().trim();
StringRequest stringRequest = new StringRequest(Request.Method.POST, LOGIN_URL, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
if (response.trim().equalsIgnoreCase(LOGIN_SUCCESS)){
SharedPreferences sharedPreferences = Sign_In.this.getSharedPreferences(SHARED_PREF_NAME, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putBoolean(LOGGEDIN_SHARED_PREF, true);
editor.putString(EMAIL_SHARED_PREF, email);
editor.apply();
Intent intent = new Intent(Sign_In.this, HomePage.class);
startActivity(intent);
}
else {
Toast.makeText(Sign_In.this,"Invalid credentials",Toast.LENGTH_LONG).show();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
}){
@Override
protected Map<String, String> getParams() throws AuthFailureError {
HashMap<String, String> prams = new HashMap<>();
prams.put(KEY_EMAIL, email);
prams.put(KEY_PASSWORD, password);
return prams;
}
};
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(stringRequest);
}
@Override
protected void onResume() {
super.onResume();
SharedPreferences sharedPreferences = getSharedPreferences(SHARED_PREF_NAME, Context.MODE_PRIVATE);
loggedIn = sharedPreferences.getBoolean(LOGGEDIN_SHARED_PREF, false);
if (loggedIn){
Intent intent = new Intent(Sign_In.this, HomePage.class);
startActivity(intent);
}
}
}
第二个活动主页
import static in.borrowfunds.build30.Sign_In.EMAIL_SHARED_PREF;
import static in.borrowfunds.build30.Sign_In.SHARED_PREF_NAME;
public class HomePage extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home_page);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.addDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event){
if (keyCode==KeyEvent.KEYCODE_BACK){
AlertDialog.Builder alertbox=new AlertDialog.Builder(HomePage.this);
alertbox.setTitle("You want to Exit?");
alertbox.setCancelable(false);
alertbox.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Intent intent = new Intent(getApplicationContext(), Sign_In.class);
startActivity(intent);
finish();
}
});
alertbox.setNegativeButton("No", new DialogInterface.OnClickListener(){
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
alertbox.show();
}
return super.onKeyDown(keyCode,event);
}
@Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.nav_b) {
Intent intent = new Intent(getApplicationContext(), B.class);
startActivity(intent);
return true;
}
else if (id == R.id.nav_l) {
Intent intent = new Intent(getApplicationContext(), L.class);
startActivity(intent);
return true;
}
else if (id == R.id.nav_p) {
Intent intent = new Intent(getApplicationContext(), P.class);
startActivity(intent);
return true;
}
else if (id == R.id.nav_e) {
Intent intent = new Intent(getApplicationContext(), E.class);
startActivity(intent);
return true;
}
else if (id == R.id.nav_pr) {
Intent intent = new Intent(getApplicationContext(), Pr.class);
startActivity(intent);
return true;
}
else if (id == R.id.nav_logout) {
SharedPreferences sharedpreferences = getSharedPreferences(SHARED_PREF_NAME, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedpreferences.edit();
editor.clear();
editor.commit();
HomePage.this.finish();
Intent intent = new Intent(getApplicationContext(), Sign_In.class);
startActivity(intent);
return true;
}
else if (id == R.id.nav_email) {
Intent intent = new Intent(Intent.ACTION_SENDTO, Uri.fromParts("mailto","contact@hgdcsa.net",null));
try {
startActivity(intent);
} catch (android.content.ActivityNotFoundException ex) {
Toast.makeText(this, "Mail account not configured", Toast.LENGTH_SHORT).show();
}
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
}
答案 0 :(得分:0)
在 retreive 数据之后,无论它是什么,您都可以使用SharedPreferences
在设备上本地存储和访问它:
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getContext());
String pref = preferences.getString("preference_key", "default-username");
// if the pref isn't set, you can designate a default value
您要访问此数据的任何活动,您都可以调用preferences.getString
(或任何类型)来访问用户存储的内容。
您可以通过SharedPreferences
和edit
更改<{1}} 存储数据:
commit
偏好设置有各种类型,特别是:
preferences.edit().putString("preference_key", "username").commit();
String
int
答案 1 :(得分:0)
我自己得到了答案,这是一种解脱,被困在这里大约10天。 顺便说一句,感谢anwers,我正在为未来目的提供解决方案。
第一项活动
SharedPreferences sharedPreferences = Sign_In.this.getSharedPreferences(SHARED_PREF_NAME, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putBoolean(LOGGEDIN_SHARED_PREF, true);
editor.putString(EMAIL_SHARED_PREF, email);
editor.apply();
Intent intent = new Intent(Sign_In.this, HomePage.class);
intent.putExtra(EMAIL_SHARED_PREF,email);
startActivity(intent);
第二项活动
TextView email1;
SharedPreferences sharedPreferences = getSharedPreferences(SHARED_PREF_NAME, Context.MODE_PRIVATE);
String email = sharedPreferences.getString(EMAIL_SHARED_PREF, "");
email1=(TextView)findViewById(R.id.textView18);
email1.setText(email);