Hafeez Ali

Hafeez Ali

  • NA
  • 28
  • 3.6k

Password to protect application

Jul 31 2018 6:21 AM
Here is the code of Password Activity that store Password in Sqlite , what actually I want when application is open for the first time then MainActivity will open when user save Password then next time whenever the application is open instead of MainActivity, login Activity will open for user login using password that is save last time. thanks in advance.
 
Password Activity
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.IO;  
  4. using System.Linq;  
  5. using System.Text;  
  6. using Android.App;  
  7. using Android.Content;  
  8. using Android.OS;  
  9. using Android.Runtime;  
  10. using Android.Views;  
  11. using Android.Widget;  
  12. namespace PasswordManager  
  13. {  
  14. [Activity(Label = "Password")]  
  15. public class Password : Activity  
  16. {  
  17. DataBase db;  
  18. protected override void OnCreate(Bundle savedInstanceState)  
  19. {  
  20. base.OnCreate(savedInstanceState);  
  21. SetContentView(Resource.Layout.Password);  
  22. db = new DataBase();  
  23. Button btnSave = FindViewById<Button>(Resource.Id.btnSave);  
  24. btnSave.Click += BtnSave_Click;  
  25. }  
  26. public void PasswordSave()  
  27. {  
  28. var txtPassword1 = FindViewById<EditText>(Resource.Id.txtPassword1);  
  29. var txtPassword2 = FindViewById<EditText>(Resource.Id.txtPassword2);  
  30. string Pass1 = txtPassword1.Text.ToString();  
  31. string pass2 = txtPassword2.Text.ToString();  
  32. if (Pass1 == "" && pass2 == "")  
  33. {  
  34. Toast.MakeText(this"Empty Fields", ToastLength.Short).Show();  
  35. }  
  36. else  
  37. {  
  38. if (Pass1 == pass2)  
  39. {  
  40. Person person = new Person()  
  41. {  
  42. password = Pass1,  
  43. };  
  44. db.insertIntoTable(person);  
  45. Toast.MakeText(this"Password Saved", ToastLength.Short).Show();  
  46. }  
  47. else  
  48. {  
  49. Toast.MakeText(this"Please Enter the Same Password", ToastLength.Short).Show();  
  50. }  
  51. }  
  52. }  
Login Activity Code
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using Android.App;  
  6. using Android.Content;  
  7. using Android.OS;  
  8. using Android.Runtime;  
  9. using Android.Views;  
  10. using Android.Widget;  
  11. namespace PasswordManager  
  12. {  
  13. [Activity(Label = "Login")]  
  14. public class Login : Activity  
  15. {  
  16. Person p;  
  17. protected override void OnCreate(Bundle savedInstanceState)  
  18. {  
  19. base.OnCreate(savedInstanceState);  
  20. SetContentView(Resource.Layout.Login);  
  21. Button btnLogin = (Button)FindViewById(Resource.Id.btnLogin);  
  22. btnLogin.Click += BtnLogin_Click;  
  23. }  
  24. private void BtnLogin_Click(object sender, EventArgs e)  
  25. {  
  26. var pass = FindViewById<EditText>(Resource.Id.txtPassword);  
  27. string npass = pass.ToString();  
  28. p = new Person();  
  29. string mypass = p.password;  
  30. if (npass == mypass)  
  31. {  
  32. Toast.MakeText(this"Password Matched", ToastLength.Long).Show();  
  33. StartActivity(typeof(MainActivity));  
  34. }  
  35. else  
  36. {  
  37. Toast.MakeText(this"Password not Matched", ToastLength.Long).Show();  
  38. }  
  39. }  
  40. }  
  41. }

Answers (3)