I have an app and every time I exit, I was asked to log in again. I want my app to keep a user logged in. I don't where to start. I'm kinda new in using shared preference with JSON in android. If anyone of you who can help me, let me know. Badly need it for school project. Thanks
Here's my sign_in.java:
public class sign_in extends AppCompatActivity {
private EditText username,password;
private Button btnSignIn;
private RequestQueue requestQueue;
private static final String URL = "http://192.168.1.2:80/OrgSpace/API/user_control.php";
private StringRequest request;
@Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sign_in);
username = (EditText) findViewById(R.id.username);
password = (EditText) findViewById(R.id.password);
btnSignIn = (Button) findViewById(R.id.btnSignIn);
requestQueue = Volley.newRequestQueue(this);
btnSignIn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
request = new StringRequest(Request.Method.POST, URL, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
JSONObject jsonObject = new JSONObject(response);
if(jsonObject.names().get(0).equals("success")){
Toast.makeText(getApplicationContext(),"SUCCESS "+jsonObject.getString("success"),Toast.LENGTH_SHORT).show();
startActivity(new Intent(getApplicationContext(),menu.class));
}else {
Toast.makeText(getApplicationContext(), "Error" +jsonObject.getString("error"), Toast.LENGTH_SHORT).show();
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
}){
@Override
protected Map<String, String> getParams() throws AuthFailureError {
HashMap<String,String> hashMap = new HashMap<String, String>();
hashMap.put("username",username.getText().toString());
hashMap.put("password",password.getText().toString());
return hashMap;
}
};
requestQueue.add(request);
}
});
}
}
This is where my splash screen:
public class MainActivity extends AppCompatActivity {
Handler h = new Handler();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
h.postDelayed(new Runnable() {
@Override
public void run() {
Intent i = new Intent(MainActivity.this, starting_page.class);
startActivity(i);
finish();
}
}, 5000);
}
}
and this is what they see after splash screen the starting_page.java:
public class starting_page extends AppCompatActivity {
TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_starting_page);
textView=findViewById(R.id.register);
textView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i1 = new Intent (starting_page.this, registration.class);
startActivity(i1);
}
});
}
public void signin(View view) {
Intent intent=new Intent(starting_page.this, sign_in.class);
startActivity(intent);
}
public void btnContinue(View view) {
Intent intent=new Intent(starting_page.this, menu.class);
startActivity(intent);
}
}
This is how you can put a value in a shared preference.
SharedPreferences sharedPreferences =
PreferenceManager.getDefaultSharedPreferences(activity /* Activity context */);
SharedPreferences.Editor myEdit = sharedPreferences.edit();
myEdit.putString("preferenceName", "preferenceValue");
// Once the changes have been made,
// we need to commit to apply those changes made,
// otherwise, it will throw an error
myEdit.commit();
This is how you can get a stored value from a shared preference.
SharedPreferences sharedPreferences =
PreferenceManager.getDefaultSharedPreferences(activity /* Activity context */);
String name = sharedPreferences.getString("preferenceKey", "defaultValue");
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.