1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
| public class Password extends Activity implements OnClickListener
{
Button submitButton;
EditText passwordEditText;
public static final String PREFS_PRIVATE = "PREFS_PRIVATE";
public static final String KEY_PRIVATE = "KEY_PRIVATE";
public static final String PREFS_READ = "PREFS_READ";
public static final String KEY_READ = "KEY_READ";
public static final String PREFS_WRITE = "PREFS_WRITE";
public static final String KEY_WRITE = "KEY_WRITE";
public static final String PREFS_READ_WRITE = "PREFS_READ_WRITE";
public static final String KEY_READ_WRITE = "KEY_READ_WRITE";
// L'identifiant pour envoyer le password avec l'intent
public static final String PASSWORD = "ID_PASSWORD";
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.pass);
// On recupere le bouton graĉe à son id
Button submitButton = (Button) findViewById(R.id.submitButton);
// ajout de la methode "onClick" lors d'un clique sur le bouton
submitButton.setOnClickListener(this);
// On recupere l'editext
EditText passwordEditText = (EditText) findViewById(R.id.passwordEditText);
// On recupere les SharedPreference avec le nom "prefs_file" et le MODE_PRIVATE
// le mode private permet au fichier de n'être accéssible qu'à l'application qui l'a créé
SharedPreferences prefs = this.getApplicationContext().getSharedPreferences("prefs_file",MODE_PRIVATE);
// on récupere le password si il y en à un d'enregistré
String password = prefs.getString("password","");
// Si le password n'est pas vide ( "" ) on lance StartMain
if(!"".equals(password))
{
StartMain(password);
}
}
// Methode lancée lors du clique sur le bouton "submitButton"
public void onClick(View v)
{
// On Récupere l'editText
EditText passwordEditText = (EditText) findViewById(R.id.passwordEditText);
// on recuperer le password dans l'editText
String password = passwordEditText.getText().toString();
// on ouvre les SharedPreference comme plus haut
SharedPreferences prefs = this.getApplicationContext().getSharedPreferences("prefs_file",MODE_PRIVATE);
// on ouvre un Editor pour pouvoir modifier ou ajouter des couples dans le SharedPreferences
Editor edit = prefs.edit();
// On ajoute la String password , avec l'identifiant "password"
edit.putString("password",password);
// on enregistre les changement
edit.commit();
// Si le password n'est pas vide ( "" ) on lance StartMain
if(!password.equals(""))
{
StartMain(password);
}
}
// StartMain lance la "SecondActivity" grâce à un intent en lui envoyant le password utilisé
public void StartMain(String password)
{
// Creatin de l'intent pour lancer la "SecondActivity" depuis celle où l'on est "this"
Intent intent = new Intent(this, SecondActivity.class);
// on ajoute le password en extra avec l'identifiant PASSWORD crée plus haut
// pour que la SecondActivity puisse connaitre avec quelle password elle a été lancée
intent.putExtra(PASSWORD, password);
// On lance l'activité avec l'intent
startActivity(intent);
}
} |