ShareSDK短信验证码功能开发(Android)

之前就想在毕设实际的注册里加上手机验证码验证功能,但那时候找了好几个短信网关都不好用而且收费贵。最终还是没做了。

之后,看到 shareSDK 出了这个功能,想试试好久了。

参考文档


官方参考文档

注册 demo


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
public class MainActivity extends Activity {
	
	private RegisterPage registerPage;
	private Button registerBut;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		// 初始化短信 SDK
		SMSSDK.initSDK(this, "2784af5d6350", "b0e314ffc11ce447ad0a0be0156ba12c");
		// 集成的 GUI
		registerPage=new RegisterPage();
		// 注册按钮
		registerBut=(Button) this.findViewById(R.id.button1);
		
		
		registerBut.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				
				registerPage.setRegisterCallback(new EventHandler(){

					@Override
					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");
				                
				                // 验证成功跳转到设置密码、信息界面,继续完成注册
				                Intent intent=new Intent(MainActivity.this, Info.class);
				                intent.putExtra("country", country);
				                intent.putExtra("phone", phone);
				                startActivity(intent);
				        }
					}
					
					
				});
				registerPage.show(MainActivity.this);
			}
		});

	}


}

RegisterPage这个类集成了一个注册 GUI,使用这个类相当方便。

点击按钮触发事件,就进入到它做好了的 UI 界面,直至验证成功。跳出来。

所以这里从在了afterEvent方法,验证成功调用此方法,在这里我做了一个跳转,跳转到Info.class,从而继续以下的注册环节。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
public class Info extends Activity {
	
	private EditText passwordET;
	private Button sureButton;
	private String phone,password,country;
	private RequestQueue rQueue;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.info);
		
		rQueue = Volley.newRequestQueue(this);
		
		Intent intent=getIntent();
		phone=intent.getStringExtra("phone");
		country=intent.getStringExtra("country");
		
		passwordET=(EditText) this.findViewById(R.id.password);
		sureButton=(Button) this.findViewById(R.id.sureButton);
		
		sureButton.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				
				password=passwordET.getText().toString();
				Toast.makeText(Info.this, phone+","+password+","+country, Toast.LENGTH_SHORT).show();
				postData();
				
			}
		});
		
	} 

	public void postData(){
		final String URL = "http://192.168.191.1:8080/ShareSdkMessageService/servlet/Register?phone="+phone+"&password="+password+"&country="+country;

		JsonObjectRequest req = new JsonObjectRequest(URL, null,
		       new Response.Listener<JSONObject>() {
		           @Override
		           public void onResponse(JSONObject response) {

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

		           }
		       });
		rQueue.add(req);
		rQueue.start();
	}

}

在此继续完成注册资料的填写,然后提交到馥务器,写入到数据库。

OK。完成。

同时,shareSDK 同提供了无 GUI 的接口。

无 GUI 开发文档


完成代码点击下载