Android - 登入系統:登入頁面


Android端:(使用HTTP POST實現)

  1. public class login extends AppCompatActivity {/**登入用**/
  2. private CheckBox remembercheck;//判斷使用者有沒有勾選記住帳號密碼
  3. private EditText username, password;//使用者的帳號密碼欄位
  4. private Button login,register,forgot;//登入按鈕,創建按鈕,忘記密碼按鈕
  5. private String LOGIN_URL;/**目前暫定用在同一個router下傳資料**/
  6. public static String hostip; //獲取裝置IP
  7. public static String hostmac; //獲取裝置MAC
  8. @Override
  9. protected void onCreate(Bundle savedInstanceState) {
  10. super.onCreate(savedInstanceState);
  11. setContentView(R.layout.activity_login);
  12. LOGIN_URL = getString(R.string.login_url);
  13. username = (EditText) findViewById(R.id.accounts);
  14. password = (EditText) findViewById(R.id.password);
  15. remembercheck = (CheckBox)findViewById(R.id.auto_save_password);
  16. login = (Button) findViewById(R.id.login);
  17. login.setOnClickListener(buttonListener);//login監聽設置
  18. register = (Button) findViewById(R.id.regist);
  19. register.setOnClickListener(buttonListener2);//創建帳號監聽設置
  20. forgot = (Button) findViewById(R.id.forgot);
  21. forgot.setOnClickListener(buttonListener3);//忘記密碼監聽設置
  22.  
  23. /**先從設定檔userinfo分別抓出username和pw兩個字串來看使用者是否曾經設置過**/
  24. SharedPreferences settings = getSharedPreferences("userinfo",0);
  25. String nowgetname = settings.getString("username", "");
  26. username.setText(nowgetname);
  27.  
  28. SharedPreferences settings2 = getSharedPreferences("userinfo",0);
  29. String nowgetpw = settings2.getString("pw","");
  30. password.setText(nowgetpw);
  31.  
  32. hostip = getLocalIpAddress(); //獲取裝置IP
  33. hostmac = getLocalMacAddress();//獲取裝置MAC
  34. }
  35. /**獲取IP >> IPv4的(如果把!inetAddress.isLinkLocalAddress()刪掉,就是IPv6的)**/
  36. public String getLocalIpAddress() {
  37. try {
  38. for (Enumeration<NetworkInterface> en = NetworkInterface
  39. .getNetworkInterfaces(); en.hasMoreElements();) {
  40. NetworkInterface intf = en.nextElement();
  41. for (Enumeration<InetAddress> enumIpAddr = intf
  42. .getInetAddresses(); enumIpAddr.hasMoreElements();) {
  43. InetAddress inetAddress = enumIpAddr.nextElement();
  44. if (!inetAddress.isLoopbackAddress()&&!inetAddress.isLinkLocalAddress()) {
  45. return inetAddress.getHostAddress().toString();
  46. }
  47. }
  48. }
  49. } catch (SocketException ex) {
  50. }
  51. return null;
  52. }
  53.  
  54. /**獲取MAC**/
  55. public String getLocalMacAddress() {
  56. WifiManager wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
  57. WifiInfo info = wifi.getConnectionInfo();
  58. return info.getMacAddress();
  59. }
  60.  
  61. /**登入紐是否被按下**/
  62. private Button.OnClickListener buttonListener = new Button.OnClickListener() {
  63. @Override
  64. public void onClick(View v) {
  65. if(remembercheck.isChecked())/**如果登入紐被按下且記住帳密室被勾選的**/
  66. {
  67. /**連接userinfo設定檔,並且找到對應的字串將使用者輸入寫進去**/
  68. SharedPreferences settings = getSharedPreferences("userinfo", 0);
  69. settings.edit().putString("username", username.getText().toString()).commit();
  70.  
  71. SharedPreferences settings3 = getSharedPreferences("userinfo", 0);
  72. settings3.edit().putString("pw", password.getText().toString()).commit();
  73. }
  74. loginUser();/**呼叫這函式進行使用者資料獲取**/
  75. }
  76. };
  77.  
  78. private Button.OnClickListener buttonListener2 = new Button.OnClickListener() {/**前往創建會員介面**/
  79. @Override
  80. public void onClick(View v) {
  81. Intent intent = new Intent();
  82. intent.setClass(login.this,register.class);
  83. startActivity(intent);
  84. }
  85. };
  86.  
  87. private Button.OnClickListener buttonListener3 = new Button.OnClickListener() {/**前往忘記密碼查詢介面**/
  88. @Override
  89. public void onClick(View v) {
  90. Intent intent = new Intent();
  91. intent.setClass(login.this,forgotpw.class);
  92. startActivity(intent);
  93. }
  94. };
  95.  
  96. private void loginUser() {/**讀取使用者輸入數據**/
  97. String name = username.getText().toString().trim().toLowerCase();
  98. String pd = password.getText().toString().trim().toLowerCase();
  99. String ip = hostip.trim().toLowerCase();
  100. String mac = hostmac.trim().toLowerCase();
  101. login(name, pd, ip, mac);/**獲取資料成功後,開始進行傳送**/
  102. }
  103.  
  104. private void login(String name, String password, String ip, String mac) {
  105. class RegisterUser extends AsyncTask<String, Void, String> {
  106. ProgressDialog loading;
  107. Createmem ruc = new Createmem();/**使用Creatmem.class的功能**/
  108. @Override
  109. protected void onPreExecute()
  110. {
  111. super.onPreExecute();/**當按下創見鈕,出現提式窗**/
  112. loading = ProgressDialog.show(login.this, "登入中...",null, true, true);
  113. }
  114. @Override
  115. protected void onPostExecute(String s) {
  116. super.onPostExecute(s);
  117. loading.dismiss();/**當提式窗結束,出現登入成功與否的訊息**/
  118. if(s.equals("登入成功!"))/**當字串比對成功即可登入**/
  119. {
  120. Toast.makeText(getApplicationContext(), s, Toast.LENGTH_SHORT).show();
  121. String id = username.getText().toString();/**將使用者唯一帳號變成字串準被傳去首頁**/
  122. Intent intent = new Intent();
  123. intent.setClass(login.this,menu.class);
  124. intent.putExtra("id",id);
  125. startActivity(intent);
  126. finish();
  127. }
  128. else if(s.equals(""))/**如果沒連接到網路**/
  129. {
  130. String show = "請檢查網路連線!";
  131. Toast.makeText(getApplicationContext(), show, Toast.LENGTH_SHORT).show();
  132. }
  133. else/**連接到網路 登入有問題**/
  134. {
  135. Toast.makeText(getApplicationContext(), s, Toast.LENGTH_SHORT).show();
  136. }
  137. }
  138. @Override
  139. protected String doInBackground(String... params)/**將資料放入hashmap**/
  140. {
  141. HashMap<String, String> data = new HashMap<String,String>();
  142. data.put("name",params[0]);
  143. data.put("password",params[1]);
  144. data.put("ip",params[2]);
  145. data.put("mac",params[3]);
  146. String result = ruc.sendPostRequest(LOGIN_URL,data);
  147. return result;
  148. }
  149. }
  150. RegisterUser ru = new RegisterUser();/**傳送資料**/
  151. ru.execute(name, password, ip, mac);
  152. }
  153. }
