短信验证

来源:互联网 发布:免费淘宝账号和密码 编辑:程序博客网 时间:2024/06/05 11:06
MainActivity
    private ImageView tv_qx;
    private EditText ed_phone;
    private TextView textView3;
    private EditText ed_yzm;
    private Button button;
    private int TIME=5;
    private int SECOND=1000;

    Handler timerhandler=new Handler();
    Runnable timerunnable=new Runnable() {
        @Override
        public void run() {
            TIME--;
            if(TIME==0)
            {
                timerhandler.removeCallbacks(this);
                TIME=5;
                textView3.setEnabled(true);
                textView3.setText("再次获取");
            }else
            {
                textView3.setEnabled(false);
                textView3.setTextColor(getResources().getColor(R.color.colorPrimaryDark));
                textView3.setText(TIME+"S");
                timerhandler.postDelayed(this,SECOND);
            }
        }
    };
    private EventHandler eventHandler;
    private ImageView qq;
    private ImageView weixin;
    private ImageView tianyi;
    private ImageView xinxissss;
    private ImageView weibo;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_other);
        setTitle("注册登录");
        initView();
        registerSMS();
    }

    private void registerSMS() {
        // 创建EventHandler对象
        eventHandler = new EventHandler() {
            public void afterEvent(int event, int result, Object data) {
                if (data instanceof Throwable) {
                    Throwable throwable = (Throwable) data;
                    final String msg = throwable.getMessage();
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            Toast.makeText(OtherActivity.this, msg, Toast.LENGTH_SHORT).show();
                        }
                    });

                } else {
                    if (result == SMSSDK.RESULT_COMPLETE) {//只有回服务器验证成功,才能允许用户登录
                        //回调完成,提交验证码成功
                        if (event == SMSSDK.EVENT_SUBMIT_VERIFICATION_CODE) {

                            runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    Toast.makeText(OtherActivity.this, "服务器验证成功", Toast.LENGTH_SHORT).show();

                                }
                            });

                        }

                    } else if (event == SMSSDK.EVENT_GET_VERIFICATION_CODE){
                        //获取验证码成功
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                Toast.makeText(OtherActivity.this,"验证码已发送",Toast.LENGTH_SHORT).show();
                            }
                        });
                    } else if (event == SMSSDK.EVENT_GET_SUPPORTED_COUNTRIES) {
                        //返回支持发送验证码的国家列表

                    }
                }
            }
        };

        // 注册监听器
        SMSSDK.registerEventHandler(eventHandler);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        UMShareAPI.get(this).onActivityResult(requestCode, resultCode, data);
    }
    private void initView() {
        qq = (ImageView) findViewById(R.id.qq);
        qq.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                UMShareAPI.get(OtherActivity.this).getPlatformInfo(OtherActivity.this,SHARE_MEDIA.QQ, umAuthListener);
            }
        });
        weixin = (ImageView) findViewById(R.id.weixin);
        weibo= (ImageView) findViewById(R.id.weibo);
        tianyi = (ImageView) findViewById(R.id.tianyi);
        xinxissss = (ImageView) findViewById(R.id.xinxissss);
        tv_qx = (ImageView) findViewById(R.id.tv_qx);
        tv_qx.setOnClickListener(this);
        ed_phone = (EditText) findViewById(R.id.ed_phone);//输入手机号
        textView3 = (TextView) findViewById(R.id.textView3);//获取验证码
        ed_yzm = (EditText) findViewById(R.id.ed_yzm);//输入验证码
        button = (Button) findViewById(R.id.button);//进入注册
        textView3.setOnClickListener(this);
        button.setOnClickListener(this);
    }


    UMAuthListener umAuthListener=new UMAuthListener()
    {

        @Override
        public void onStart(SHARE_MEDIA share_media) {

        }

        @Override
        public void onComplete(SHARE_MEDIA share_media, int i, Map<String, String> map) {
            String iconurl = map.get("iconurl");
            ImageLoader.getInstance().displayImage(iconurl,qq);
        }

        @Override
        public void onError(SHARE_MEDIA share_media, int i, Throwable throwable) {

        }

        @Override
        public void onCancel(SHARE_MEDIA share_media, int i) {

        }
    };

    @Override
    public void onClick(View view) {
            switch (view.getId())
            {
                case R.id.button:
                    verify();

                    SMSSDK.submitVerificationCode("86",ed_phone.getText().toString(),ed_yzm.getText().toString());

                    break;
                case R.id.textView3:

                    if(TextUtils.isEmpty(ed_phone.getText().toString()))
                    {
                        Toast.makeText(OtherActivity.this,"请输入手机号",Toast.LENGTH_SHORT).show();
                        return;
                    }
                    timerhandler.postDelayed(timerunnable,SECOND);
                    SMSSDK.getVerificationCode("86",ed_phone.getText().toString());
                    break;

            }

    }

    private void verify() {
        if(TextUtils.isEmpty(ed_phone.getText().toString()))
        {
            Toast.makeText(OtherActivity.this,"请输入手机号",Toast.LENGTH_SHORT).show();
            return;
        }
        if(TextUtils.isEmpty(textView3.getText().toString()))
        {
            Toast.makeText(OtherActivity.this,"请输入验证码",Toast.LENGTH_SHORT).show();
            return;
        }

    }


Xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:orientation="vertical"
    android:layout_height="match_parent"
    tools:context="com.example.wangaishi20170830unit1_internet.OtherActivity">

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:id="@+id/linearLayout2">
        <ImageView
            android:id="@+id/tv_qx"
            android:layout_gravity="center_vertical"
            android:src="@drawable/nnm"
            android:layout_marginLeft="300dp"
            android:layout_width="30dp"
            android:layout_height="30dp" />

    </LinearLayout>

    <TextView
        android:text="登录你的头条,精彩永不丢失"
        android:textSize="25dp"
        android:layout_margin="5dp"
        android:gravity="center"
        android:layout_marginTop="21dp"
        android:layout_width="match_parent"
        android:layout_height="35dp"
        android:layout_below="@+id/linearLayout2"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:id="@+id/textView" />

    <EditText
        android:hint="  请输入手机号"
        android:textSize="20dp"
        android:background="@drawable/white1"
        android:layout_width="match_parent"
        android:layout_height="45dp"
        android:layout_margin="20dp"
        android:layout_marginTop="23dp"
        android:layout_marginRight="20dp"
        android:layout_marginLeft="20dp"
        android:layout_below="@+id/textView"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:id="@+id/ed_phone" />

    <TextView
        android:textSize="25dp"
        android:text="| 发送验证码"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginRight="20dp"
        android:layout_marginEnd="20dp"
        android:id="@+id/textView3"
        android:layout_alignBaseline="@+id/ed_phone"
        android:layout_alignBottom="@+id/ed_phone"
        android:layout_alignRight="@+id/ed_phone"
        android:layout_alignEnd="@+id/ed_phone" />

    <EditText
        android:hint="  请输入验证码"
        android:textSize="20dp"
        android:background="@drawable/white1"
        android:layout_width="match_parent"
        android:layout_height="45dp"
        android:id="@+id/ed_yzm"
        android:layout_below="@+id/ed_phone"
        android:layout_alignRight="@+id/ed_phone"
        android:layout_alignEnd="@+id/ed_phone"
        android:layout_alignLeft="@+id/ed_phone"
        android:layout_alignStart="@+id/ed_phone" />

    <TextView
        android:layout_marginTop="5dp"
        android:text="未注册手机验证后自动登录"
        android:layout_width="match_parent"
        android:gravity="center_horizontal"
        android:layout_height="20dp"
        android:id="@+id/textView2"
        android:layout_below="@+id/ed_yzm"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />
    
    <Button
        android:background="@drawable/redhong"
        android:text="进入头条"
        android:textSize="25dp"
        android:textColor="#ffffff"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_marginTop="26dp"
        android:id="@+id/button"
        android:layout_below="@+id/ed_yzm"
        android:layout_alignLeft="@+id/ed_yzm"
        android:layout_alignStart="@+id/ed_yzm"
        android:layout_alignRight="@+id/ed_yzm"
        android:layout_alignEnd="@+id/ed_yzm" />

    <CheckBox
        android:id="@+id/cb"
        android:textSize="15dp"
        android:text="我已阅读并同意“用户协议和隐私条款”"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/button"
        android:layout_centerHorizontal="true" />

    <TextView
        android:text="账号密码登录"
        android:textSize="23dp"
        android:layout_marginRight="20dp"
        android:layout_marginLeft="20dp"
        android:gravity="center_horizontal"
        android:id="@+id/login_pass_name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/cb"
        android:layout_alignLeft="@+id/button"
        android:layout_alignStart="@+id/button" />

    <LinearLayout
        android:gravity="center_horizontal"
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_above="@+id/linearLayout3"
        android:layout_alignLeft="@+id/textView"
        android:layout_alignStart="@+id/textView">

    <ImageView
        android:id="@+id/imageView2"
        android:layout_width="90dp"
        android:layout_height="wrap_content"
        android:src="@drawable/bv" />

        <TextView
            android:text="更多登录方式"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
        <ImageView
            android:id="@+id/imageView3"
            android:layout_width="90dp"
            android:layout_height="wrap_content"
            android:src="@drawable/bc" />
    </LinearLayout>

    <LinearLayout
        android:layout_marginBottom="15dp"
        android:weightSum="5"
        android:orientation="horizontal"
        android:layout_alignParentBottom="true"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/linearLayout3">

        <ImageView
            android:id="@+id/weixin"
            android:src="@drawable/n"
            android:layout_weight="1"
            android:layout_gravity="center"
            android:padding="6dp"
            android:layout_width="60dp"
            android:layout_height="60dp" />
        <ImageView
            android:id="@+id/qq"
            android:layout_weight="1"
            android:layout_gravity="center"
            android:src="@drawable/abc"
            android:padding="6dp"
            android:layout_width="60dp"
            android:layout_height="60dp" />
        <ImageView
            android:id="@+id/weibo"
            android:layout_weight="1"
            android:layout_gravity="center"
            android:src="@drawable/abn"
            android:padding="6dp"
            android:layout_width="60dp"
            android:layout_height="60dp" />
        <ImageView
            android:id="@+id/tianyi"
            android:layout_weight="1"
            android:layout_gravity="center"
            android:src="@drawable/abj"
            android:padding="6dp"
            android:layout_width="60dp"
            android:layout_height="60dp" />
        <ImageView
            android:id="@+id/xinxissss"
            android:layout_weight="1"
            android:layout_gravity="center"
            android:src="@drawable/abdd"
            android:padding="6dp"
            android:layout_width="60dp"
            android:layout_height="60dp" />
    </LinearLayout>
</RelativeLayout>


原创粉丝点击