Android登入註冊功能封裝
我們都知道Android應用軟體基本上都會用到登入註冊功能,那麼對一個一個好的登入註冊模組進行封裝就勢在必行了。這裡給大家介紹一下我的第一個專案中所用到的登入註冊功能的,已經對其進行封裝,希望能對大家有幫助,如果有什麼錯誤或者改進的話希望各位可以指出。
我們都知道登入註冊系列功能的實現有以下幾步:
-
註冊賬號
-
登入賬號 (第三方賬號登入)
-
記住密碼
-
自動登入
-
修改密碼
大體的流程如下
-
對於需要獲取使用者登入狀態的操作,先判斷使用者是否已經登入。
-
如果使用者已經登入,則繼續後面的操作,否則,跳轉到登入頁面進行登入。
-
如果已經有賬號,則可以直接登入,或者可以直接選擇第三方平臺授權登入。
-
如果還沒有賬號,則需要先進行賬號註冊,註冊成功後再登入;也可以不註冊賬號,通過第三方平臺授權進行登入。
-
如果有賬號,但忘記密碼,可以重置密碼,否則直接登入。
好了,一個登入註冊系列的常用功能就是以上這五點了,大體流程也已經知道了,接下來讓我們一個一個的實現它們。
註冊功能的實現
註冊時一般通過手機或者郵箱來註冊,這裡我選擇利用手機號來註冊;且註冊時通常需要接收驗證碼,這裡通過第三方的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;
相關推薦
Android登入註冊功能封裝
我們都知道Android應用軟體基本上都會用到登入註冊功能,那麼對一個一個好的登入註冊模組進行封裝就勢在必行了。這裡給大家介紹一下我的第一個專案中所用到的登入註冊功能的,已經對其進行封裝,希望能對大家有幫助,如果有什麼錯誤或者改進的話希望各位可以指出。 我們都知
android SQLite實現本地登入註冊功能,SQLite簡單應用(android studio)
SQLite簡單用法介紹 SQLite基本用法很簡單,繼承SQLiteOpenHelper 中有兩個抽象方法,分別是onCreate()和 onUpgrade(),我們必須在自己的幫助類裡面重寫這兩個方法,然後分別在這兩個方法中去實現建
C語言利用陣列和檔案登入註冊功能
C語言利用檔案系統實現簡單的使用者登入和註冊功能 版本一:利用陣列 最近有個朋友讓我幫他做一個C語言的登入註冊功能,考慮到他沒有學到資料庫於是想到了存入檔案 此版本使用的陣列,第二個版本使用的是連結串列,連結串列是一個很好的資料結構,推薦大家用連結串列 第二版連結可以去我
php微信網頁開發實現自動登入註冊功能例項
功能:自動登入註冊功能 描述:php實現微信網頁自動登入註冊功能 範圍:適用於所有php版本 thinkphp5.0例項 $token = cookie('token'); if($token){ //這裡寫登入後的邏輯 }else{ $
【解決】php實現頁面登入註冊功能並實現跳轉
目錄如下: 思路是不通過資料庫等操作實現頁面(頁面比較簡陋,請笑納) 關於register功能 html頁面必要的表單功能 在頭部寫上 寫上條件判斷 if ($_SERVER['REQUEST_METHOD'] === 'POST') { login();
ajax+springboot+mybits實現登入註冊功能
html程式碼 使用者名稱:<input type="text" placeholder="使用者名稱” id="form-username"> 密碼:<input type="password" placeholder="密碼" id
HTML-css-JavaScript實現簡單登入註冊功能,並讓登入和註冊在同一頁內切換。
(水平有限,如果發現有什麼問題,歡迎指出來) HTML+css先做出來一個登入頁,包括基本的輸入框和button。再做一個註冊頁。程式碼如下: 隨後解釋程式碼 <form id="register" name="myForm" class="register"
javaweb簡單的登入註冊功能實現
下面是使用者登入註冊流程圖 登陸介面 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% String path = request.getContextPat
django學習之包含資料庫的登入註冊功能
本文由本人原創,僅作為自己的學習記錄 這篇文章實現了簡單的登入註冊,登入驗證,session儲存登入狀態,cache快取,ajax接收後臺響應,利用{% csrf_token %}防止csrf跨站保護機制。 前端使用html+css+bootstrap+ajax實現,後端
java利用jdbc連線Mysql資料庫——實現登入註冊功能
實現功能如下: ①0選中註冊,若使用者名稱相同則註冊失敗,重新選擇 ②若使用者名稱不存在則儲存到資料庫 ③1選中登入,若使用者名稱和密碼符合時,登入成功。 程式碼如下: package com.lucfzy; import java.sql.Connection; imp
Django 部落格系統------登入註冊功能
一、登入: urls配置:註冊中需要圖片, url(r'^media/(?P<path>.*)$', serve, {'document_root': settings.MEDIA_ROOT}), 登入login只需用auth驗證一下即可,加了
登入註冊功能——Django
一、定義使用者模型 user/models.py from django.db import models from django.contrib.auth.hashers import make_password class User(models
Android 登入註冊 資料庫SQLite 驗證
展示效果 程式碼區 MainActivity(登入方法) public class MainActivity extends AppCompatActivity { @BindView(R.id.editText) EditTex
登入註冊功能PRD的正確書寫方式
登入註冊功能需求文件【PRD】: 一、功能優化/開發原因 APP登入頁面更改:由於APP登入介面,登入按鈕不突出,並且我們的標準使用者為年齡較大的網際網路低頻人群,學習、記憶能力較差,因此對登入介面UI進行更改並加入簡訊快捷登入。 二、更改模組涉及到的資源 登入頁面
Android登入註冊
Android 登入註冊 學了一段時間的Android開發,通過寫部落格記錄一下學習過程遇到的問題和一些想要分享的東西。 頁面效果如圖所示: 整個登入和註冊頁面都是連線伺服器進行操作的,客戶端向服務端發出請求,服務端接收請求並進行相應的處理。登入過程中,客戶端
day78_淘淘商城專案_11_實現SSO系統的登入註冊功能 + 門戶首頁展示使用者名稱 + ajax請求跨域問題詳解_匠心筆記
淘淘商城專案_11 1、服務介面實現 1.1、檢查資料是否可用介面開發 1.1.1、功能分析 1.1.2、Dao 1.1.3、Service 1.1.4、釋出服務
【tcp-ip學習總結】基於udp的多人聊天室,帶有登入註冊功能
環境;vs2010,vs2013 伺服器端 #include<stdio.h> #include<Winsock2.h> #include<stdlib.h> #pragma comment(lib,"Ws2_32
Android 登入註冊頁面遇到的一些問題方法-小白懂得太少
1.String ver =get_ver.getText().toString().trim() 其中toString().trim(),表示獲得物件的欄位的值,然後轉換成string 型別,並且去掉前後空白trim()是去兩邊空格的方法。toString().trim()
用MVC設計模式實現簡單的JavaWeb登入註冊功能
MVC 全稱Model(模型)-View(檢視)-Controller(控制器),這是一種開發模式,他的好處是可以將介面和業務邏輯分離。 Model(模型),是程式的主體部分,主要包含業務資料和業務邏輯。在模型層,還會涉及到使用者釋出的服務,在服務中會根據不
使用springmvc+mybatis實現增刪改查並進行登入註冊功能
一、第一次使用springmvc+mybatis整合出的框架,使用的是maven版本 1.使用maven有以下主要要建立maven資料夾,匯入maven包並進行配置環境變數,在後面的文章中會提到maven配置。使用maven主要是依賴其pom.xml配置檔案進行匯入jar包