仿京东长城系列15------用户注册,SMSSDK集成

本项目来自菜鸟窝,有兴趣者点击http://www.cniao5.com/course/

项目已经做完,
https://github.com/15829238397/CN5E-shop


仿京东商城系列0------项目简介
仿京东商城系列1------fragmentTabHost实现底部导航栏
仿京东商城系列2------自定义toolbar
仿京东商城系列3------封装Okhttp
仿京东商城系列4------轮播广告条
仿京东商城系列5------商品推荐栏
仿京东商城系列6------下拉刷新上拉加载的商品列表
仿京东商城系列7------商品分类页面
仿京东商城系列8------自定义的数量控制器
仿京东商城系列9------购物车数据存储器实现
仿京东商城系列10------添加购物车,管理购物车功能实现
仿京东商城系列11------商品排序功能以及布局切换实现(Tablayout)
仿京东商城系列12------商品详细信息展示(nativie与html交互)
仿京东商城系列13------商品分享(shareSDK)
仿京东商城系列14------用户登录以及app登录拦截
仿京东长城系列15------用户注册,SMSSDK集成
仿京东商城系列16------支付SDK集成
仿京东商城系列17------支付功能实现
仿京东商城系列18------xml文件读取(地址选择器)
仿京东商城系列19------九宫格订单展示
仿京东商城系列20------终章


前言

本文为大家介绍菜鸟商城app的注册功能实现,以及SMSSDK集成。先上效果图:

用户注册.gif

内容

SMSSDK

一、简介:

短信验证码SDK,为开发者提供全球通用的短信验证码工具,开发者可以用其在App植入短信验证码SDK、简单设置即可短信验证,集成快速便捷,且后期易于管理。

二、功能:
image.png
三、集成方式:

1.官网下载SMSSDK。http://www.mob.com/
2.AS版本的SMSSDK目录下包含以下内容:


MobCommons.jar:Mob 通用公共库(必须)
MobTools.jar:Mob 工具公共库(必须)
SMSSDK-<version>.aar:SMSSDK 核心(必须)
SMSSDKGUI-<version>.aar:SMSSDK GUI 开源库(非必须)
HowToUse.txt:使用说明
注意:如果你同时使用ShareSDK,保留一份公共库就行(公共库版本一致或兼容)。
2.1、将以上文件按需放入Android Studio项目所要使用SMSSDK的Module所在的Libs里面:
smssdk_导入as项目

