国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

Android開發中通過手機號+短信驗證碼登錄的實例代碼

2019-10-23 18:28:51
字體:
來源:轉載
供稿:網友

首先,需要一個電話號碼,目前很多賬戶都是將賬戶名設置成手機號,然后點擊按鈕獲取手機驗證碼。

其次,你需要后臺給你手機短信的驗證接口,各個公司用的不一樣,這個身為前端,不需要你來考慮,你只要讓你后臺給你寫好接口,你直接調用就好了。

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;//倒計時秒數  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, "不能重復發送驗證碼", 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");         //我這里按照我的要求寫的,你們也可以適當改動             //獲取用戶信息的狀態            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(“這里是你請求的驗證碼接口,讓后臺給你,參數什么的加在后面”);    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();//這里是用來進行請求參數的          } 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開發中通過手機號+短信驗證碼登錄的實例代碼,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對VEVB武林網網站的支持!


注:相關教程知識閱讀請移步到Android開發頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 宜宾市| 聊城市| 鹿泉市| 泾川县| 青浦区| 汶上县| 凯里市| 区。| 崇文区| 沛县| 冀州市| 东乌| 库车县| 海阳市| 同心县| 凌海市| 恭城| 德庆县| 浑源县| 庆元县| 金湖县| 磐石市| 绍兴市| 五寨县| 浦东新区| 内黄县| 博白县| 南召县| 明水县| 新巴尔虎左旗| 利川市| 泗水县| 甘泉县| 广饶县| 赫章县| 平遥县| 高清| 娄烦县| 洱源县| 都昌县| 随州市|