android发送短信

在这里插入图片描述

去官网注册 MobSDK ,注册完成之后进入开发者平台创建应用
在这里插入图片描述

在这里插入图片描述

其中App Key和App Secret我们待会要用,然后这边就完事了
在这里插入图片描述

接着到我们应用中来
1、在目录app外的build里添加集成
即 在buildscript下repositories里添加

 maven {

            url "http://mvn.mob.com/android"

        }

在dependencies里添加

 classpath "com.android.tools.build:gradle:4.0.0"
        classpath 'com.mob.sdk:MobSDK:+'

如下图红框 是咱们要添加的
在这里插入图片描述
2、在app内的build里面
添加

// 添加插件
apply plugin: 'com.mob.sdk'

// 在MobSDK的扩展中注册SMSSDK的相关信息
MobSDK {
    appKey "m3005e***"//刚刚的appKey
    appSecret "359494f89e8855e***"//刚刚的appSecret
    SMSSDK {}
}

如下图
在这里插入图片描述
请求短信接口 https://webapi.sms.mob.com/sms/verify
需要传参
RequestBody requestBody = new FormBody.Builder()
.add(“appkey”, appkey)//你的appkey
.add(“zone”, zone)//区号 86
.add(“code”, codes)//接收的验证码
.add(“phone”, phone)//手机号
.build();

注册activity

public class RegisterActivity extends AppCompatActivity {
    EditText username,email,code,password,rePassword;
    private TextView sendCode;
    private int times = 60;
    public String appkey = "m3005e98e**";//你的appkey
    public String zone = "86";

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.register_activity);
        init();
    }


    private void init(){
        sendCode = (TextView)findViewById(R.id.get_code);//发送验证码
        username = (EditText)findViewById(R.id.edusername);
        email = (EditText)findViewById(R.id.edemail);
        code = (EditText)findViewById(R.id.edCode);
        password = (EditText)findViewById(R.id.edpassword);
        rePassword = (EditText)findViewById(R.id.edRepassword);
        Log.i("tests","tests___________");
    }
    public void goBack(View v){//返回
        finish();
    }
    public void getCode(View v) {//获取验证码
        String phone = email.getText().toString();
        countNum();
        //在onCreate方法初始化SDK  短信注册

        MobSDK.init(RegisterActivity.this,"你的appkey","你的appSecret");
        SMSSDK.registerEventHandler(eh);
        Log.i("test","testc"+phone);
        SMSSDK.getVerificationCode("86",phone);
    }

    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) {
                    //提交验证码成功
                    Log.i("EventHandler", "提交验证码成功");
                }else if (event == SMSSDK.EVENT_GET_VERIFICATION_CODE){
                    //获取验证码成功

                    Log.i("EventHandler", "获取验证码成功");
                }else if (event ==SMSSDK.EVENT_GET_SUPPORTED_COUNTRIES){
                    //返回支持发送验证码的国家列表

                    Log.i("EventHandler", "返回支持发送验证码的国家列表");
                }
            }else{
                ((Throwable)data).printStackTrace();
                Log.i("EventHandler", "回调失败");
            }
        }
    };


    private void countNum(){//发送验证码计时
        CountDownTimer timer = new CountDownTimer(60000, 1000) {
            @Override
            public void onTick(long millisUntilFinished) {
                sendCode.setEnabled(false);
                sendCode.setText("已发送(" + millisUntilFinished / 1000 + ")");
            }

            @Override
            public void onFinish() {
                sendCode.setEnabled(true);
                sendCode.setText("重新获取");
            }
        }.start();
    }

    public void registerInfo(View v){//注册
        String phone = email.getText().toString();
        String codes = code.getText().toString();

        RequestBody requestBody = new FormBody.Builder()
                .add("appkey", appkey)
                .add("zone", zone)//区号 86
                .add("code", codes)//接收的验证码
                .add("phone", phone)//手机号
                .build();

        HttpRegister.sendOkHttpRequestCheckCode("https://webapi.sms.mob.com/sms/verify",requestBody, new okhttp3.Callback() {
            @Override
            public void onFailure(Call call, IOException e) {

            }

            @RequiresApi(api = Build.VERSION_CODES.Q)
            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String resposeData = response.body().string();

                Gson gson = new GsonBuilder()
                        .registerTypeAdapter(new TypeToken<Map<String, Long>>(){
                        }.getType(),new MyTypeAdapter()).create();
                Map<String,Long> dataDetail = gson.fromJson(resposeData,new TypeToken<Map<String,Long>>(){

                }.getType());//gson类型转换
                Long codeId = dataDetail.get("status");

                Map<String,String> users = new HashMap<>();
                users.put("userName",username.getText().toString());
                users.put("password",password.getText().toString());
                users.put("phone",email.getText().toString());

                if (codeId == 200) {
                    Log.d("msg","注册成功");

//                    注册逻辑
                    register(users);
                } else if (codeId == 405) {
                    Log.d("msg","AppKey为空");
                } else if (codeId == 406) {
                    Log.d("msg","AppKey无效");
                } else if (codeId == 456) {
                    Log.d("msg","手机号码为空");
                } else if (codeId == 457) {
                    Log.d("msg","手机号码格式错误");
                } else if (codeId == 466) {
                    Log.d("msg","请求验证的验证码为空");
                } else if (codeId == 467) {
                    Log.d("msg","请求验证验证码重复");
                } else if (codeId == 468) {
                    Log.d("msg","验证码错误");
                } else if (codeId == 474) {
                    Log.d("msg","没有打开服务端验证开关");
                } else {
                    throw new IllegalStateException("Unexpected value: " + codeId);
                }
//
            }
        });
    }

    public void register(Map users) {//注册

    }
}

