短信驗證——第三方實現

第一步:導入SDK所在的Libs裏面android

第二步:build.gradle裏面將libs加入倉庫(repositories):ide

repositories{
    flatDir{
        dirs 'libs' //就是你放aar的目錄地址
    }
}

第三步:build.gradle裏面添加依賴(dependencies ):gradle

//你的其餘依賴
compile(name: 'SMSSDK-3.0.0', ext: 'aar')
compile(name: 'SMSSDKGUI-3.0.0', ext: 'aar')

第四步:配置AndroidManifest.xmlui

1.添加如下權限:this

<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" />

2添加如下Activity:xml

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

3在Application節點下添加如下屬性:對象

android:name="com.mob.MobApplication"

4在Application節點下添加如下meta-data:ci

<meta-data
    android:name="Mob-AppKey"
    android:value="221607712f160" />
<meta-data
    android:name="Mob-AppSecret"
    android:value="0f6ddf18f8f69bbb3fa8f1b29967c138" />

 

別忘了去http://wiki.mob.com/sdk-sms-android-3-0-0/獲取AppkeyheAppSecretget

5代碼混淆:it

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

第五步:在你的主Activity的onCreate方法中添加如下代碼以完成sdk的註冊:

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private EditText dianhua;
    private EditText yanzhengma;
    private Button but1;
    private Button but2;
    private EventHandler eventHandler;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        // 若是但願在讀取通訊錄的時候提示用戶,能夠添加下面的代碼,而且必須在其餘代碼調用以前,不然不起做用;若是沒這個需求,能夠不加這行代碼
        // SMSSDK.setAskPermisionOnReadContact(boolShowInDialog)

        // 建立EventHandler對象
        eventHandler = new EventHandler() {
            public void afterEvent(int event, int result, Object data) {
                if (data instanceof Throwable) {
                    Throwable throwable = (Throwable)data;
                    final String msg = throwable.getMessage();
                    Log.i("login",msg+"------");
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            Toast.makeText(MainActivity.this, msg, Toast.LENGTH_SHORT).show();
                        }
                    });
                } else {
                    if (event == SMSSDK.EVENT_GET_VERIFICATION_CODE) {
                    }
                    else if(event==SMSSDK.EVENT_SUBMIT_VERIFICATION_CODE){
                        Log.i("login","驗證成功");
                        Intent intent = new Intent(MainActivity.this,Main2Activity.class);
                        startActivity(intent);
                    }
                }
            }
        };
        // 註冊監聽器
        SMSSDK.registerEventHandler(eventHandler);
    }
    //在onDestroy中註銷SDK
    protected void onDestroy() {
        super.onDestroy();
        SMSSDK.unregisterEventHandler(eventHandler);
    }
    private void initView() {
        dianhua = (EditText) findViewById(R.id.dianhua);
        yanzhengma = (EditText) findViewById(R.id.yanzhengma);
        but1 = (Button) findViewById(R.id.but1);
        but2 = (Button) findViewById(R.id.but2);
        but1.setOnClickListener(this);
        but2.setOnClickListener(this);
    }
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.but1:
                SMSSDK.getVerificationCode("86", dianhua.getText().toString());
                break;
            case R.id.but2:
                SMSSDK.submitVerificationCode("86",dianhua.getText().toString(),yanzhengma.getText().toString());
                break;
        }
    }
相關文章
相關標籤/搜索