短信验证 我用的是mob首先去mob官网下载smsssdk,下载完之后会有俩文件夹
之后是打开
在完后就是
下面直接代码
public class MainActivity extends AppCompatActivity { private EditText editphone; private EditText edityanzheng; private Button btnhuoqu; private Button btnyanzheng; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); editphone = (EditText) findViewById(R.id.editphone); edityanzheng = (EditText) findViewById(R.id.edityanzheng); btnhuoqu = (Button) findViewById(R.id.btnhuoqu); btnyanzheng = (Button) findViewById(R.id.btnyanzheng); SMSSDK.registerEventHandler(eh); btnhuoqu.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { SMSSDK.getVerificationCode("86", editphone.getText().toString().trim(), new OnSendMessageHandler() { @Override public boolean onSendMessage(String s, String s1) { return false; } }); } }); btnyanzheng.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { SMSSDK.submitVerificationCode("86", editphone.getText().toString().trim(), edityanzheng.getText().toString().trim()); } }); } EventHandler eh = new EventHandler() { @Override public void afterEvent(int event, int result, Object data) { if (result == SMSSDK.RESULT_COMPLETE) { //回调完成 if (event == SMSSDK.EVENT_SUBMIT_VERIFICATION_CODE) { //提交验证码正确的回调 runOnUiThread(new Runnable() { @Override public void run() { Toast.makeText(MainActivity.this, "验证成功", Toast.LENGTH_SHORT).show(); } }); } else if (event == SMSSDK.EVENT_GET_VERIFICATION_CODE) { //获取验证码成功 runOnUiThread(new Runnable() { @Override public void run() { Toast.makeText(MainActivity.this, "获取验证码成功", Toast.LENGTH_SHORT).show(); } }); } else if (event == SMSSDK.EVENT_GET_SUPPORTED_COUNTRIES) { //返回支持发送验证码的国家列表 } } else { ((Throwable) data).printStackTrace(); Log.e("tag", ((Throwable) data).getMessage().toString()); //获取验证码成功 runOnUiThread(new Runnable() { @Override public void run() { Toast.makeText(MainActivity.this, "失败", Toast.LENGTH_SHORT).show(); } }); } } }; @Override protected void onDestroy() { super.onDestroy(); SMSSDK.unregisterEventHandler(eh); } }
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@android:color/white" android:orientation="vertical"> <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="手机号" android:id="@+id/editphone"/> <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="验证码" android:id="@+id/edityanzheng"/> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/btnhuoqu" android:text="获取验证码"/> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/btnyanzheng" android:text="验证"/> </LinearLayout>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.shilongxinduanxin"> <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" /> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme" android:name="com.mob.MobApplication"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name="com.mob.tools.MobUIShell" android:theme="@android:style/Theme.Translucent.NoTitleBar" android:configChanges="keyboardHidden|orientation|screenSize" android:windowSoftInputMode="stateHidden|adjustResize"/> <meta-data android:name="Mob-AppKey" android:value="20c5ba6bb818f"/> <meta-data android:name="Mob-AppSecret" android:value="1965928e2b6e6ff7956332ef83b06d52"/> </application> </manifest>
buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } } } repositories{ flatDir{ dirs 'libs' //就是你放aar的目录地址 } } dependencies { compile fileTree(include: ['*.jar'], dir: 'libs') androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', { exclude group: 'com.android.support', module: 'support-annotations' }) compile 'com.android.support:appcompat-v7:26.+' compile 'com.android.support.constraint:constraint-layout:1.0.2' testCompile 'junit:junit:4.12' compile files('libs/MobCommons-2017.0607.1736.jar') compile name:'SMSSDK-3.0.0',ext:'aar' compile name:'SMSSDKGUI-3.0.0',ext:'aar' }