Skip to content

Instantly share code, notes, and snippets.

@riccardopirani
Created July 10, 2018 12:57
Show Gist options
  • Save riccardopirani/88bcfb035ad156664f3b588f8a6f6386 to your computer and use it in GitHub Desktop.
Save riccardopirani/88bcfb035ad156664f3b588f8a6f6386 to your computer and use it in GitHub Desktop.
Activity Android with login
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();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment