短信验证模板

XML文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <RelativeLayout style="@style/Title_layout">

        <RelativeLayout
            android:id="@+id/mime_authen_back"
            style="@style/Title_back_layout">

            <ImageView style="@style/red_Title_back" />
        </RelativeLayout>

        <TextView
            style="@style/Title_text"
            android:text="身份验证" />
    </RelativeLayout>

    <TextView
        android:id="@+id/authentication"
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:layout_marginLeft="10dp"
        android:gravity="center_vertical"
        android:text="请输入"
        android:textColor="#000" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:background="#fff"
        android:gravity="center_vertical"
        android:orientation="horizontal">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:text="短信验证"
            android:textColor="#000" />

        <View
            android:layout_width="1dp"
            android:layout_height="match_parent"
            android:layout_marginLeft="10dp"
            android:background="#666" />

        <EditText
            android:id="@+id/authen_writecode"
            android:layout_width="100dp"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:background="@null"
            android:hint="短信验证码" />

        <TextView
            android:id="@+id/authen_getcode"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="90dp"
            android:text="获取验证码"
            android:textColor="#f00" />
    </LinearLayout>

    <TextView
        android:id="@+id/authenticaiton_measure"
        android:layout_width="300dp"
        android:layout_height="45dp"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="15dp"
        android:background="@drawable/log_background"
        android:gravity="center"
        android:text="确定"
        android:textColor="#fefefe"
        android:textSize="20sp" />
</LinearLayout>
Activity文件

import android.annotation.TargetApi;
import android.app.ProgressDialog;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.Handler;
import android.os.Message;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import com.android.volley.AuthFailureError;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.umeng.analytics.MobclickAgent;
import com.yyekt.Constants;
import com.yyekt.R;
import com.yyekt.appliaciton.App;
import com.yyekt.utils.MyLog;
import com.yyekt.utils.VolleyUtils;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.HashMap;
import java.util.Map;

public class AuthenticationActivity extends BaseActivity implements View.OnClickListener {

