1、Android实现获取短信验证码的功能以及自定义GUI短信验证Android实现获取短信验证码的功能以及自定义GUI短信验证1、 获取SDK请到官网下载最新版本的SDK,下载回来后解压,你会发现有好几个文件,其中“SMSSDK”目录存放的是短信SDK的全部内容,“SMSSDKSample”中保存了短信SDK的演示项目代码,而“SMSSDKSample.apk”则是“SMSSDKSample”的可执行程序。app是这个Project的一个module。如要在这个module里面使用带界面的SMSSDK。就将MobCommons.jar,MobTools.jar,SMSSDK-2.0.2.aar,
2、SMSSDKGUI-2.0.2.aar放到了app的libs目录下。然后在app目录下的build.gradle,加上红圈中的这几句就行了。注意版本号要一致。我这里用的是html view plain copy 在CODE上查看代码片派生到我的代码片repositories flatDir dirs libs /就是你放aar的目录地址 dependencies compile fileTree(include: *.jar, dir: libs) testCompile junit:junit:4.12 compile com.android.support:appcompat-v7:21.
3、0.3 compile name:SMSSDK-2.0.2,ext:aar compile name:SMSSDKGUI-2.0.2,ext:aar 2、配置AndroidManifest.xml 打开您项目的“AndroidManifest.xml”,在其中添加如下的权限:html view plain copy 在CODE上查看代码片派生到我的代码片 如果使用自带的GUI,需要再“application”下添加如下activity:html view plain copy 在CODE上查看代码片派生到我的代码片 3、添加代码启动SDK在您的项目启动时,调用下面的代码:html view p
4、lain copy 在CODE上查看代码片派生到我的代码片SMSSDK.initSDK(this, 您的appkey, 您的appsecret); 发送短信验证码短信SDK内置了开源的GUI功能,您可以通过调用下面的代码打开短信验证页面:html view plain copy 在CODE上查看代码片派生到我的代码片/打开注册页面 RegisterPage registerPage = new RegisterPage(); registerPage.setRegisterCallback(new EventHandler() public void afterEvent(int event,
5、 int result, Object data) / 解析注册结果 if (result = SMSSDK.RESULT_COMPLETE) SuppressWarnings(unchecked) HashMap phoneMap = (HashMap) data; String country = (String) phoneMap.get(country); String phone = (String) phoneMap.get(phone); / 提交用户信息 registerUser(country, phone); ); registerPage.show(context); h
6、tml view plain copy 在CODE上查看代码片派生到我的代码片/ 提交用户信息 private void registerUser(String country, String phone) Random rnd = new Random(); int id = Math.abs(rnd.nextInt(); String uid = String.valueOf(id); String nickName = SmsSDK_User_ + uid; String avatar = AVATARSid % 12; SMSSDK.submitUserInfo(uid, nickNa
7、me, avatar, country, phone); 其中的AVATARS就是一个字符串数组,里面是头像的网络链接而已,这些在下载的SDKDEMO中可查看。4、下面看看如何自定义GUI短信验证而不是调用系统的设计我们的布局文件:html view plain copy 在CODE上查看代码片派生到我的代码片 一个主Activity代码如下:html view plain copy 在CODE上查看代码片派生到我的代码片public class MainActivity extends Activity implements OnClickListener private EditText
8、phone; private EditText cord; private TextView now; private Button getCord; private Button saveCord; private String iPhone; private String iCord; private int time = 60; private boolean flag = true; Override protected void onCreate(Bundle savedInstanceState) super.onCreate(savedInstanceState); reques
9、tWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.activity_main); init(); SMSSDK.initSDK(this, , ); EventHandler eh=new EventHandler() Override public void afterEvent(int event, int result, Object data) Message msg = new Message(); msg.arg1 = event; msg.arg2 = result; msg.obj = data;
10、handler.sendMessage(msg); ; SMSSDK.registerEventHandler(eh); private void init() phone = (EditText) findViewById(R.id.phone); cord = (EditText) findViewById(R.id.cord); now = (TextView) findViewById(R.id.now); getCord = (Button) findViewById(R.id.getcord); saveCord = (Button) findViewById(R.id.savec
11、ord); getCord.setOnClickListener(this); saveCord.setOnClickListener(this); Override public void onClick(View v) switch (v.getId() case R.id.getcord: if(!TextUtils.isEmpty(phone.getText().toString().trim() if(phone.getText().toString().trim().length()=11) iPhone = phone.getText().toString().trim(); S
12、MSSDK.getVerificationCode(86,iPhone); cord.requestFocus(); getCord.setVisibility(View.GONE); else Toast.makeText(MainActivity.this, 请输入完整电话号码, Toast.LENGTH_LONG)how(); phone.requestFocus(); else Toast.makeText(MainActivity.this, 请输入您的电话号码, Toast.LENGTH_LONG).show(); phone.requestFocus(); break; case
13、 R.id.savecord: if(!TextUtils.isEmpty(cord.getText().toString().trim() if(cord.getText().toString().trim().length()=4) iCord = cord.getText().toString().trim(); SMSSDK.submitVerificationCode(86, iPhone, iCord); flag = false; else Toast.makeText(MainActivity.this, 请输入完整验证码, Toast.LENGTH_LONG).show();
14、 cord.requestFocus(); else Toast.makeText(MainActivity.this, 请输入验证码, Toast.LENGTH_LONG).show(); cord.requestFocus(); break; default: break; /验证码送成功后提示文字 private void reminderText() now.setVisibility(View.VISIBLE); handlerText.sendEmptyMessageDelayed(1, 1000); Handler handlerText =new Handler() publi
15、c void handleMessage(Message msg) if(msg.what=1) if(time0) now.setText(验证码已发送+time+秒); time-; handlerText.sendEmptyMessageDelayed(1, 1000); else now.setText(提示信息); time = 60; now.setVisibility(View.GONE); getCord.setVisibility(View.VISIBLE); else cord.setText(); now.setText(提示信息); time = 60; now.set
16、Visibility(View.GONE); getCord.setVisibility(View.VISIBLE); ; ; Handler handler=new Handler() Override public void handleMessage(Message msg) / TODO Ao-generated method stub super.handleMessage(msg); int event = msg.arg1; int result = msg.arg2; Object data = msg.obj; Log.e(event, event=+event); if (
17、result = SMSSDK.RESULT_COMPLETE) /短信注册成功后,返回MainActivity,然后提示新好友 if (event = SMSSDK.EVENT_SUBMIT_VERIFICATION_CODE) /提交验证码成功,验证通过 Toast.makeText(getApplicationContext(), 验证码校验成功, Toast.LENGTH_SHORT).show(); handlerText.sendEmptyMessage(2); else if (event = SMSSDK.EVENT_GET_VERIFICATION_CODE)/服务器验证码发
18、送成功 reminderText(); Toast.makeText(getApplicationContext(), 验证码已经发送, Toast.LENGTH_SHORT).show(); else if (event =SMSSDK.EVENT_GET_SUPPORTED_COUNTRIES)/返回支持发送验证码的国家列表 Toast.makeText(getApplicationContext(), 获取国家列表成功, Toast.LENGTH_SHORT).show(); else if(flag) getCord.setVisibility(View.VISIBLE); Toast.makeText(MainActivity.this, 验证码获取失败,请重新获取, T
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1