2017-05-14 15 views
-3

基本上我想使用用户登录的电子邮件ID存储和使用任何活动从服务器获取数据。请帮帮我。Android应用程序坚持共享从1活动到另一个用户名,但没有通过SharedPreferences获取它

第一个活动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","[email protected]",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

我有我自己的答案,有什么救济,被困在这10天左右。 顺便说一句,谢谢你的肛门,我为未来的目的提供解决方案。

第一活动

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); 
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通过editcommit荷兰国际集团的变化:

preferences.edit().putString("preference_key", "username").commit(); 

首进来各种类型,值得注意的是:

  • String
  • int
  • boolean
相关问题