1. 程式人生 > >Android登入註冊功能封裝

Android登入註冊功能封裝

我們都知道Android應用軟體基本上都會用到登入註冊功能,那麼對一個一個好的登入註冊模組進行封裝就勢在必行了。這裡給大家介紹一下我的第一個專案中所用到的登入註冊功能的,已經對其進行封裝,希望能對大家有幫助,如果有什麼錯誤或者改進的話希望各位可以指出。

我們都知道登入註冊系列功能的實現有以下幾步:

  • 註冊賬號

  • 登入賬號 (第三方賬號登入)

  • 記住密碼

  • 自動登入

  • 修改密碼

大體的流程如下

  1. 對於需要獲取使用者登入狀態的操作,先判斷使用者是否已經登入。

  2. 如果使用者已經登入,則繼續後面的操作,否則,跳轉到登入頁面進行登入。

  3. 如果已經有賬號,則可以直接登入,或者可以直接選擇第三方平臺授權登入。

  4. 如果還沒有賬號,則需要先進行賬號註冊,註冊成功後再登入;也可以不註冊賬號,通過第三方平臺授權進行登入。

  5. 如果有賬號,但忘記密碼,可以重置密碼,否則直接登入。

好了,一個登入註冊系列的常用功能就是以上這五點了,大體流程也已經知道了,接下來讓我們一個一個的實現它們。

註冊功能的實現

註冊時一般通過手機或者郵箱來註冊,這裡我選擇利用手機號來註冊;且註冊時通常需要接收驗證碼,這裡通過第三方的Mob平臺的簡訊SDK來實現,第三方賬號授權也是利用Mob的ShareSDK來實現的。註冊完成後由客戶端將註冊資訊提交至服務端進行註冊,提交方式為HTTP的POST請求方式。

SignUpActivity.java

