最近才学了如何使用短信验证的方法。我在这里就说说我知道的两种最简单的。
要做短信验证首先要下载SMSSDK,其下载地址为:http://www.mob.com/#/downloadDetail/SMS/android
这里使用的是ShareSDK下载的。里面需要注册账号,与登录。可以下载eclipse的和studio 的这里主要说的是eclipse的下载完成之后进行解压解压之后将SMSSDK下的两个库文件导入工程中。如果你要使用带UI界面的就将你自己创建的工程与ShortMessafeSDKGUI关联。关联的方法自己百度。如果要是自己创建见面的话就只关联SMSSDK就OK。代码如下:
第一种带UI界面的java代码:
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SMSSDK.initSDK(this, "1523ed29cd044", "be4bd8ada27938eb238c902268d785f4");
// 打开注册页面
RegisterPage registerPage = new RegisterPage();
// registerPage.setRegisterCallback(new EventHandler() {
// public void afterEvent(int event, int result, Object data) {
// // 解析注册结果
// if (result == SMSSDK.RESULT_COMPLETE) {
// @SuppressWarnings("unchecked")
// HashMap
// String country = (String) phoneMap.get("country");
// String phone = (String) phoneMap.get("phone");
// // 提交用户信息
// registerReceiver(country, phone);
// }
// }
// private void registerReceiver(String country, String phone) {
// }
// });
registerPage.show(this);
// 打开通信录好友列表页面
// ContactsPage contactsPage = new ContactsPage();
// contactsPage.show(this);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
清单文件里面配置的代码如下:
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
android:label="@string/app_name" >
android:configChanges="keyboardHidden|orientation|screenSize"
android:theme="@android:style/Theme.Translucent.NoTitleBar"
android:windowSoftInputMode="stateHidden|adjustResize" />
第二种自定义UI的java代码如下:
public class MainActivity extends Activity {
private EditText et_phone;
private EditText et_yan;
private Button bt_ok;
private TextView tv_ok;
private TextView num;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SMSSDK.initSDK(this, "1523ed29cd044", "be4bd8ada27938eb238c902268d785f4");
init();
EventHandler eh = new EventHandler() {
public void afterEvent(int event, int result, Object data) {
if (result == SMSSDK.RESULT_COMPLETE) {
// 回调完成
if (event == SMSSDK.EVENT_SUBMIT_VERIFICATION_CODE) {
Intent i = new Intent(MainActivity.this, SuccessActivity.class);
startActivity(i);
finish();
// 提交验证码成功
} else if (event == SMSSDK.EVENT_GET_VERIFICATION_CODE) {
tv_ok.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
String phone = et_phone.getText().toString().trim();
String yan = et_yan.getText().toString().trim();
if (TextUtils.isEmpty(yan)) {
Toast.makeText(MainActivity.this, "请输入验证码", Toast.LENGTH_LONG).show();
} else {
SMSSDK.submitVerificationCode("86", phone, yan);
}
}
});
// 获取验证码成功
} else if (event == SMSSDK.EVENT_GET_SUPPORTED_COUNTRIES) {
}
} else {
((Throwable) data).printStackTrace();
}
}
};
SMSSDK.registerEventHandler(eh); // 注册短信回调
}
private void init() {
num = (TextView) findViewById(R.id.bt_tv_num);
tv_ok = (TextView) findViewById(R.id.main_tv_ok);
et_phone = (EditText) findViewById(R.id.main_ed_phone);
et_yan = (EditText) findViewById(R.id.main_ed_yan);
bt_ok = (Button) findViewById(R.id.bt_ok);
bt_ok.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
bt_ok.setVisibility(View.INVISIBLE);
num.setVisibility(View.VISIBLE);
CountDown();
String phone = et_phone.getText().toString().trim();
if (TextUtils.isEmpty(phone)) {
Toast.makeText(MainActivity.this, "请输入手机号", Toast.LENGTH_LONG).show();
} else if (phone.length() == 11) {
SMSSDK.getVerificationCode("+86", phone);
} else {
Toast.makeText(MainActivity.this, "你输入的手机号有误,请从新输入!", Toast.LENGTH_LONG).show();
}
}
});
}
// 倒计时
private void CountDown() {
CountDownTimer timer = new CountDownTimer(60000, 1000) {
@Override
public void onTick(long millisUntilFinished) {
num.setText(String.valueOf(millisUntilFinished / 1000));
}
@Override
public void onFinish() {
bt_ok.setVisibility(View.VISIBLE);
num.setVisibility(View.GONE);
}
};
timer.start();
}
}
自定义界面的XML中的代码如下:
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#cccccc"
tools:context="${relativePackage}.${activityClass}" >
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="#ff00"
>
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="输入验证码"
android:textSize="23sp"
android:textColor="#ffffff"
/>
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:text="确定"
android:textColor="#ffffff"
/>
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/main_Rl"
android:layout_centerHorizontal="true"
android:layout_marginTop="18dp"
android:text="请输入有效手机号,并确认数据连接可用" />
android:layout_width="match_parent"
android:layout_height="238dp"
android:layout_alignParentRight="true"
android:layout_below="@+id/textView1"
android:layout_marginTop="16dp"
android:background="#ffffff" >
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="19dp"
android:layout_marginTop="26dp"
android:text="手机号"
android:textSize="18sp"
/>
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView2"
android:layout_below="@+id/textView2"
android:layout_marginTop="17dp"
android:text="验证码"
android:textSize="18sp"
/>
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/textView3"
android:layout_toRightOf="@+id/textView2"
android:background="@drawable/ed_bg_item"
android:maxLines="11"
android:ems="10" />
android:layout_width="78dp"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/textView3"
android:layout_alignBottom="@+id/textView3"
android:layout_alignLeft="@+id/main_ed_phone"
android:background="@drawable/ed_bg_item"
android:maxLines="4"
android:ems="10" >
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/main_Rl_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
android:text="你还以在设置界面进行验证手机号" />
清单文件里面的代码如下:
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
android:label="@string/app_name" >
android:configChanges="keyboardHidden|orientation|screenSize"
android:theme="@android:style/Theme.Translucent.NoTitleBar"
android:windowSoftInputMode="stateHidden|adjustResize" />
android:label="@string/title_activity_success" >