public class MainActivity extends AppCompatActivity implements View.OnClickListener{ private EditText phonenumber; private EditText et2; private TextView yanzhengma; private TextView yanzheng; private TextView duanxinyanzheng; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); phonenumber = (EditText) findViewById(R.id.phonenumber); et2 = (EditText) findViewById(R.id.et2); yanzhengma = (TextView) findViewById(R.id.yanzhengma); yanzheng = (TextView) findViewById(R.id.yanzheng); duanxinyanzheng = (TextView) findViewById(R.id.duanxinyanzheng); yanzheng.setOnClickListener(this); yanzhengma.setOnClickListener(this); duanxinyanzheng.setOnClickListener(this); SMSSDK.registerEventHandler(eh); } @Override public void onClick(View v) { switch (v.getId()){ case R.id.duanxinyanzheng:{ //打开注册页面 RegisterPage registerPage = new RegisterPage(); registerPage.setRegisterCallback(new EventHandler() { public void afterEvent(int event, int result, Object data) { // 解析注册结果 if (result == SMSSDK.RESULT_COMPLETE) { @SuppressWarnings("unchecked") HashMap<String,Object> phoneMap = (HashMap<String, Object>) data; String country = (String) phoneMap.get("country"); String phone = (String) phoneMap.get("phone"); } } }); registerPage.show(MainActivity.this); } break; case R.id.yanzhengma:{ SMSSDK.getVerificationCode("86", phonenumber.getText().toString().trim(), new OnSendMessageHandler(){ @Override public boolean onSendMessage(String s, String s1) { return false; } }); } break; case R.id.yanzheng:{ SMSSDK.submitVerificationCode("86", phonenumber.getText().toString().trim(), et2.getText().toString().trim()); } break; } } EventHandler eh=new EventHandler(){ @Override public void afterEvent(int event, int result, Object data) { if (result == SMSSDK.RESULT_COMPLETE) { //回调完成 if (event == SMSSDK.EVENT_SUBMIT_VERIFICATION_CODE) { runOnUiThread(new Runnable() { @Override public void run() { Toast.makeText(MainActivity.this, "验证成功", Toast.LENGTH_SHORT).show(); } }); //提交验证码成功 }else if (event == SMSSDK.EVENT_GET_VERIFICATION_CODE){ runOnUiThread(new Runnable() { @Override public void run() { Toast.makeText(MainActivity.this, "获取验证码成功", Toast.LENGTH_SHORT).show(); } }); //获取验证码成功 }else if (event ==SMSSDK.EVENT_GET_SUPPORTED_COUNTRIES){ //返回支持发送验证码的国家列表 } }else{ ((Throwable)data).printStackTrace(); runOnUiThread(new Runnable() { @Override public void run() { Toast.makeText(MainActivity.this, "失败", Toast.LENGTH_SHORT).show(); } }); } } }; @Override protected void onDestroy() { super.onDestroy(); SMSSDK.registerEventHandler(eh); } } 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"> <EditText android:id="@+id/phonenumber" android:layout_width="280dp" android:layout_height="wrap_content" android:hint="请输入手机号" /> <LinearLayout android:layout_width="350dp" android:layout_height="wrap_content" android:layout_marginTop="15dp" android:orientation="horizontal"> <EditText android:id="@+id/et2" android:layout_width="150dp" android:layout_height="wrap_content" android:hint="请输入验证码" android:onClick="onClick" /> <TextView android:id="@+id/yanzhengma" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="10dp" android:onClick="onClick" android:padding="10dp" android:text="获取验证码" android:textSize="19dp" /> </LinearLayout> <TextView android:id="@+id/yanzheng" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="15dp" android:text="验证" android:textSize="25dp" /> <TextView android:id="@+id/duanxinyanzheng" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:layout_margin="15dp" android:text="短信验证" android:textSize="25dp" /> </LinearLayout>
短信验证
最新推荐文章于 2019-08-01 18:36:46 发布