layout

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/bg3"
    >
    <LinearLayout
        android:id="@+id/top"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_alignParentTop="true"
        android:orientation="horizontal"
        >
        <ImageView
            android:layout_marginLeft="@dimen/baseMargin"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:onClick="goBack"
            android:src="@drawable/back"/>
        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:text="@string/top_title2"
            android:layout_marginRight="@dimen/baseMargin"
            android:gravity="center"
            android:textSize="16dp"
            android:textColor="@color/white"/>
    </LinearLayout>
    <LinearLayout
        android:layout_below="@+id/top"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center_vertical"
        android:orientation="vertical"
        >
        <LinearLayout
            android:id="@+id/usernameLay"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="-60dp"
            android:layout_marginRight="@dimen/baseMargin"
            android:layout_marginLeft="@dimen/baseMargin"
            android:orientation="vertical"
            android:layout_below="@+id/top">
            <RelativeLayout
                android:layout_width="wrap_content"
                android:layout_gravity="center"
                android:gravity="center"
                android:layout_height="30dp">
                <ImageView
                    android:id="@+id/textUsername"
                    android:layout_width="wrap_content"
                    android:layout_height="match_parent"
                    android:layout_marginRight="@dimen/baseMargin"
                    android:src="@drawable/xingmingyonghumingnicheng"
                    />
                <EditText
                    android:id="@+id/edusername"
                    android:layout_toRightOf="@+id/textUsername"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:textSize="@dimen/baseSize"
                    android:background="@color/nonColor"
                    android:textColor="@color/likeWhite"
                    android:hint="@string/usernameHint"/>
            </RelativeLayout>
            <View
                android:layout_width="match_parent"
                android:layout_height="2dp"
                android:layout_marginTop="5dp"
                android:background="#ddd"/>
        </LinearLayout>

        <LinearLayout
            android:id="@+id/emailLay"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="20dp"
            android:layout_marginRight="@dimen/baseMargin"
            android:layout_marginLeft="@dimen/baseMargin"
            android:orientation="vertical"
            android:layout_below="@+id/usernameLay">
            <RelativeLayout
                android:layout_width="wrap_content"
                android:layout_gravity="center"
                android:gravity="center"
                android:layout_height="30dp">
                <ImageView
                    android:id="@+id/email"
                    android:layout_width="wrap_content"
                    android:layout_height="match_parent"
                    android:layout_marginRight="@dimen/baseMargin"
                    android:src="@drawable/phone"
                    />
                <EditText
                    android:id="@+id/edemail"
                    android:layout_toRightOf="@+id/email"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:textSize="@dimen/baseSize"
                    android:background="@color/nonColor"
                    android:textColor="@color/likeWhite"
                    android:hint="@string/emailHint"/>
            </RelativeLayout>
            <View
                android:layout_width="match_parent"
                android:layout_height="2dp"
                android:layout_marginTop="5dp"
                android:background="#ddd"/>
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="20dp"
            android:layout_marginRight="@dimen/baseMargin"
            android:layout_marginLeft="@dimen/baseMargin"
            android:orientation="vertical">
            <RelativeLayout
                android:layout_width="wrap_content"
                android:layout_height="30dp">
                <ImageView
                    android:id="@+id/code"
                    android:layout_width="wrap_content"
                    android:layout_height="match_parent"
                    android:layout_marginRight="@dimen/baseMargin"
                    android:src="@drawable/yanzhengma"/>
                <EditText
                    android:id="@+id/edCode"
                    android:layout_toRightOf="@+id/code"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:textSize="@dimen/baseSize"
                    android:background="@color/nonColor"
                    android:textColor="@color/likeWhite"
                    android:hint="@string/code"/>
                <TextView
                    android:id="@+id/get_code"
                    android:layout_alignParentRight="true"
                    android:layout_width="100dp"
                    android:layout_height="match_parent"
                    android:gravity="center"
                    android:textColor="@color/white"
                    android:background="@color/baseColor"
                    android:textSize="@dimen/baseSize"
                    android:onClick="getCode"
                    android:text="@string/getcode"/>
            </RelativeLayout>
            <View
                android:layout_width="match_parent"
                android:layout_height="2dp"
                android:layout_marginTop="5dp"
                android:background="#ddd"/>
        </LinearLayout>

        <LinearLayout
            android:id="@+id/passwordLay"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="20dp"
            android:layout_marginRight="@dimen/baseMargin"
            android:layout_marginLeft="@dimen/baseMargin"
            android:orientation="vertical"
            android:layout_below="@+id/emailLay">
            <RelativeLayout
                android:layout_width="wrap_content"
                android:layout_height="30dp">
                <ImageView
                    android:id="@+id/textPassword"
                    android:layout_width="wrap_content"
                    android:layout_height="match_parent"
                    android:layout_marginRight="@dimen/baseMargin"
                    android:src="@drawable/mima"/>
                <EditText
                    android:id="@+id/edpassword"
                    android:layout_toRightOf="@+id/textPassword"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:textSize="@dimen/baseSize"
                    android:background="@color/nonColor"
                    android:textColor="@color/likeWhite"
                    android:hint="@string/passwordHint"/>
            </RelativeLayout>
            <View
                android:layout_width="match_parent"
                android:layout_height="2dp"
                android:layout_marginTop="5dp"
                android:background="#ddd"/>
        </LinearLayout>
        <LinearLayout
            android:id="@+id/repasswordLay"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="20dp"
            android:layout_marginRight="@dimen/baseMargin"
            android:layout_marginLeft="@dimen/baseMargin"
            android:orientation="vertical"
            android:layout_below="@+id/passwordLay">
            <RelativeLayout
                android:layout_width="wrap_content"
                android:layout_height="30dp">
                <ImageView
                    android:id="@+id/repassword"
                    android:layout_width="wrap_content"
                    android:layout_height="match_parent"
                    android:layout_marginRight="@dimen/baseMargin"
                    android:src="@drawable/mima"/>
                <EditText
                    android:id="@+id/edRepassword"
                    android:layout_toRightOf="@+id/repassword"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:textSize="@dimen/baseSize"
                    android:background="@color/nonColor"
                    android:textColor="@color/likeWhite"
                    android:hint="@string/rePassword"/>
            </RelativeLayout>
            <View
                android:layout_width="match_parent"
                android:layout_height="2dp"
                android:layout_marginTop="5dp"
                android:background="#ddd"/>
        </LinearLayout>

        <LinearLayout
            android:layout_alignParentBottom="true"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:layout_margin="@dimen/baseMargin"
            >
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="35dp">

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="30dp"
                    android:layout_weight="1"
                    android:gravity="right|center_vertical"
                    android:text="@string/top_title1"/>
            </LinearLayout>
            <Button
                android:layout_width="match_parent"
                android:layout_height="50dp"
                android:text="@string/top_title2"
                android:textColor="@color/white"
                android:background="@color/baseColor"
                android:onClick="registerInfo"
                android:textSize="16dp"/>
        </LinearLayout>
    </LinearLayout>

