Hi then the activity below makes a simple login! With this acitivity I have these two problems on the code below
1) SharedPreferences: Once logged in the user the user credentials should remain saved for the next boot but this only works the first time when then the user closes the application for the second time the app does not maintain saved credentials
2) Saved Login Services: I would like to disable the request to save the passowrd and the username in the various services to keep the login data saved!
how can i solve these?
Java Code:
package app;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.StrictMode;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import org.json.JSONException;
import app.Controller.User;
import app.Service.Permissions;
import app.Service.Support;
import app.View.TabActivity;
public class MainActivity extends AppCompatActivity {
private TextView tvUsername, tvPassword;
private EditText txtUsername, txtPassword;
private TextView tv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getSupportActionBar().setDisplayShowHomeEnabled(true);
getSupportActionBar().setDisplayUseLogoEnabled(true);
//Disabilita da StrictMode di android solo su sdk superiori a 9
if (android.os.Build.VERSION.SDK_INT > 9) {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
}
/* Istanza dei campi */
tvUsername = (TextView) findViewById(R.id.usernametextview);
txtUsername = (EditText) findViewById(R.id.usernameeditext);
tvPassword = (TextView) findViewById(R.id.passwordtextview);
txtPassword = (EditText) findViewById(R.id.passwordtext);
final Button loginbutton = (Button) findViewById(R.id.buttonlogin);
//Configuro la funzione Listener sul login button
loginbutton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Code here executes on main thread after user presses button
if (txtUsername.length() != 0 && txtPassword.length() != 0) {
if (Permissions.isOnline() == true) {
LoginFunction(txtUsername.getText().toString(), txtPassword.getText().toString(), true);
} else {
Support.Notification(MainActivity.this, "Errore", "Non stai navigando");
}
}
}
});
SharedPreferences sp1 = this.getSharedPreferences("Login", Context.MODE_PRIVATE);
String userload = sp1.getString("Username", null);
String pwdload = sp1.getString("Password", null);
LoginFunction(userload, pwdload, false);
}
private void LoginFunction(String Username, String Password, Boolean ShowMessage) {
User u = new User();
Boolean ret = false;
try {
ret = u.Login(Username, Password);
if (ret == true) {
//Salvo le credenziali d'accesso in sharedPrefrences
SharedPreferences sp = this.getSharedPreferences("Login", Context.MODE_PRIVATE);
SharedPreferences.Editor Ed = sp.edit();
Ed.putString("Username", txtUsername.getText().toString());
Ed.putString("Password", txtPassword.getText().toString());
Ed.commit();
//Creo un oggetto Intent da inviare ad una activity
Intent tabhome = new Intent(MainActivity.this, TabActivity.class);
tabhome.putExtra("User", u);
startActivity(tabhome);
} else {
if (ShowMessage == true) {
Support.Notification(MainActivity.this, "Errore", "Login non riuscito");
}
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}