安卓笔记侠

专注安卓开发

导航

项目总结手机号+短信验证码登录

首先,需要一个电话号码,目前很多账户都是将账户名设置成手机号,然后点击按钮获取手机验证码。
其次,你需要后台给你手机短信的验证接口,各个公司用的不一样,这个身为前端,不需要你来考虑,你只要让你后台给你写好接口,你直接调用就好了。

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);
            }
        });
    }
}

  

posted on 2017-05-17 22:52  安卓笔记侠  阅读(3252)  评论(0编辑  收藏  举报