Android介面布局
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:tools="http://schemas.android.com/tools"
  4. android:layout_width="match_parent"
  5. android:layout_height="match_parent"
  6. android:background="@drawable/login_bg"
  7. tools:context="project.rmotex.achat.login">
  8.  
  9. <ImageView android:id="@+id/image"
  10. android:background="@drawable/login_title"
  11. android:layout_width="wrap_content"
  12. android:layout_height="wrap_content"
  13. android:layout_marginTop="102dp"
  14. android:layout_alignParentTop="true"
  15. android:layout_centerHorizontal="true" />
  16.  
  17. <LinearLayout
  18. android:orientation="vertical"
  19. android:id="@+id/input"
  20. android:layout_width="fill_parent"
  21. android:layout_height="wrap_content"
  22. android:layout_marginLeft="28.0dip"
  23. android:layout_marginRight="28.0dip"
  24. android:layout_below="@+id/image">
  25. <EditText android:textSize="16.0sp"
  26. android:textColor="#E0E0E0"
  27. android:textColorHint="#E0E0E0"
  28. android:id="@+id/accounts"
  29. android:background="#40000000"
  30. android:gravity="center_vertical"
  31. android:paddingLeft="12.0dip"
  32. android:layout_width="fill_parent"
  33. android:layout_height="44dp"
  34. android:maxLines="1"
  35. android:maxLength="16"
  36. android:hint="Account"
  37. android:textStyle="bold"
  38. android:inputType="textPersonName"/>
  39.  
  40. <View android:background="#ffc0c3c4"
  41. android:layout_width="fill_parent"
  42. android:layout_height="1.0px"
  43. android:layout_marginLeft="1.0px"
  44. android:layout_marginRight="1.0px" />
  45.  
  46. <EditText android:textSize="16.0sp"
  47. android:textColor="#E0E0E0"
  48. android:textColorHint="#E0E0E0"
  49. android:id="@+id/password"
  50. android:background="#40000000"
  51. android:hint="Password"
  52. android:textStyle="bold"
  53. android:gravity="center_vertical"
  54. android:paddingLeft="12.0dip"
  55. android:layout_width="fill_parent"
  56. android:layout_height="44dp"
  57. android:maxLines="1"
  58. android:maxLength="16"
  59. android:inputType="textVisiblePassword" />
  60.  
  61. <Button
  62. android:layout_width="match_parent"
  63. android:layout_height="match_parent"
  64. android:gravity="center"
  65. android:background="#40000000"
  66. android:text="Sign In"
  67. android:textStyle="bold"
  68. android:id="@+id/login"
  69. android:paddingTop="5.0dip"
  70. android:layout_marginLeft="12.0dip"
  71. android:layout_marginTop="12.0dip"
  72. android:layout_marginRight="12.0dip"
  73. android:textSize="20.0sp"
  74. />
  75.  
  76. <RelativeLayout
  77. android:layout_width="fill_parent"
  78. android:layout_height="wrap_content"
  79. android:layout_marginLeft="30.0dip"
  80. android:layout_marginTop="8.0dip"
  81. android:layout_marginRight="30.0dip"
  82. android:layout_below="@id/login"
  83. android:layout_weight="1" >
  84. <CheckBox android:textSize="16.0sp"
  85. android:layout_marginTop="8.0dip"
  86. android:textColor="#ffffffff"
  87. android:layout_alignParentLeft="true"
  88. android:id="@+id/auto_save_password"
  89. android:background="#00000000"
  90. android:layout_width="wrap_content"
  91. android:layout_height="wrap_content"
  92. android:checked="false"
  93. android:text="KEEP"
  94. android:gravity="right|center"
  95. android:textStyle="bold"
  96. />
  97.  
  98. <Button android:textSize="16.0sp"
  99. android:textColor="#ffffffff"
  100. android:layout_alignParentRight="true"
  101. android:gravity="left|center"
  102. android:id="@+id/regist"
  103. android:background="#00000000"
  104. android:clickable="true"
  105. android:layout_width="wrap_content"
  106. android:layout_height="wrap_content"
  107. android:textStyle="bold"
  108. android:paddingLeft="18.0dip"
  109. android:text="Sign Up" />
  110. </RelativeLayout>
  111.  
  112. <Button android:textSize="12.0sp"
  113. android:textColor="#ffffffff"
  114. android:id="@+id/forgot"
  115. android:background="#00000000"
  116. android:clickable="true"
  117. android:layout_width="wrap_content"
  118. android:layout_height="wrap_content"
  119. android:textStyle="bold"
  120. android:layout_marginLeft="12.0dip"
  121. android:layout_marginRight="12.0dip"
  122. android:text="Forgot Password?"
  123. android:layout_gravity="center_horizontal" />
  124. </LinearLayout>
  125. </RelativeLayout>
