首先是权限 和 注册
<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" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
在application
android:name=“com.mob.MobApplication”
注册
<activity
android:name="com.mob.tools.MobUIShell"
android:configChanges="keyboardHidden|orientation|screenSize"
android:theme="@android:style/Theme.Translucent.NoTitleBar"
android:windowSoftInputMode="stateHidden|adjustResize" />
代码
public class MainActivity extends AppCompatActivity {
private Button btn_phone, btn_yzm;
private EditText et_phone, et_yzm;
private String phone;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//获取控件id
btn_phone = (Button) findViewById(R.id.btn_phone);
btn_yzm = (Button) findViewById(R.id.btn_yzm);
et_phone = (EditText) findViewById(R.id.et_phone);
et_yzm = (EditText) findViewById(R.id.et_yzm);
EventHandler eh=new EventHandler(){
@Override
public void afterEvent(int event, int result, Object data) {
// TODO 此处不可直接处理UI线程,处理后续操作需传到主线程中操作
Message msg = new Message();
msg.arg1 = event;
msg.arg2 = result;
msg.obj = data;
mHandler.sendMessage(msg);
}
};
//注册一个事件回调监听,用于处理SMSSDK接口请求的结果
SMSSDK.registerEventHandler(eh);
btn_phone.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
SMSSDK.getVerificationCode("86", et_phone.getText().toString());
phone = et_phone.getText().toString();
}
});
btn_yzm.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
SMSSDK.submitVerificationCode("86", phone, et_yzm.getText().toString());
}
});
}
Handler mHandler = new Handler() {
public void handleMessage(Message msg) {
// TODO Auto-generated method stub
super.handleMessage(msg);
int event = msg.arg1;
int result = msg.arg2;
Object data = msg.obj;
if (result == SMSSDK.RESULT_COMPLETE) {
//短信注册成功后,返回MainActivity,然后提示新好友
if (event == SMSSDK.EVENT_SUBMIT_VERIFICATION_CODE) {//提交验证码成功
Toast.makeText(getApplicationContext(), "提交验证码成功", Toast.LENGTH_SHORT).show();
} else if (event == SMSSDK.EVENT_GET_VERIFICATION_CODE) {
//已经验证
Toast.makeText(getApplicationContext(), "验证码已经发送", Toast.LENGTH_SHORT).show();
} else if (event == SMSSDK.EVENT_GET_SUPPORTED_COUNTRIES) {
//已经验证
Toast.makeText(getApplicationContext(), "获取国家列表成功", Toast.LENGTH_SHORT).show();
}
} else {
int status = 0;
try {
((Throwable) data).printStackTrace();
Throwable throwable = (Throwable) data;
JSONObject object = new JSONObject(throwable.getMessage());
String des = object.optString("detail");
status = object.optInt("status");
if (!TextUtils.isEmpty(des)) {
Toast.makeText(MainActivity.this, des, Toast.LENGTH_SHORT).show();
return;
}
} catch (Exception e) {
SMSLog.getInstance().w(e);
}
}
}
};
/* //将资源的图片保存到本地图片
public static void savePic(Context context) {
File pic = new File("/sdcard/logo_bluetooth.png");
if (!pic.exists()) {
try {
//把资源文件转成bitmap
Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.ic_launcher_foreground);
//再转成字节数组
ByteArrayOutputStream out = new ByteArrayOutputStream();
int i = 100;
bitmap.compress(Bitmap.CompressFormat.PNG, i, out);
byte[] array = out.toByteArray();
//最后通过流在保存
FileOutputStream fos = new FileOutputStream(pic);
fos.write(array);
fos.close();
} catch (FileNotFoundException e) {
// TODO: handle exception
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}*/
protected void onDestroy() {
super.onDestroy();
SMSSDK.unregisterAllEventHandler();
}
}