mob短信速成

本文详细介绍了在Android应用中集成短信验证码功能的过程,包括必要的权限声明、MobSDK的注册与使用,以及如何处理短信验证码的发送与验证。同时,提供了完整的代码示例,帮助开发者快速实现短信验证码功能。
摘要由CSDN通过智能技术生成

首先是权限 和 注册

<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();
}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值