一、參考資料html
Mob網站:http://www.mob.com/java
Mob在Github上的例子:https://github.com/MobClub/SMSSDK-for-Androidandroid
教程:http://www.cnblogs.com/lizhanqi/p/5720962.html git
教程的源代碼: http://git.oschina.net/lizhanqi/MobSMSDemogithub
Dialog教程:http://blog.csdn.net/liang5630/article/details/44098899數據庫
定時器教程:http://www.cnblogs.com/wansho/p/5104335.htmlapp
二、前期準備框架
(1)Mob網站上下載SMS對應的SDK,解壓出來爲圖1,打開圖1,將其中的文件拷貝到as項目中的libs文件夾下,另外在add as library。ide
圖1gradle
圖2
(2)在as項目中的AndroidManifest.xml文件中添加圖3所需權限,因爲SMS權限爲危險權限,因此還要動態申請,這在下面代碼中會有體現。注意權限是寫在application外面,不是裏面。
圖3
(3)若是圖4中的targetSdkVersion大於22,那麼在代碼中必須動態申請權限。
圖4
(4)在as項目中的圖7中的build.gradle文件中添加圖5和圖6紅色框中內容。
圖5
圖6
圖7
(5)在Mob網站上申請App Key和App Secret。
圖8
(6)Toast
三、代碼(沒整理,將就看)
此代碼是不使用Mob自帶的GUI的接口調用
簡單的講一下:
1 package com.gxu.lepao.activity; 2 3 import android.Manifest; 4 import android.app.Dialog; 5 import android.content.DialogInterface; 6 import android.content.Intent; 7 import android.content.pm.PackageManager; 8 import android.os.Bundle; 9 import android.os.CountDownTimer; 10 import android.os.Handler; 11 import android.os.Message; 12 import android.support.v4.app.ActivityCompat; 13 import android.support.v4.content.ContextCompat; 14 import android.support.v7.app.AlertDialog; 15 import android.text.TextUtils; 16 import android.util.Log; 17 import android.view.View; 18 import android.widget.Button; 19 import android.widget.EditText; 20 import android.widget.TextView; 21 import android.widget.Toast; 22 import com.gxu.lepao.R; 23 import com.gxu.lepao.model.UserInfo; 24 import org.litepal.crud.DataSupport;//LitePal開源數據庫框架 25 import java.util.List; 26 import cn.smssdk.EventHandler;//Mob短信驗證碼 27 import cn.smssdk.SMSSDK;//Mob短信驗證碼 28 29 /** 30 * Created by ljy on 2017-05-21. 31 * 獲取和驗證短信驗證碼界面 32 */ 33 34 public class RegisterActivity extends BaseActivity { 35 36 private EditText phoneEdit; 37 private EditText smsEdit; 38 private Button getSms; 39 private Button submitSms; 40 private TextView login; 41 private TextView reset; 42 private CountDownTimer countDownTimer; 43 //倒計時 44 private int TIME = 60; 45 //這是中國區號,若是須要其餘國家列表,可使用getSupportedCountries();得到國家區號 46 public String country = "86"; 47 //APPKEY 48 private static String appKey = "......"; 49 // 填寫從短信SDK應用後臺註冊獲得的APPSECRET 50 private static String appSecret = "......"; 51 //表示是否使用了registerEventHandler 52 private boolean ready; 53 54 55 @Override 56 protected void onCreate(Bundle savedInstanceState) { 57 super.onCreate(savedInstanceState); 58 Log.d("RegisterActivity","Task; id is " + getTaskId()); 59 setContentView(R.layout.activity_register); 60 // 初始化界面 61 initView(); 62 //初始化SMSSDK 63 initSDK();
//獲取驗證碼按鈕監聽 64 getSms.setOnClickListener(new View.OnClickListener(){ 65 @Override 66 public void onClick(View v) { 67 boolean isLogin = false; 68 //判斷手機號碼是否已註冊過 69 isLogin = isRegister(); 70 if(!TextUtils.isEmpty(phoneEdit.getText().toString())){ 71 if(!isLogin){ 72 //發送短信權限 73 if(ContextCompat.checkSelfPermission(RegisterActivity.this 74 ,Manifest.permission.RECEIVE_SMS) != PackageManager.PERMISSION_GRANTED){ 75 //顯示申請權限彈窗 76 showRequestPermission(); 77 }else{ 78 //彈窗確認 79 alterWarning(); 80 } 81 }else{ 82 Toast.makeText(RegisterActivity.this,"此手機號碼已經註冊過",Toast.LENGTH_SHORT).show(); 83 } 84 }else{ 85 Toast.makeText(RegisterActivity.this,"請輸入手機號碼",Toast.LENGTH_SHORT).show(); 86 } 87 } 88 });
//提交驗證碼按鈕監聽 89 submitSms.setOnClickListener(new View.OnClickListener(){ 90 @Override 91 public void onClick(View v) { 92 String sms = smsEdit.getText().toString(); 93 String phone = phoneEdit.getText().toString(); 94 if(!TextUtils.isEmpty(sms)){
//提交驗證碼和手機號碼到Mob,回調在eventHandle中得到 95 SMSSDK.submitVerificationCode( country, phone, sms); 96 }else{ 97 Toast.makeText(RegisterActivity.this,"請輸入驗證碼",Toast.LENGTH_SHORT).show(); 98 } 99 } 100 }); 101 login.setOnClickListener(new View.OnClickListener(){ 102 @Override 103 public void onClick(View v) { 104 Intent intent = new Intent(RegisterActivity.this,LoginActivity.class); 105 startActivity(intent); 106 } 107 }); 108 reset.setOnClickListener(new View.OnClickListener(){ 109 @Override 110 public void onClick(View v) { 111 Intent intent = new Intent(RegisterActivity.this,ResetPwdActivity.class); 112 startActivity(intent); 113 } 114 }); 115 } 116 117 //申請權限彈窗 118 private void showRequestPermission(){ 119 //先new出一個監聽器,設置好監聽 120 DialogInterface.OnClickListener dialogOnclicListener=new DialogInterface.OnClickListener(){ 121 @Override 122 public void onClick(DialogInterface dialog, int which) { 123 switch(which){ 124 case Dialog.BUTTON_POSITIVE: 125 //接收短信權限 126 ActivityCompat.requestPermissions(RegisterActivity.this,new String[]{Manifest.permission.RECEIVE_SMS},1); 127 //發送短信權限 128 ActivityCompat.requestPermissions(RegisterActivity.this,new String[]{Manifest.permission.SEND_SMS},2); 129 //彈窗確認 130 alterWarning(); 131 break; 132 case Dialog.BUTTON_NEGATIVE: 133 Toast.makeText(RegisterActivity.this, "拒絕" + which, Toast.LENGTH_SHORT).show(); 134 break; 135 } 136 } 137 }; 138 //dialog參數設置 139 AlertDialog.Builder builder=new AlertDialog.Builder(RegisterActivity.this); //先獲得構造器 140 builder.setTitle("申請權限"); //設置標題 141 builder.setMessage("要容許lepao接收驗證碼短信嗎?"); //設置內容 142 builder.setIcon(R.mipmap.ic_launcher);//設置圖標,圖片id便可 143 builder.setPositiveButton("容許",dialogOnclicListener); 144 builder.setNegativeButton("拒絕", dialogOnclicListener); 145 builder.create().show(); 146 } 147 148 //申請權限 149 public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) { 150 switch (requestCode){ 151 case 1: 152 if(grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED){ 153 //initSDK(); 154 Toast.makeText(this,"你受權。。。接收短信權限",Toast.LENGTH_SHORT).show(); 155 }else{ 156 Toast.makeText(this,"你拒絕。。。接收短信權限",Toast.LENGTH_SHORT).show(); 157 } 158 break; 159 case 2: 160 if(grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED){ 161 Toast.makeText(this,"你受權。。。發送短信權限",Toast.LENGTH_SHORT).show(); 162 }else{ 163 Toast.makeText(this,"你拒絕。。。發送短信權限",Toast.LENGTH_SHORT).show(); 164 } 165 break; 166 default: 167 } 168 } 169 170 // 初始化短信SDK 171 private void initSDK() { 172 SMSSDK.initSDK(this, appKey, appSecret, true); 173 //final Handler handler = new Handler(); 174 EventHandler eventHandler = new EventHandler() { 175 public void afterEvent(int event, int result, Object data) { 176 //回調完成 177 if (result == SMSSDK.RESULT_COMPLETE) 178 { 179 //驗證碼驗證成功 180 if (event == SMSSDK.EVENT_SUBMIT_VERIFICATION_CODE) 181 { 182 toast("驗證成功"); 183 Intent intent = new Intent(RegisterActivity.this,SetPwdActivity.class); 184 intent.putExtra("phone",phoneEdit.getText().toString()); 185 startActivity(intent); 186 finish(); 187 } 188 //已發送驗證碼 189 else if (event == SMSSDK.EVENT_GET_VERIFICATION_CODE) 190 { 191 toast("驗證碼已經發送"); 192 } else 193 { 194 ((Throwable) data).printStackTrace(); 195 String str = data.toString(); 196 toast(str); 197 } 198 } 199 if(result==SMSSDK.RESULT_ERROR) { 200 toast("驗證碼錯誤"); 201 } 202 203 } 204 }; 205 // 註冊回調監聽接口 206 SMSSDK.registerEventHandler(eventHandler); 207 ready = true; 208 } 209 210 //吐司的一個小方法 211 private void toast(final String str) { 212 runOnUiThread(new Runnable() { 213 @Override 214 public void run() { 215 Toast.makeText(RegisterActivity.this, str, Toast.LENGTH_SHORT).show(); 216 } 217 }); 218 } 219 220 221 222 //銷燬短信註冊 223 @Override 224 protected void onDestroy() { 225 // 註銷回調接口registerEventHandler必須和unregisterEventHandler配套使用,不然可能形成內存泄漏。 226 if(ready){ 227 SMSSDK.unregisterAllEventHandler(); 228 } 229 super.onDestroy(); 230 } 231 232 //初始化界面 233 private void initView(){ 234 phoneEdit = (EditText) findViewById(R.id.phone); 235 smsEdit = (EditText) findViewById(R.id.sms); 236 getSms = (Button) findViewById(R.id.getSms); 237 submitSms = (Button) findViewById(R.id.submitSms); 238 login = (TextView) findViewById(R.id.login); 239 reset = (TextView) findViewById(R.id.reset); 240 } 241 242 //彈窗確認 243 private void alterWarning(){ 244 //先new出一個監聽器,設置好監聽 245 DialogInterface.OnClickListener dialogOnclicListener=new DialogInterface.OnClickListener(){ 246 @Override 247 public void onClick(DialogInterface dialog, int which) { 248 switch(which){ 249 case Dialog.BUTTON_POSITIVE: 250 dialog.dismiss(); 251 //經過sdk發送短信驗證(請求獲取短信驗證碼,在監聽(eventHandle)中返回) 252 SMSSDK.getVerificationCode(country, phoneEdit.getText().toString()); 253 //設置獲取驗證碼按鈕不能點擊 254 getSms.setClickable(false); 255 //倒計時,執行次數爲(TIME+1)*1000/1000,countDownTimer每次執行間隔:1000(單位爲毫秒) 256 countDownTimer = new CountDownTimer((TIME+1)*1000, 1000) { 257 @Override 258 public void onTick(long millisUntilFinished) { 259 getSms.setText(TIME-- + "秒後再次獲取驗證碼"); 260 } 261 262 @Override 263 public void onFinish() { 264 //設置獲取驗證碼按鈕能夠點擊 265 getSms.setClickable(true); 266 getSms.setText("點擊獲取短信驗證碼"); 267 } 268 }; 269 countDownTimer.start(); 270 break; 271 case Dialog.BUTTON_NEGATIVE: 272 dialog.dismiss(); 273 Toast.makeText(RegisterActivity.this, "已取消", Toast.LENGTH_SHORT).show(); 274 break; 275 } 276 } 277 }; 278 //dialog參數設置 279 AlertDialog.Builder builder=new AlertDialog.Builder(this); //先獲得構造器 280 builder.setTitle("發送短信"); //設置標題 281 builder.setMessage("咱們將把驗證碼發送到如下號碼:\n"+country+"-"+phoneEdit.getText().toString()); //設置內容 282 builder.setIcon(R.drawable.icon);//設置圖標,圖片id便可 283 builder.setPositiveButton("確認",dialogOnclicListener); 284 builder.setNegativeButton("取消", dialogOnclicListener); 285 builder.create().show(); 286 } 287 288 //判斷手機號碼是否已註冊過 289 public boolean isRegister(){ 290 boolean isLogin = false; 291 。。。。。299 return isLogin; 300 } 301 302 303 }