登錄 http://www.mob.com/ 註冊 創建項目
加入依賴
貼代碼
classpath “com.mob.sdk:MobSDK:2018.0319.1724”
apply plugin: ‘com.mob.sdk’
// 在MobSDK的擴展中註冊SMSSDK的相關信息
MobSDK {
appKey “2e2974aef81c0”
appSecret “1d35b871b02a9c62c62b5ff811bb5c47”
SMSSDK {}
}
不要加錯了
權限
<!-- 權限 短信驗證-->
<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.GET_TASKS"/>
<!-- 短信驗證 提供的activate -->
<activity android:name="com.mob.tools.MobUIShell"
android:configChanges="keyboardHidden|orientation|screenSize"
android:theme="@android:style/Theme.Translucent.NoTitleBar"
android:windowSoftInputMode="stateAlwaysHidden|adjustResize"/>
# 接着帖 我的完整代碼 MainActivity
package wlt.com.zen;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import cn.smssdk.EventHandler;
import cn.smssdk.SMSSDK;
import cn.smssdk.gui.RegisterPage;
public class MainActivity extends AppCompatActivity {
private Button bdsjh,tijiao;
private TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView=(TextView)this.findViewById(R.id.tvsms);
//事件
textView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
sendCode(MainActivity.this);
}
});
bdsjh=(Button)this.findViewById(R.id.bdsjh);
tijiao=(Button)this.findViewById(R.id.tijiao);
tijiao.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
bdsjh.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this,"功能暫時沒有開通....",Toast.LENGTH_LONG).show();
}
});
}
public void sendCode(Context context) {
RegisterPage page = new RegisterPage();
//如果使用我們的ui,沒有申請模板編號的情況下需傳null
page.setTempCode(null);
page.setRegisterCallback(new EventHandler() {
public void afterEvent(int event, int result, Object data) {
if (result == SMSSDK.RESULT_COMPLETE) {
//回調完成
if (event == SMSSDK.EVENT_SUBMIT_VERIFICATION_CODE) {
//提交驗證碼成功 添加動作
Intent intent=new Intent(MainActivity.this,ok_long_Activate.class);
startActivity(intent);
}else if (event == SMSSDK.EVENT_GET_VERIFICATION_CODE){
//獲取驗證碼成功
}else if (event ==SMSSDK.EVENT_GET_SUPPORTED_COUNTRIES){
//返回支持發送驗證碼的國家列表
}
} else{
// TODO 處理錯誤的結果
}
}
});
page.show(context);
}
}
佈局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#FF7788"
android:gravity="center"
>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<TextView
android:textSize="20dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="用戶名:"/>
<EditText
android:hint="請輸入用戶名"
android:layout_width="181dp"
android:layout_height="wrap_content"
android:inputType="text" />
</LinearLayout>
<LinearLayout
android:layout_marginTop="20dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<TextView
android:textSize="20dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="密 碼 :"/>
<EditText
android:hint="請輸入密碼"
android:layout_width="181dp"
android:layout_height="wrap_content"
android:inputType="numberPassword"/>
</LinearLayout>
<LinearLayout
android:layout_marginTop="20dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<Button
android:id="@+id/bdsjh"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="登 錄" />
<Button
android:id="@+id/tijiao"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="注 冊" />
</LinearLayout>
<TextView
android:id="@+id/tvsms"
android:text="手機驗證碼登錄"
android:layout_marginTop="20dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20dp">
</TextView>
</LinearLayout>
驗證成功跳轉的activate
package wlt.com.zen;
import android.app.Activity;
import android.os.Bundle;
public class ok_long_Activate extends Activity {
@Override
protected void onCreate( Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.oklong);
}
}
佈局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:gravity="center"
android:layout_height="match_parent">
<TextView
android:textSize="30dp"
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="歡迎光臨" />
</LinearLayout>
以下是文檔 提供學習參考http://www.mob.com/ 路徑
貼上 相關方法
/ 請求驗證碼,其中country表示國家代碼,如“86”;phone表示手機號碼,如“13800138000”
SMSSDK.getVerificationCode(country, phone);
/ 提交驗證碼,其中的code表示驗證碼,如“1357”
SMSSDK.submitVerificationCode(country, phone, code);
public void sendCode(Context context) {
RegisterPage page = new RegisterPage();
//如果使用我們的ui,沒有申請模板編號的情況下需傳null
page.setTempCode(null);
page.setRegisterCallback(new EventHandler() {
public void afterEvent(int event, int result, Object data) {
if (result == SMSSDK.RESULT_COMPLETE) {
// 處理成功的結果
HashMap<String,Object> phoneMap = (HashMap<String, Object>) data;
// 國家代碼,如“86”
String country = (String) phoneMap.get("country");
// 手機號碼,如“13800138000”
String phone = (String) phoneMap.get("phone");
這裏有個問題 在提交驗證碼 code 參數 不知道 怎麼弄出來 如果相遇大神 求解 一下
// TODO 利用國家代碼和手機號碼進行後續的操作
} else{
// TODO 處理錯誤的結果
}
}
});
page.show(context);
}
// 使用完EventHandler需註銷,否則可能出現內存泄漏
protected void onDestroy() {
super.onDestroy();
SMSSDK.unregisterEventHandler(eventHandler);
}
以上是是在參考文檔下實現的 在真機上測試成功 複製代碼可用 還有好多細節 不解 還需加油 。。