仿京東長城系列15------用戶注冊,SMSSDK集成

本項目來自菜鳥窩,有興趣者點擊http://www.cniao5.com/course/

項目已經(jīng)做完,
https://github.com/15829238397/CN5E-shop


仿京東商城系列0------項目簡介
仿京東商城系列1------fragmentTabHost實現(xiàn)底部導(dǎo)航欄
仿京東商城系列2------自定義toolbar
仿京東商城系列3------封裝Okhttp
仿京東商城系列4------輪播廣告條
仿京東商城系列5------商品推薦欄
仿京東商城系列6------下拉刷新上拉加載的商品列表
仿京東商城系列7------商品分類頁面
仿京東商城系列8------自定義的數(shù)量控制器
仿京東商城系列9------購物車數(shù)據(jù)存儲器實現(xiàn)
仿京東商城系列10------添加購物車,管理購物車功能實現(xiàn)
仿京東商城系列11------商品排序功能以及布局切換實現(xiàn)(Tablayout)
仿京東商城系列12------商品詳細信息展示(nativie與html交互)
仿京東商城系列13------商品分享(shareSDK)
仿京東商城系列14------用戶登錄以及app登錄攔截
仿京東長城系列15------用戶注冊,SMSSDK集成
仿京東商城系列16------支付SDK集成
仿京東商城系列17------支付功能實現(xiàn)
仿京東商城系列18------xml文件讀取(地址選擇器)
仿京東商城系列19------九宮格訂單展示
仿京東商城系列20------終章


前言

本文為大家介紹菜鳥商城app的注冊功能實現(xiàn),以及SMSSDK集成。先上效果圖:

用戶注冊.gif

內(nèi)容

SMSSDK

一、簡介:

短信驗證碼SDK,為開發(fā)者提供全球通用的短信驗證碼工具,開發(fā)者可以用其在App植入短信驗證碼SDK、簡單設(shè)置即可短信驗證,集成快速便捷,且后期易于管理。

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

1.官網(wǎng)下載SMSSDK。http://www.mob.com/
2.AS版本的SMSSDK目錄下包含以下內(nèi)容:


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_導(dǎo)入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看起來應(yīng)該像這樣:
smssdk_as build文件

3.添加代碼:
1.配置AndroidManifest.xml
1.1、添加以下權(quán)限:

<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節(jié)點下添加以下屬性:

android:name="com.mob.MobApplication"

1.4、在Application節(jié)點下添加以下meta-data:
<meta-data android:name="Mob-AppKey" android:value="你的AppKey"/><meta-data android:name="Mob-AppSecret" android:value="你的AppSecret"/>
最終,你的AndroidManifest.xml看起來應(yīng)該像這樣:


2.在Activity中注冊sdk
2.1、在你的主Activity的onCreate方法中添加以下代碼以完成sdk的注冊:
@Overrideprotected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // 如果希望在讀取通信錄的時候提示用戶,可以添加下面的代碼,并且必須在其他代碼調(diào)用之前,否則不起作用;如果沒這個需求,可以不加這行代碼 SMSSDK.setAskPermisionOnReadContact(boolShowInDialog) // 創(chuàng)建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) { // 處理你自己的邏輯 } } } }; // 注冊監(jiān)聽器 SMSSDK.registerEventHandler(eventHandler);}
其中EventHandler是短信SDK的操作回調(diào),具體說明文檔請參閱:短信SDK操作回調(diào)章節(jié)。
2.2、在onDestroy中注銷SDK:
protected void onDestroy() { super.onDestroy(); SMSSDK.unregisterEventHandler(eventHandler);}
3.關(guān)于配置AppKey和AppSecret的說明
配置AppKey和AppSecret有兩種方式:
(1)通過AndroidManifest配置
(2)通過代碼配置
以上方法擇一即可,建議使用第一種方式進行配置。
3.1、通過AndroidManifest配置:
(1)在Application節(jié)點下添加以下屬性:
android:name="com.mob.MobApplication"
注意:如果你有自己的Application類,那么也可以讓你的Application類繼承MobApplication即可。
(2)在Application節(jié)點下添加以下子節(jié)點:
<meta-data android:name="Mob-AppKey" android:value="你的AppKey"/><meta-data android:name="Mob-AppSecret" android:value="你的AppSecret"/>
3.2、通過代碼配置:
如果選擇通過代碼配置,則不需要繼承MobApplication,只要在使用SMSSDK之前,調(diào)用以下代碼:
// 通過代碼注冊你的AppKey和AppSecretMobSDK.init(context, "你的AppKey", "你的AppSecret");

四、代碼混淆

如果你開啟了proguard混淆,需要在proguard的rules里面添加以下規(guī)則:

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

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

注冊功能設(shè)計

  • 界面設(shè)計較為簡單,分為兩個頁面。效果圖如上面動圖效果,這里直接上代碼。
<?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="國家或者地區(qū)"
            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="我們已發(fā)送"
            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="點擊重新發(fā)送"
            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>
  • 代碼實現(xiàn)也十分簡單。我們著重對SMSSDK注冊和使用進行研究。
    1.自定義一個內(nèi)部類繼承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事件監(jiān)聽,對獲得驗證碼的結(jié)果進行處理。

 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.如果獲得驗證碼成功,跳轉(zhuǎn)到下一頁面,驗證驗證碼。

 SMSSDK.submitVerificationCode(countryCode , phoneNum , getCode()) ;
  • 除了上述主線功能之外還設(shè)計了一些細節(jié)體驗,例如60秒以后才能重新發(fā)送的計時器等等,在此不做贅述。

有興趣者請點擊頁首github地址。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容