package com.example.administrator.loginandregister.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.util.Log;
import android.view.KeyEvent;
import
android.view.View; import android.view.View.OnClickListener; import android.view.inputmethod.EditorInfo; import android.widget.Button; import android.widget.TextView; import android.widget.TextView.OnEditorActionListener; import android.widget.Toast; import com.example.administrator.loginandregister.R; import com.example.administrator.loginandregister.utils.RegexUtils; import com.example.administrator.loginandregister.utils.ToastUtils; import com.example.administrator.loginandregister.utils.VerifyCodeManager; import com.example.administrator.loginandregister.views.CleanEditText; import org.json.JSONException; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStream; import java.net.HttpURLConnection; import java.net.URL; import java.net.URLEncoder; import java.util.Timer; import java.util.TimerTask; /** * Created by JimCharles on 2016/11/27. */ public class SignUpActivity extends Activity implements OnClickListener { private static final String TAG = "SignupActivity"; // 介面控制元件 private CleanEditText phoneEdit; private CleanEditText passwordEdit; private CleanEditText verifyCodeEdit; private CleanEditText nicknameEdit; private Button getVerifiCodeButton; private Button createAccountButton; private VerifyCodeManager codeManager; String result = ""; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_signup); initViews(); codeManager = new VerifyCodeManager(this, phoneEdit, getVerifiCodeButton); } /** * 通過findViewById,減少重複的型別轉換 * * @param id * @return */ @SuppressWarnings("unchecked") public final <E extends View> E getView(int id) { try { return (E) findViewById(id); } catch (ClassCastException ex) { Log.e(TAG, "Could not cast View to concrete class.", ex); throw ex; } } private void initViews() { getVerifiCodeButton = getView(R.id.btn_send_verifi_code); getVerifiCodeButton.setOnClickListener(this); createAccountButton = getView(R.id.btn_create_account); createAccountButton.setOnClickListener(this); phoneEdit = getView(R.id.et_phone); phoneEdit.setImeOptions(EditorInfo.IME_ACTION_NEXT);// 下一步 verifyCodeEdit = getView(R.id.et_verifiCode); verifyCodeEdit.setImeOptions(EditorInfo.IME_ACTION_NEXT);// 下一步 nicknameEdit = getView(R.id.et_nickname); nicknameEdit.setImeOptions(EditorInfo.IME_ACTION_NEXT); passwordEdit = getView(R.id.et_password); passwordEdit.setImeOptions(EditorInfo.IME_ACTION_DONE); passwordEdit.setImeOptions(EditorInfo.IME_ACTION_GO); passwordEdit.setOnEditorActionListener(new OnEditorActionListener() { @Override public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { // 點選虛擬鍵盤的done if (actionId == EditorInfo.IME_ACTION_DONE || actionId == EditorInfo.IME_ACTION_GO) { try { commit(); } catch (IOException | JSONException e1) { e1.printStackTrace(); } } return false; } }); } private void commit() throws IOException, JSONException { String phone = phoneEdit.getText().toString().trim(); String password = passwordEdit.getText().toString().trim(); if (checkInput(phone, password)) { // TODO:請求服務端註冊賬號 createAccountButton.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { //android4.0後的新的特性,網路資料請求時不能放在主執行緒中。 //使用執行緒執行訪問伺服器,獲取返回資訊後通知主執行緒更新UI或者提示資訊。 final Handler handler = new Handler() { @Override public void handleMessage(Message msg) { if (msg.what == 1) { //提示讀取結果 Toast.makeText(SignUpActivity.this, result, Toast.LENGTH_LONG).show(); if (result.contains("成")){ Toast.makeText(SignUpActivity.this, result, Toast.LENGTH_LONG).show(); ToastUtils.showShort(SignUpActivity.this, "註冊成功......"); } else{ final Intent it = new Intent(SignUpActivity.this, LoginActivity.class); //你要轉向的Activity Timer timer = new Timer(); TimerTask task = new TimerTask() { @Override public void run() { startActivity(it); //執行 } }; timer.schedule(task, 1000); //1秒後 } } } }; // 啟動執行緒來執行任務 new Thread() { public void run() { //請求網路 try { Register(phoneEdit.getText().toString(),passwordEdit.getText().toString(),nicknameEdit.getText().toString()); } catch (IOException | JSONException e) { e.printStackTrace(); } Message m = new Message(); m.what = 1; // 傳送訊息到Handler handler.sendMessage(m); } }.start(); } }); } } private boolean checkInput(String phone, String password) { if (TextUtils.isEmpty(phone)) { // 電話號碼為空 ToastUtils.showShort(this, R.string.tip_phone_can_not_be_empty); } else { if (!RegexUtils.checkMobile(phone)) { // 電話號碼格式有誤 ToastUtils.showShort(this, R.string.tip_phone_regex_not_right); } else if (password == null || password.trim().equals("")) { Toast.makeText(this, R.string.tip_password_can_not_be_empty, Toast.LENGTH_LONG).show(); }else if (password.length() < 6 || password.length() > 32 || TextUtils.isEmpty(password)) { // 密碼格式 ToastUtils.showShort(this, R.string.tip_please_input_6_32_password); } else { return true; } } return false; } public Boolean Register(String account, String passWord, String niceName) throws IOException, JSONException { try { String httpUrl="http://cdz.ittun.cn/cdz/user_register.php"; URL url = new URL(httpUrl);//建立一個URL HttpURLConnection connection = (HttpURLConnection)url.openConnection();//通過該url獲得與伺服器的連線 connection.setDoOutput(true); connection.setDoInput(true); connection.setRequestMethod("POST");//設定請求方式為post connection.setConnectTimeout(3000);//設定超時為3秒 //設定傳送型別 connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); connection.setRequestProperty("Charset", "utf-8"); //提交資料 String data = "&name=" + URLEncoder.encode(niceName, "UTF-8")+"&cardid=" + "&passwd=" +passWord+ "&money=0"+ "&number=" + account;//傳遞的資料 connection.setRequestProperty("Content-Length",String.valueOf(data.getBytes().length)); ToastUtils.showShort(this, "資料提交成功......"); //獲取輸出流 OutputStream os = connection.getOutputStream(); os.write(data.getBytes()); os.flush(); //獲取響應輸入流物件 InputStreamReader is = new InputStreamReader(connection.getInputStream()); BufferedReader bufferedReader = new BufferedReader(is); StringBuffer strBuffer = new StringBuffer(); String line = null; //讀取伺服器返回資訊 while ((line = bufferedReader.readLine()) != null){ strBuffer.append(line); } result = strBuffer.toString(); is.close(); connection.disconnect(); } catch (Exception e) { return true; } return false; } @Override public void onClick(View v) { switch (v.getId()) { case R.id.btn_send_verifi_code: // TODO 請求介面傳送驗證碼 codeManager.getVerifyCode(VerifyCodeManager.REGISTER); break; case R.id.btn_create_account: try { commit(); } catch (IOException | JSONException e) { e.printStackTrace(); } break; default: break; } } }
  • 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
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
  • 194
  • 195
  • 196
  • 197
  • 198
  • 199
  • 200
  • 201
  • 202
  • 203
  • 204
  • 205
  • 206
  • 207
  • 208
  • 209
  • 210
  • 211
  • 212
  • 213
  • 214
  • 215
  • 216
  • 217
  • 218
  • 219
  • 220
  • 221
  • 222
  • 223
  • 224
  • 225
  • 226
  • 227
  • 228
  • 229
  • 230
  • 231
  • 232
  • 233
  • 234
  • 235
  • 236
  • 237
  • 238
  • 239
  • 240
  • 241
  • 242
  • 243
  • 244
  • 245
  • 246
  • 247
  • 248
  • 249
  • 250
  • 251

登入功能的實現

登入功能需要在完成註冊以後才能進行,只要提交賬號、密碼等資訊至伺服器,請求登入即可,至於第三方登入功能利用Mob平臺的ShareSDK來實現。

LoginActivity.java

package com.example.administrator.loginandregister.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;