    private TextView authen_getcode,authenticaiton_measure,authentication;
    private EditText authen_writecode;
    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (msg != null) {
                if (msg.what == 88) {
                    if (msg.arg1 > 0) {
                        authen_getcode.setText("(" + msg.arg1 + ")" + "秒可用");
                        authen_getcode.setTextColor(Color.GRAY);
                    } else {
                        authen_getcode.setEnabled(true);
                        authen_getcode.setText("获取验证码");
                        authen_getcode.setTextColor(Color.RED);
                    }
                }

            }
        }
    };
    private String strContent;
    private String phone;
    private RequestQueue requestQueue;
    private String nickName,sex,head;
    private Bundle bundle;
    private ProgressDialog progressDialog;
    private String uid,deviceToken;
    private JSONObject result;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_authentication);
        progressDialog = new ProgressDialog(this);
        progressDialog.setMessage("正在加载...");
        progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);

        Intent intent = getIntent();
        phone = intent.getStringExtra("phone");
        authentication=(TextView)findViewById(R.id.authentication);
        authentication.setText("请输入"+phone+"收到的短信验证码");
        requestQueue= VolleyUtils.getQueue(getApplicationContext());
        authen_getcode=(TextView)findViewById(R.id.authen_getcode);
        authen_getcode.setOnClickListener(this);
        authenticaiton_measure=(TextView)findViewById(R.id.authenticaiton_measure);
        authenticaiton_measure.setOnClickListener(this);
        authen_writecode=(EditText)findViewById(R.id.authen_writecode);
        findViewById(R.id.mime_authen_back).setOnClickListener(this);


    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        switch (id){
            case R.id.authen_getcode:
                progressDialog.show();
                //获取验证码
                StringRequest stringRequest= new StringRequest(StringRequest.Method.POST,Constants.USING_LIBRARY + Constants.VERIFICAITON_CODE, new Response.Listener<String>() {
                    @Override
                    public void onResponse(String s) {
                        try {
                            MyLog.e("Authentication--onclick--url=",Constants.USING_LIBRARY + Constants.VERIFICAITON_CODE);
                            JSONObject jsonObject = new JSONObject(s);
                            MyLog.e("jsonObject",jsonObject.toString());
                            String msg = jsonObject.getString("message");
                            String errorCode = jsonObject.getString("errorCode");
                            String jsessionid = jsonObject.getString("result");
                            App.jsessionid = jsessionid;
                            boolean success = jsonObject.getBoolean("success");
                            if(success){
                                strContent=msg;
                                App.jsessionid = jsessionid;
                                authen_getcode.setEnabled(false);
                                progressDialog.cancel();
                                Toast.makeText(AuthenticationActivity.this, msg, Toast.LENGTH_LONG).show();
                                Thread thread = new Thread(new Runnable() {
                                    int cout = 60;
                                    boolean flag = true;

                                    @Override
                                    public void run() {
                                        while (flag) {
                                            if (cout == 0) {
                                                flag = false;
                                            }
                                            Message message = Message.obtain();
                                            message.what = 88;
                                            message.arg1 = cout--;
                                            handler.sendMessage(message);
                                            try {
                                                Thread.sleep(1000);
                                            } catch (InterruptedException e) {
                                                e.printStackTrace();
                                            }
                                        }
                                    }
                                });
                                thread.start();
                            }else{
                                progressDialog.cancel();
                                Toast.makeText(AuthenticationActivity.this, msg, Toast.LENGTH_LONG).show();
                            }


                        } catch (Exception e) {
                            e.printStackTrace();
                        }

                    }
                }, new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError volleyError) {

                    }
                }) {
                    @Override
                    protected Map<String, String> getParams() throws AuthFailureError {
                        Map<String, String> map = new HashMap<String, String>();
                        map.put("username", phone);
                        map.put("mark", "3");

                        return map;
                    }
                };
                requestQueue.add(stringRequest);
                break;
            case R.id.authenticaiton_measure:
                //确定
                final String writCode=authen_writecode.getText().toString().trim();
                final String soleId = App.getSoleId(this);
                StringRequest stringRequest1= new StringRequest(StringRequest.Method.POST,Constants.USING_LIBRARY + Constants.LOGIN_VERIFICATION+";jsessionid="+App.jsessionid, new Response.Listener<String>() {
                    @Override
                    public void onResponse(String s) {

                        JSONObject jsonObject = null;
                        
//			 try {
//                            jsonObject = new JSONObject(s);
//                            MyLog.e("kkk","authentication--queding--jsonobject"+jsonObject.toString());
//                            String msg = jsonObject.getString("message");
//                            String errorCode = jsonObject.getString("errorCode");
//                            String jsessionid = jsonObject.getString("result");
//                            boolean success = jsonObject.getBoolean("success");
//                            if(success){
//                                Toast.makeText(AuthenticationActivity.this,msg , Toast.LENGTH_SHORT).show();
//                                result = jsonObject.getJSONObject("result");
//                                MyLog.e("kkk", "authentication--soleId"+result.getString("soleId"));
//                                MyLog.e("kkk","authentication--jsessionid"+result.getString("sessionId"));
//                                writeConfig(result);
//                                Intent intent1 = new Intent();
//                                intent1.putExtra("nickname", result.getString("nickname"));
//                                intent1.putExtra("sex", result.getString("sex"));
//                                intent1.putExtra("head", result.getString("head"));
//                                setResult(88, intent1);
//                                finish();
//                            }else{
//                                Toast.makeText(AuthenticationActivity.this,msg+"!!" , Toast.LENGTH_SHORT).show();
//                            }
//                        } catch (JSONException e) {
//                            e.printStackTrace();
//                        }
} }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError volleyError) { } }) { @Override protected Map<String, String> getParams() throws AuthFailureError { Map<String, String> map = new HashMap<String, String>(); map.put("username", phone); map.put("messageCode", writCode);// MyLog.e("kkk", "authentication--queding--messagecode=" + writCode);// MyLog.e("kkk","authentication--queding--soleId="+soleId);// MyLog.e("kkk","authentication--queding--phone="+phone); return map; } }; requestQueue.add(stringRequest1); break; case R.id.mime_authen_back: finish(); break; } } public void writeConfig(JSONObject obj) throws JSONException { SharedPreferences sp = getSharedPreferences("config", MODE_PRIVATE); SharedPreferences.Editor edit = sp.edit(); edit.putString("id", obj.getString("id")); edit.commit(); }}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值