您好,登錄后才能下訂單哦!
首先,需要一個電話號碼,目前很多賬戶都是將賬戶名設置成手機號,然后點擊按鈕獲取手機驗證碼。
其次,你需要后臺給你手機短信的驗證接口,各個公司用的不一樣,這個身為前端,不需要你來考慮,你只要讓你后臺給你寫好接口,你直接調(diào)用就好了。
activity_login.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <LinearLayout android:layout_width="match_parent" android:layout_height="60dp" android:layout_marginTop="20dp" android:layout_marginLeft="10dp" android:layout_marginRight="10dp" android:gravity="center" android:orientation="horizontal"> <EditText android:id="@+id/mobile_login" android:layout_width="match_parent" android:layout_height="40dp" android:layout_marginLeft="10dp" android:hint="請輸入您的手機號" android:textSize="16sp" android:background="@null" android:inputType="number" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="50dp" android:layout_marginLeft="10dp" android:layout_marginRight="10dp" android:layout_marginTop="15dp" android:gravity="center" android:orientation="horizontal"> <EditText android:id="@+id/yanzhengma" android:layout_width="0dp" android:layout_height="40dp" android:layout_weight="4" android:background="@null" android:layout_marginLeft="10dp" android:hint="請輸入驗證碼" android:textSize="16sp" android:inputType="number" /> <Button android:id="@+id/getyanzhengma1" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1.9" android:textSize="13sp" android:gravity="center" android:text="獲取驗證碼" android:layout_gravity="center" /> </LinearLayout> <Button android:id="@+id/login_btn" android:layout_width="match_parent" android:layout_height="40dp" android:layout_marginTop="20dp" android:layout_marginLeft="20dp" android:layout_marginRight="20dp" android:text="登錄" android:textSize="16sp" /> </LinearLayout>
LoginActivity.java
public class LoginActivity extends Activity implements View.OnClickListener { private int countSeconds = 60;//倒計時秒數(shù) private EditText mobile_login, yanzhengma; private Button getyanzhengma1, login_btn; private Context mContext; private String usersuccess; private Handler mCountHandler = new Handler() { @Override public void handleMessage(Message msg) { super.handleMessage(msg); if (countSeconds > 0) { --countSeconds; getyanzhengma1.setText("(" + countSeconds + ")后獲取驗證碼"); mCountHandler.sendEmptyMessageDelayed(0, 1000); } else { countSeconds = 60; getyanzhengma1.setText("請重新獲取驗證碼"); } } }; private String userinfomsg; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); mContext = this; setContentView(R.layout.activity_login); initView(); initEvent(); initData(); } private void initView() { mobile_login = (EditText) findViewById(R.id.mobile_login); getyanzhengma1 = (Button) findViewById(R.id.getyanzhengma1); yanzhengma = (EditText) findViewById(R.id.yanzhengma); login_btn = (Button) findViewById(R.id.login_btn); } private void initEvent() { getyanzhengma1.setOnClickListener(this); login_btn.setOnClickListener(this); } private void initData() { } @Override public void onClick(View v) { switch (v.getId()) { case R.id.getyanzhengma1: if (countSeconds == 60) { String mobile = mobile_login.getText().toString(); Log.e("tag", "mobile==" + mobile); getMobiile(mobile); } else { Toast.makeText(LoginActivity.this, "不能重復發(fā)送驗證碼", Toast.LENGTH_SHORT).show(); } break; case R.id.login_btn: login(); break; default: break; } } //獲取信息進行登錄 public void login() { String mobile = mobile_login.getText().toString().trim(); String verifyCode = yanzhengma.getText().toString().trim(); RequestParams params = new RequestParams(“這里換成你的請求登錄的接口”); x.http().post(params, new Callback.ProgressCallback<String>() { @Override public void onWaiting() { } @Override public void onStarted() { } @Override public void onLoading(long total, long current, boolean isDownloading) { } @Override public void onSuccess(String result) { try { JSONObject jsonObject = new JSONObject(result); Log.e("tag", "登陸的result=" + jsonObject); String success = jsonObject.optString("success"); String data = jsonObject.optString("data"); String msg=jsonObject.optString("msg"); if ("true".equals(success)) { Log.e("tag","登陸的data="+data); JSONObject json = new JSONObject(data); token = json.optString("token"); userId = json.optString("userId"); //我這里按照我的要求寫的,你們也可以適當改動 //獲取用戶信息的狀態(tài) getUserInfo(); }else{ Toast.makeText(LoginActivity.this, msg, Toast.LENGTH_SHORT).show(); } } catch (JSONException e) { e.printStackTrace(); } } @Override public void onError(Throwable ex, boolean isOnCallback) { } @Override public void onCancelled(CancelledException cex) { } @Override public void onFinished() { } }); } //獲取驗證碼信息,判斷是否有手機號碼 private void getMobiile(String mobile) { if ("".equals(mobile)) { Log.e("tag", "mobile=" + mobile); new AlertDialog.Builder(mContext).setTitle("提示").setMessage("手機號碼不能為空").setCancelable(true).show(); } else if (isMobileNO(mobile) == false) { new AlertDialog.Builder(mContext).setTitle("提示").setMessage("請輸入正確的手機號碼").setCancelable(true).show(); } else { Log.e("tag", "輸入了正確的手機號"); requestVerifyCode(mobile); } } //獲取驗證碼信息,進行驗證碼請求 private void requestVerifyCode(String mobile) { RequestParams requestParams = new RequestParams(“這里是你請求的驗證碼接口,讓后臺給你,參數(shù)什么的加在后面”); x.http().post(requestParams, new Callback.ProgressCallback<String>() { @Override public void onWaiting() { } @Override public void onStarted() { } @Override public void onLoading(long total, long current, boolean isDownloading) { } @Override public void onSuccess(String result) { try { JSONObject jsonObject2 = new JSONObject(result); Log.e("tag", "jsonObject2" + jsonObject2); String state = jsonObject2.getString("success"); String verifyCode = jsonObject2.getString("msg"); Log.e("tag", "獲取驗證碼==" + verifyCode); if ("true".equals(state)) { Toast.makeText(LoginActivity.this, verifyCode, Toast.LENGTH_SHORT).show(); startCountBack();//這里是用來進行請求參數(shù)的 } else { Toast.makeText(LoginActivity.this, verifyCode, Toast.LENGTH_SHORT).show(); } } catch (JSONException e) { e.printStackTrace(); } } @Override public void onError(Throwable ex, boolean isOnCallback) { ex.printStackTrace(); } @Override public void onCancelled(CancelledException cex) { } @Override public void onFinished() { } }); } //使用正則表達式判斷電話號碼 public static boolean isMobileNO(String tel) { Pattern p = Pattern.compile("^(13[0-9]|15([0-3]|[5-9])|14[5,7,9]|17[1,3,5,6,7,8]|18[0-9])\\d{8}$"); Matcher m = p.matcher(tel); System.out.println(m.matches() + "---"); return m.matches(); } //獲取驗證碼信息,進行計時操作 private void startCountBack() { ((Activity) mContext).runOnUiThread(new Runnable() { @Override public void run() { getyanzhengma1.setText(countSeconds + ""); mCountHandler.sendEmptyMessage(0); } }); } }
以上所述是小編給大家介紹的Android開發(fā)中通過手機號+短信驗證碼登錄的實例代碼,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對億速云網(wǎng)站的支持!
免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權內(nèi)容。