這裡使用XAMPP建立的Server,將相對應的PHP放置在htdocs這資料夾底下 
登入用PHP(Server必須先完成與MYSQL的連線)
  1. <?php
  2. /**用於使用者登入系統**/
  3. if($_SERVER['REQUEST_METHOD']=='POST'){//限制條件為POST
  4. $name = $_POST['name'];
  5. $password = $_POST['password'];
  6. if($name == '' || $password == '')
  7. {
  8. echo '請確實輸入帳號密碼!';
  9. }
  10. else if($name == '')
  11. {
  12. echo '請輸入帳號!';
  13. }
  14. else if($password == '')
  15. {
  16. echo '請輸入密碼!';
  17. }
  18. else
  19. {
  20. require_once('dbConnect.php');
  21. $sql = "SELECT * FROM userinfo WHERE name='$name'";//先檢查帳號是否存在
  22. $check = mysqli_fetch_array(mysqli_query($con,$sql));
  23. if(isset($check)==false)
  24. {
  25. echo '此帳號不存在!';
  26. }
  27. else//檢查整個帳戶
  28. {
  29. $sqll = "SELECT * FROM userinfo WHERE name='$name' AND password='$password'";
  30. $checkk = mysqli_fetch_array(mysqli_query($con,$sqll));
  31. if(isset($checkk)==false)
  32. {
  33. echo '帳號正確,請檢查密碼!';
  34. }
  35. else
  36. {
  37. echo '登入成功!';
  38. $sqlll = "UPDATE userinfo SET online = NOT online WHERE name = '$name'";//修改上線狀態
  39. mysqli_query($con,$sqlll);
  40. }
  41. }
  42. mysqli_close($con);
  43. }
  44. }
  45. else
  46. {
  47. echo 'Error';
  48. }
  49. ?>

留言