</RelativeLayout>

其中参照很多大佬的成果,时隔有点久找不到链接了
源码:https://github.com/wuyunxiayaya/SendMessage

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android发送短信有几种方法可以实现。一种方法是使用Intent隐式调用系统短信应用进行发送。可以使用以下代码来发送短信: ```java Uri uri = Uri.parse("smsto:10086"); Intent it = new Intent(Intent.ACTION_SENDTO, uri); it.putExtra("sms_body", "Hello World!"); startActivity(it); ``` 另一种方法是使用SmsManager发送短信。可以使用以下代码来发送短信: ```java SmsManager sms = SmsManager.getDefault(); List<String> texts = sms.divideMessage("需要发送短信内容!"); for (String text : texts) { sms.sendTextMessage("10086", null, text, null, null); } ``` 还有一种发送非文本文件的方法,比如加密数据。可以使用以下代码来发送: ```java SmsManager smsm = SmsManager.getDefault(); short port = 1000; PendingIntent pi = PendingIntent.getBroadcast(this, 0, new Intent(), 0); smsm.sendDataMessage(number, null, port, message.getBytes(), pi, null); ``` 这些方法可以根据你的需求选择使用。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [Android 发送短信](https://blog.csdn.net/a451319296/article/details/117857513)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"] - *2* [android 发送短信的两种方式](https://blog.csdn.net/qq_33209777/article/details/94553043)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"] - *3* [android 几种发送短信的方法](https://blog.csdn.net/tianyitianyi1/article/details/18037771)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值