2.2、在Module的build.gradle里面将libs加入仓库(repositories):
repositories{ flatDir{ dirs 'libs' //就是你放aar的目录地址 }}
2.3、在Module的build.gradle里面添加依赖(dependencies ):
dependencies { ....//你的其他依赖 compile name:'SMSSDK-<version>',ext:'aar' compile name:'SMSSDKGUI-<version>',ext:'aar'}
最终,你的build.gradle看起来应该像这样:
smssdk_as build文件

3.添加代码:
1.配置AndroidManifest.xml
1.1、添加以下权限:

<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.READ_SMS" /><uses-permission android:name="android.permission.GET_TASKS" /><uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

1.2、添加以下Activity:

<activity android:name="com.mob.tools.MobUIShell" android:theme="@android:style/Theme.Translucent.NoTitleBar" android:configChanges="keyboardHidden|orientation|screenSize" android:windowSoftInputMode="stateHidden|adjustResize"/>

1.3、在Application节点下添加以下属性:

android:name="com.mob.MobApplication"

1.4、在Application节点下添加以下meta-data:
<meta-data android:name="Mob-AppKey" android:value="你的AppKey"/><meta-data android:name="Mob-AppSecret" android:value="你的AppSecret"/>
最终,你的AndroidManifest.xml看起来应该像这样:


2.在Activity中注册sdk
2.1、在你的主Activity的onCreate方法中添加以下代码以完成sdk的注册:
@Overrideprotected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // 如果希望在读取通信录的时候提示用户,可以添加下面的代码,并且必须在其他代码调用之前,否则不起作用;如果没这个需求,可以不加这行代码 SMSSDK.setAskPermisionOnReadContact(boolShowInDialog) // 创建EventHandler对象 eventHandler = new EventHandler() { public void afterEvent(int event, int result, Object data) { if (data instanceof Throwable) { Throwable throwable = (Throwable)data; String msg = throwable.getMessage(); Toast.makeText(MainActivity.this, msg, Toast.LENGTH_SHORT).show(); } else { if (event == SMSSDK.EVENT_GET_VERIFICATION_CODE) { // 处理你自己的逻辑 } } } }; // 注册监听器 SMSSDK.registerEventHandler(eventHandler);}
其中EventHandler是短信SDK的操作回调,具体说明文档请参阅:短信SDK操作回调章节。
2.2、在onDestroy中注销SDK:
protected void onDestroy() { super.onDestroy(); SMSSDK.unregisterEventHandler(eventHandler);}
3.关于配置AppKey和AppSecret的说明
配置AppKey和AppSecret有两种方式:
(1)通过AndroidManifest配置
(2)通过代码配置
以上方法择一即可,建议使用第一种方式进行配置。
3.1、通过AndroidManifest配置:
(1)在Application节点下添加以下属性:
android:name="com.mob.MobApplication"
注意:如果你有自己的Application类,那么也可以让你的Application类继承MobApplication即可。
(2)在Application节点下添加以下子节点:
<meta-data android:name="Mob-AppKey" android:value="你的AppKey"/><meta-data android:name="Mob-AppSecret" android:value="你的AppSecret"/>
3.2、通过代码配置:
如果选择通过代码配置,则不需要继承MobApplication,只要在使用SMSSDK之前,调用以下代码:
// 通过代码注册你的AppKey和AppSecretMobSDK.init(context, "你的AppKey", "你的AppSecret");

四、代码混淆

如果你开启了proguard混淆,需要在proguard的rules里面添加以下规则:

# SMSSDK
-dontwarn com.mob.**
-keep class com.mob.**{*;}

-dontwarn cn.smssdk.**
-keep class cn.smssdk.**{*;}

注册功能设计

  • 界面设计较为简单,分为两个页面。效果图如上面动图效果,这里直接上代码。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <com.example.cne_shop.widget.CnToolbar
        android:id="@+id/toolBar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:minHeight="?attr/actionBarSize"
        android:minWidth="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:leftButtonIcon="@drawable/icon_back_32px"
        app:title="用户注册(1/2)"
        android:titleTextColor="@color/white"
        app:isShowSearchView="false"
        app:rightButtonText="下一步"
        ></com.example.cne_shop.widget.CnToolbar>

    <View
        style="@style/line_max_vertical"
        />
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:paddingLeft="5dp"
        android:paddingRight="5dp"
        android:paddingTop="10dp"
        android:paddingBottom="10dp"
        android:background="@color/white"
        >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="国家或者地区"
            android:textSize="18sp" />

        <TextView
            android:id="@+id/country"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="right"
            android:gravity="right"
            android:text="中国"
            android:textSize="18sp" />

    </LinearLayout>
    <View
        style="@style/line_vertical"/>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:paddingLeft="5dp"
        android:paddingRight="5dp"
        android:paddingTop="10dp"
        android:paddingBottom="10dp"
        android:background="@color/white"
       >

        <TextView
            android:id="@+id/countryNum"
            android:layout_width="40dp"
            android:layout_height="wrap_content"
            android:text="+86"
            android:textSize="18sp" />
        <View
            style="@style/line_horizontal"
            />

        <com.example.cne_shop.widget.MyEditText
            android:id="@+id/phone"
            android:layout_width="match_parent"
            android:layout_height="35dp"
            android:background="@color/white"
            android:hint="  请输入常用手机号"
            android:paddingLeft="5dp"
            android:paddingRight="5dp"
            android:enabled="true"
            android:textSize="14sp"
           />
    </LinearLayout>

    <View
        style="@style/line_vertical"/>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:paddingLeft="5dp"
        android:paddingRight="5dp"
        android:paddingTop="10dp"
        android:paddingBottom="10dp"
        android:background="@color/white"
        >

        <TextView
            android:id="@+id/textView"
            android:layout_width="40dp"
            android:layout_height="wrap_content"
            android:text="密码"
            android:textSize="18sp" />

        <View
            style="@style/line_horizontal"
            />

        <com.example.cne_shop.widget.MyEditText
            android:id="@+id/password"
            android:layout_width="match_parent"
            android:layout_height="35dp"
            android:background="@color/white"
            android:hint="  请输入密码"
            android:paddingLeft="5dp"
            android:paddingRight="5dp"
            android:enabled="true"
            android:textSize="14sp"
            />

    </LinearLayout>


    <TextView
        android:paddingLeft="5dp"
        android:paddingRight="5dp"
        android:paddingTop="10dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="点击下一步获取手机验证码"
        android:textColor="@color/cardview_shadow_start_color"
        android:textSize="12sp"/>

</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <com.example.cne_shop.widget.CnToolbar
        android:id="@+id/toolBar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:minHeight="?attr/actionBarSize"
        android:minWidth="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:leftButtonIcon="@drawable/icon_back_32px"
        app:title="用户注册(2/2)"
        android:titleTextColor="@color/white"
        app:isShowSearchView="false"
        app:rightButtonText="完成"
        ></com.example.cne_shop.widget.CnToolbar>
    <View
        style="@style/line_max_vertical"
        />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        >
        <TextView

            android:paddingLeft="5dp"
            android:paddingTop="10dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="我们已发送"
            android:textColor="@color/cardview_shadow_start_color"
            android:textSize="12sp"
            android:paddingBottom="10dp"
            />

        <TextView
            android:paddingTop="10dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="验证码"
            android:textColor="@color/green"
            android:textSize="12sp"
            android:paddingBottom="10dp"/>

        <TextView
            android:paddingTop="10dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="短信到这个号码:+86 "
            android:textColor="@color/cardview_shadow_start_color"
            android:textSize="12sp"
            android:paddingBottom="10dp"/>
        <TextView
            android:paddingTop="10dp"

            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/phone"
            android:text="15829238397"
            android:textColor="@color/cardview_shadow_start_color"
            android:textSize="12sp"
            android:paddingBottom="10dp"/>

    </LinearLayout>



    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:paddingLeft="10dp"
        android:paddingRight="20dp"
        android:paddingTop="10dp"
        android:paddingBottom="10dp"
        android:background="@color/white"
        >

        <Button
            android:layout_width="wrap_content"
            android:layout_height="35dp"
            android:paddingRight="5dp"
            android:paddingLeft="5dp"
            android:layout_gravity="right"
            android:gravity="center"
            android:id="@+id/reRequset"
            android:text="点击重新发送"
            android:layout_alignParentRight="true"
            android:background="@color/green"
            android:textColor="@color/white"/>

        <com.example.cne_shop.widget.MyEditText
            android:id="@+id/identifyCode"
            android:layout_width="match_parent"
            android:layout_height="35dp"
            android:background="@color/white"
            android:hint="  请输入验证码"
            android:layout_alignParentLeft="true"
            android:paddingLeft="5dp"
            android:paddingRight="5dp"
            android:enabled="true"
            android:textSize="14sp"
            />

    </RelativeLayout>

</LinearLayout>
  • 代码实现也十分简单。我们着重对SMSSDK注册和使用进行研究。
    1.自定义一个内部类继承EventHandler。
 public class SMSEvenHandler extends EventHandler{
        @Override
        public void afterEvent(final int event, final int result, final Object data) {

            runOnUiThread(new Runnable() {
                @Override
                public void run() {

                    if (data instanceof Throwable) {
                        Throwable throwable = (Throwable)data;
                        String msg = throwable.getMessage();
                        Toast.makeText(RegisterActivity.this, msg, Toast.LENGTH_SHORT).show();
                        return ;
                    }else {

                        if (result == SMSSDK.RESULT_COMPLETE){
                            if (event == SMSSDK.EVENT_GET_SUPPORTED_COUNTRIES){

                            }else if (event == SMSSDK.EVENT_GET_VERIFICATION_CODE){

                                Log.d("", "run:获取验证码成功----------------------------- ");
                                //请求验证码之后,进行操作
                                afterVerificationCodeRequested((Boolean) data);

                            }else if (event == SMSSDK.EVENT_SUBMIT_VERIFICATION_CODE){
                            }
                        }}
                }
            });
        }
    }

2.注册SMSSDK,并添加SMSSDK事件监听,对获得验证码的结果进行处理。

 smsEvenHandler = new SMSEvenHandler() ;
        SMSSDK.registerEventHandler(smsEvenHandler);


        String[] countryMsg = SMSSDK.getCountry(DEFAUT_COUNTRY_CODE) ;
        if(countryMsg != null){
            country.setText(countryMsg[0]);
            countryNum.setText("+"+countryMsg[1]);
        }

3.提出验证码申请

         phoneCode = phone.getText().toString().trim() ;
        countryCode = countryNum.getText().toString().trim() ;
        passWord = password.getText().toString().trim()  ;

        if(!requestIdentifyCode(phoneCode , countryCode ,passWord)){
            return false;
        }

        SMSSDK.getVerificationCode(countryCode , phoneCode );

4.如果获得验证码成功,跳转到下一页面,验证验证码。

 SMSSDK.submitVerificationCode(countryCode , phoneNum , getCode()) ;
  • 除了上述主线功能之外还设计了一些细节体验,例如60秒以后才能重新发送的计时器等等,在此不做赘述。

有兴趣者请点击页首github地址。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,491评论 5 459
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,856评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,745评论 0 319
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,196评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,073评论 4 355
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,112评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,531评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,215评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,485评论 1 290
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,578评论 2 309
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,356评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,215评论 3 312
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,583评论 3 299
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,898评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,174评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,497评论 2 341
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,697评论 2 335

推荐阅读更多精彩内容