Android 短信发送器

Android中原始系统就带有很多功能
之前讲解过电话发送器的文章,现在我们来说一说短信发送器

短信发送器:逻辑比电话拨号器要难一点

  1. 先是获取手机号码及短信类容
  2. 然后在获取短信管理器实例
  3. 短信中一般有字数限制,拆分循环发送
  4. 获取发送结果

java代码及注解如下:

package com.example.administrator.foundationdemo;

import android.app.Activity;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import java.util.ArrayList;

public class SMSActivity extends AppCompatActivity {

    private EditText sms_phone_number_edittext;
    private EditText sms_text_edittext;
    private Button sms_button;


    PendingIntent sendIntent;
    PendingIntent backIntent;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        init(this);
        smsSending();
    }


    private void init(Context context){

        setContentView(R.layout.activity_sms);

        sms_phone_number_edittext = (EditText) findViewById(R.id.sms_phone_number_edittext);
        sms_text_edittext = (EditText) findViewById(R.id.sms_text_edittext);
        sms_button = (Button) findViewById(R.id.sms_button);

        //处理返回的发送状态
        String SENT_SMS_ACTION = "SENT_SMS_ACTION";
        Intent sentIntent = new Intent(SENT_SMS_ACTION);
        sendIntent= PendingIntent.getBroadcast(context, 0, sentIntent,
                0);
        context.registerReceiver(new BroadcastReceiver() {
            @Override
            public void onReceive(Context _context, Intent _intent) {
                switch (getResultCode()) {
                    case Activity.RESULT_OK:
                        Toast.makeText(SMSActivity.this,
                                "短信发送成功", Toast.LENGTH_SHORT)
                                .show();
                        break;
                    case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
                        Toast.makeText(SMSActivity.this,
                                "短信发送失败", Toast.LENGTH_SHORT)
                                .show();
                        break;
                    case SmsManager.RESULT_ERROR_RADIO_OFF:
                        Toast.makeText(SMSActivity.this,
                                "短信发送失败", Toast.LENGTH_SHORT)
                                .show();
                        break;
                    case SmsManager.RESULT_ERROR_NULL_PDU:
                        Toast.makeText(SMSActivity.this,
                                "短信发送失败", Toast.LENGTH_SHORT)
                                .show();
                        break;
                }
            }
        }, new IntentFilter(SENT_SMS_ACTION));

        //处理返回的接收状态
        String DELIVERED_SMS_ACTION = "DELIVERED_SMS_ACTION";
        // create the deilverIntent parameter
        Intent deliverIntent = new Intent(DELIVERED_SMS_ACTION);
        backIntent= PendingIntent.getBroadcast(context, 0,
                deliverIntent, 0);
        context.registerReceiver(new BroadcastReceiver() {
            @Override
            public void onReceive(Context _context, Intent _intent) {
                Toast.makeText(SMSActivity.this,
                        "收信人已经成功接收", Toast.LENGTH_SHORT)
                        .show();
            }
        }, new IntentFilter(DELIVERED_SMS_ACTION));
    }

    private void smsSending(){
        sms_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String smsPhonenumber = sms_phone_number_edittext.getText().toString();
                String smsText = sms_text_edittext.getText().toString();

                if ("".equals(smsPhonenumber)||"".equals(smsText)){
                    Toast.makeText(SMSActivity.this,"电话号码或短信内容不能为空",Toast.LENGTH_LONG).show();
                    return;
                }
                SmsManager smsManager = SmsManager.getDefault();//获取短信管理器的实例
                ArrayList<String> smsTextsArrayList = smsManager.divideMessage(smsText);//当短信类容超过一条短信类容时,进行拆分
                for (String text:smsTextsArrayList){
                    /**sendTextMessage (String destinationAddress, String scAddress, String text, PendingIntent sentIntent, PendingIntent deliveryIntent)
                     destinationAddress      发送短信的地址(也就是号码)
                     scAddress               短信服务中心,如果为null,就是用当前默认的短信服务中心
                     text                    短信内容
                     sentIntent              如果不为null,当短信发送成功或者失败时,这个PendingIntent会被广播出去成功的结果代码是Activity.RESULT_OK,
                                             或者下面这些错误之一  :RESULT_ERROR_GENERIC_FAILURE,RESULT_ERROR_RADIO_OFF,RESULT_ERROR_NULL_PDU等
                     通俗点说: 发送 -->中国移动 --> 中国移动发送失败 --> 返回发送成功或失败信号 --> 后续处理   即,这个意图包装了短信发送状态的信息
                     deliveryIntent          如果不为null,当这个短信发送到接收者那里,这个PendtingIntent会被广播,
                                             状态报告生成的pdu(指对等层次之间传递的数据单位)会拓展到数据("pdu")
                     通俗点就是:发送 -->中国电信 --> 中国电信发送成功 --> 返回对方是否收到这个信息 --> 后续处理  即:这个意图包装了短信是否被对方收到的状态信息(供应商已经发送成功,但是对方没有收到)。
                     */
                    smsManager.sendTextMessage(smsPhonenumber,null,text,sendIntent,backIntent);
                }
            }
        });
    }



}

XML布局如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.example.administrator.foundationdemo.SMSActivity">

    <EditText
        android:id="@+id/sms_phone_number_edittext"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:singleLine="true"
        android:inputType="number|phone"
        android:hint="请输入电话号码"/>
    <EditText
        android:id="@+id/sms_text_edittext"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:minLines="3"
        android:hint="请输入短信内容"/>
    <Button
        android:id="@+id/sms_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="发送短信"/>
</LinearLayout>

布局效果图:
这里写图片描述

希望能对你们有帮助!!!!!

上一章:Android电话拨号器

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值