隐藏

Android 短信群发功能的实现

发布:2019/4/1 14:48:33作者:管理员 来源:本站 浏览次数:1183

Android 短信群发功能的实现


随着互联网的发展,即时通讯变得越来于重要,逢年过节都要给亲朋好友发祝贺短信,今天我们就来看一下Android如何实现群发短信。
但是在Android 4.4开始系统默认的短信应用才具备短信的手法功能,如果你的手机是4.4以上的系统,那么你就需要的到默认短信应用的权限才能使用短信的发送功能。

实现的程序不算难,程序提供了一个带列表的对话框供用户选择去发短信的对象,即收件人的电话号码。

    // 记录需要群发的号码列表
    ArrayList<String> sendList = new ArrayList<String>();

    1
    2

再使用 SmsManager 向每个电话号码依次发送短信。

for (String number : sendList)
                {
                    // 创建一个PendingIntent对象
                    PendingIntent pi = PendingIntent.getActivity(
                            MainActivity.this, 0, new Intent(), 0);
                    // 发送短信
                    sManager.sendTextMessage(number, null, content
                            .getText().toString(), pi, null);
                }
                // 提示短信群发完成

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10

以下是源代码

public class MainActivity extends Activity
{
    EditText numbers, content;
    Button select, send;
    SmsManager sManager;
    // 记录需要群发的号码列表
    ArrayList<String> sendList = new ArrayList<String>();
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        sManager = SmsManager.getDefault();
        // 获取界面上的文本框、按钮组件
        numbers = (EditText) findViewById(R.id.numbers);
        content = (EditText) findViewById(R.id.content);
        select = (Button) findViewById(R.id.select);
        send = (Button) findViewById(R.id.send);
        // 为send按钮的单击事件绑定监听器
        send.setOnClickListener(new OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                for (String number : sendList)
                {
                    // 创建一个PendingIntent对象
                    PendingIntent pi = PendingIntent.getActivity(
                            MainActivity.this, 0, new Intent(), 0);
                    // 发送短信
                    sManager.sendTextMessage(number, null, content
                            .getText().toString(), pi, null);
                }
                // 提示短信群发完成
                Toast.makeText(MainActivity.this, "短信群发完成"
                        , Toast.LENGTH_SHORT).show();
            }
        });
        // 为select按钮的单击事件绑定监听器
        select.setOnClickListener(new OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                // 查询联系人的电话号码
                final Cursor cursor = getContentResolver().query(
                        ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                        null, null, null, null);
                BaseAdapter adapter = new BaseAdapter()
                {
                    @Override
                    public int getCount()
                    {
                        return cursor.getCount();
                    }
                    @Override
                    public Object getItem(int position)
                    {
                        return position;
                    }
                    @Override
                    public long getItemId(int position)
                    {
                        return position;
                    }
                    @Override
                    public View getView(int position, View convertView,
                                        ViewGroup parent)
                    {
                        cursor.moveToPosition(position);
                        CheckBox rb = new CheckBox(MainActivity.this);
                        // 获取联系人的电话号码,并去掉中间的中画线、空格
                        String number = cursor
                                .getString(cursor.getColumnIndex(ContactsContract
                                        .CommonDataKinds.Phone.NUMBER))
                                .replace("-", "")
                                .replace(" " , "");
                        rb.setText(number);
                        // 如果该号码已经被加入发送人名单,默认勾选该号码
                        if (isChecked(number))
                        {
                            rb.setChecked(true);
                        }
                        return rb;
                    }
                };
                // 加载list.xml布局文件对应的View
                View selectView = getLayoutInflater().inflate(
                        R.layout.list, null);
                // 获取selectView中的名为list的ListView组件
                final ListView listView = (ListView) selectView
                        .findViewById(R.id.list);
                listView.setAdapter(adapter);
                new AlertDialog.Builder(MainActivity.this)
                        .setView(selectView)
                        .setPositiveButton("确定",
                                new DialogInterface.OnClickListener()
                                {
                                    @Override
                                    public void onClick(DialogInterface dialog,
                                                        int which)
                                    {
                                        // 清空sendList集合
                                        sendList.clear();
                                        // 遍历listView组件的每个列表项
                                        for (int i = 0; i < listView.getCount(); i++)
                                        {
                                            CheckBox checkBox = (CheckBox) listView
                                                    .getChildAt(i);
                                            // 如果该列表项被勾选
                                            if (checkBox.isChecked())
                                            {
                                                // 添加该列表项的电话号码
                                                sendList.add(checkBox.getText()
                                                        .toString());
                                            }
                                        }
                                        numbers.setText(sendList.toString());
                                    }
                                }).show();
            }
        });
    }
    // 判断某个电话号码是否已在群发范围内
    public boolean isChecked(String phone)
    {
        for (String s1 : sendList)
        {
            if (s1.equals(phone))
            {
                return true;
            }
        }
        return false;
    }
}

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136

但是存在一个风险,如果发送的联系人过多,容易出现严重的网络延迟。
---------------------
作者:sourai09
来源:CSDN
原文:https://blog.csdn.net/weixin_40599987/article/details/80697682
版权声明:本文为博主原创文章,转载请附上博文链接!