Android短信验证码mob,SMS的使用
来源:互联网 发布:手机淘宝官方下载 编辑:程序博客网 时间:2024/06/05 08:58
1,Android sdutio导入SMS框架
2,代码实现
①,获取手机验证码,
②,提交验证码
③,截取短信中的验证码
一,Android sdutio导入SMS框架
1,下载SMS
2,导入SMS
1,正常导入
2,Module导入:
Module使用SMS,如果用以上方法不行,建议使用以下方法。
或以下方法
①在Android studio的File -> New -> New module进入“create new module”对话框
②、选中“Import .jar/.aar Package”后点击“下一步”,然后在对话框里选择你的aar文件
③、点击“下一步”就导入工程了
④、添加依靠
之后设置manifest。
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.hanhai.group.mynotemob"> <uses-permission android:name="android.permission.READ_CONTACTS" /> <uses-permission android:name="android.permission.READ_PHONE_STATE" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.RECEIVE_SMS" /> <uses-permission android:name="android.permission.SEND_SMS"/> <uses-permission android:name="android.permission.READ_SMS" /> <uses-permission android:name="android.permission.GET_TASKS" /> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> <application android:allowBackup="true" android:label="@string/app_name" android:supportsRtl="true"> <!--这里的Activity是SMS里封装好的一个手机验证,可以直接调用--> <activity android:name="com.mob.tools.MobUIShell" android:theme="@android:style/Theme.Translucent.NoTitleBar" android:configChanges="keyboardHidden|orientation|screenSize" android:windowSoftInputMode="stateHidden|adjustResize"/> <activity android:name=".NoteMobActivity"></activity> </application></manifest>
二,代码实现
Activity代码如下:
public class NoteMobActivity extends AppCompatActivity { @BindView(R.id.et)//findViewById EditText editText; @BindView(R.id.et1)//findViewById EditText editText1; private String APPKEY="1a8c72c356cc0"; private String APPSECRET="bc6478ed89c7ea2b3a1be04e298856ed"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ButterKnife.bind(this);//黄油刀 //初始化SDK SMSSDK.initSDK(this, APPKEY, APPSECRET); SMSSDK.registerEventHandler(eh); //注册短信回调 // 短信监听变化 SmsContent content = new SmsContent(this, new Handler(),editText); // 短信变化监听 this.getContentResolver().registerContentObserver(Uri.parse("content://sms/"), true, content); } @OnClick(R.id.button)//监听 public void button(){ ed=editText1.getText().toString(); if (!ed.equals("")){ Log.i("yyy", "tel "+ed); //请求向手机发送验证码, SMSSDK.getVerificationCode("86", ed);//86为国家代码(注:不是"+86"),ed为手机号码String类型 } } String ed; @OnClick(R.id.button_ok) public void button_ok(){ String a=editText.getText().toString(); if (a!=null) { Log.i("yyy", "editText="+a); //提交验证码,判断验证码是否正确 SMSSDK.submitVerificationCode("86",ed, a);//86为国家代码(注:不是"+86"),ed为手机号码String类型,a为,需要提交的验证码 } } EventHandler eh = new EventHandler() {//注册短信的回调 @Override public void afterEvent(int event, int result, Object data) { Log.i("yyy","yyy:"+event+" result:"+result+" data:"+data.toString()); switch (event) { case SMSSDK.EVENT_SUBMIT_VERIFICATION_CODE://短信验证是否成功的回调 if (result == SMSSDK.RESULT_COMPLETE) { Log.i("yyy", "验证成功");//为了简洁就直接打印log了 } else { Log.i("yyy", "验证失败");//为了简洁就直接打印log了 } break; case SMSSDK.EVENT_GET_VERIFICATION_CODE://短信是否向手机发送成功的回调 if (result == SMSSDK.RESULT_COMPLETE) { Log.i("yyy", "获取验证码成功");//为了简洁就直接打印log了 //默认的智能验证是开启的,我已经在后台关闭 } else { Log.i("yyy", "获取验证码失败");//为了简洁就直接打印log了 } break; } } };}
SmsContent代码如下:
public class SmsContent extends ContentObserver { private String SMS_URI_INBOX = "content://sms/inbox"; private Activity activity = null; private String smsContent = ""; private EditText editText = null; private String tel = "1065712071171471";//读取收件箱中指定的号码 public SmsContent(Activity activity, Handler handler, EditText editText) { super(handler); this.activity = activity; this.editText = editText; } @Override public void onChange(boolean selfChange) { super.onChange(selfChange); Cursor cursor = null;// 光标 // 读取收件箱中指定号码的短信 cursor = activity.managedQuery(Uri.parse(SMS_URI_INBOX), new String[]{"_id", "address", "body", "read"}, "address=? and read=?", new String[]{tel, "1"}, "date desc"); if (cursor != null) {// 如果短信为未读模式 Log.i("yyy", "收到短信 "); cursor.moveToFirst(); if (cursor.moveToFirst()) { String smsbody = cursor.getString(cursor.getColumnIndex("body")); String regEx = "[^0-9]";//正则表达式0到9之间的数字 Pattern p = Pattern.compile(regEx); Matcher m = p.matcher(smsbody.toString()); smsContent = m.replaceAll("").trim().toString(); editText.setText(smsContent); } } }}
XML布局如下:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.hanhai.group.textactivity.MainActivity"> <EditText android:id="@+id/et1" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="输入手机号码" /> <Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="获取短信验证码" /> <EditText android:id="@+id/et" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="请输入验证码" /> <Button android:id="@+id/button_ok" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="提交验证码" /></LinearLayout>
自认为SMS里封装好的手机验证,没多大的用。这里就不必记了!
如若发现有错的地方,还希望各位博友们多多指出!
1 0
- Android短信验证码mob,SMS的使用
- Android studio 下使用mob的短信验证码SMSSDK
- mob短信验证码SMSSDK的使用
- 使用mob实现短信验证码验证
- Mob- 短信验证使用
- 使用mob进行Android短信验证流程
- Android之Mob短信验证简单使用
- [置顶] mob短信验证码SMSSDK的使用
- android mob 短信验证码 功能
- Android 免费短信验证码--Mob.com
- android短信验证码(mob)
- 使用mob获取短信验证码
- Android-使用Mob短信验证码SDK完成短信验证功能
- mob短信验证码SMSSDK的错误代码
- Android之短信验证码(用的Mob短信验证)
- 『实践』Android之短信验证码(用的Mob短信验证)
- android短信验证之Mob
- 【mob】Android短信验证+源码
- 在ios开发中怎么获取应用崩溃日志
- Java对象在虚拟机中的创建过程
- web笔记
- for循环
- UE4 关卡流切换注意问题
- Android短信验证码mob,SMS的使用
- mongodb优化细节
- Linux 使用新磁盘
- 磁盘阵列无法挂载的问题
- background-size值为cover和值为100%的区别
- IE不兼容forEach解决方案
- 关于eclipse导入v4包源码的解决方案
- 微信支付
- ThreadLocal