Android SmsManager 短信群发

news/2024/7/5 11:41:56

AndroidManifest.xml

    <uses-permission android:name="android.permission.READ_PHONE_STATE" />
    <uses-permission android:name="android.permission.SEND_SMS" />

list.xml

<?xml version="1.0" encoding="UTF-8"?>  
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"   
    android:orientation="vertical"   
    android:layout_width="fill_parent"   
    android:layout_height="fill_parent"   
    >   
<ListView     
    android:id="@+id/list"   
    android:layout_width="fill_parent"    
    android:layout_height="wrap_content"    
    /> 
</LinearLayout>  
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="shortcut.song.com.myapplication.GroupSendSmsActivity">

    <EditText
        android:id="@+id/edit_number"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
    <EditText
        android:id="@+id/edit_mgs"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
    <Button
        android:id="@+id/btn_select"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Select"/>
    <Button
        android:id="@+id/btn_send_gsms"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="GroupSms"/>
</LinearLayout>
package shortcut.song.com.myapplication;

import android.app.PendingIntent;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.provider.ContactsContract;
import android.provider.Telephony;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;

import java.lang.reflect.Array;
import java.util.ArrayList;

public class GroupSendSmsActivity extends AppCompatActivity {
    EditText numbers, sendmessage;
    Button btnSelect, btnSend;
    SmsManager smsManager;
    // 记录群发的号码列表
    ArrayList<String> sendList = new ArrayList<String>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_group_send_sms);

        smsManager = SmsManager.getDefault();

        numbers = (EditText)findViewById(R.id.edit_number);
        sendmessage = (EditText)findViewById(R.id.edit_mgs);
        btnSelect = (Button)findViewById(R.id.btn_select);
        btnSend = (Button)findViewById(R.id.btn_send_gsms);

        btnSend.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                for (String number : sendList) {
                    // 创建PendingIntent对象
                    PendingIntent pi = PendingIntent.getActivity(GroupSendSmsActivity.this, 0, new Intent(), 0);
                    // 发送短信
                    smsManager.sendTextMessage(number, null, sendmessage.getText().toString(), pi, null);
                }
                Toast.makeText(GroupSendSmsActivity.this, "发送完成", Toast.LENGTH_SHORT).show();
            }
        });

        btnSelect.setOnClickListener(new View.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(GroupSendSmsActivity.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(GroupSendSmsActivity.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 s: sendList) {
            if (s.equals(phone)) {
                return true;
            }
        }
        return false;
    }


}

这里写图片描述


http://www.niftyadmin.cn/n/4308596.html

相关文章

JavaScipt 源码解析 异步

我们常见的异步操作&#xff1a; 定时器setTimeout postmessage WebWorkor CSS3 动画 XMLHttpRequest HTML5的本地数据 等等… JavaScript要求在与服务器进行交互时要用异步通信&#xff0c;如同AJAX一样。因为是异步模型&#xff0c;所以在调用Transaction游览器提供的本地数据…

.NET平台开源项目速览(10)FluentValidation验证组件深入使用(二)

在上一篇文章&#xff1a;.NET平台开源项目速览(6)FluentValidation验证组件介绍与入门(一) 中&#xff0c;给大家初步介绍了一下FluentValidation验证组件的使用情况。文章从构建间的验证器开始&#xff0c;到最后的结果&#xff0c;以及复杂验证等都做了比较深入的讲解和使用…

EhCache RMI 分布式缓存/缓存集群

EhCache 系统简介 EhCache 是一个纯 Java 的进程内缓存框架&#xff0c;具有快速、精干等特点。 EhCache 的主要特性有&#xff1a; 快速、精干简单&#xff1b;多种缓存策略&#xff1b;缓存数据有两级&#xff1a;内存和磁盘&#xff0c;因此无需担心容量问题&#xff1b;缓存…

谈谈博客园和写博客,以及通过博客遇到的那些人

不知不觉&#xff0c;博客园园龄已经5年11个月了&#xff0c;还曾依稀的记得&#xff0c;那是研究生毕业设计搞完了&#xff0c;有没有什么事情可以做&#xff0c;只能每天背个屌丝的书包去学院机房&#xff0c;狂赚CSDN积分&#xff0c;曾经高峰期的时候CSDN积分达到16000分&a…

SmartHome

ZigBee: CEL MeshConnect ZICM357SP2-1 Z-Wave: ZM4101AA-CME3

Python函数中的参数(二)

当使用混合特定的参数匹配模型时&#xff0c;Python将会遵循以下有关顺序的法则&#xff1a; 1、在函数调用中&#xff0c;参数必须以这样的顺序出现&#xff1a;任何位置参数&#xff08;Value&#xff09;、任何关键字参数&#xff08;name Value&#xff09;和*sequence形式…

.NET平台开源项目速览(11)KwCombinatorics排列组合使用案例(1)

今年上半年&#xff0c;我在KwCombinatorics系列文章中&#xff0c;重点介绍了KwCombinatorics组件的使用情况&#xff0c;其实这个组件我5年前就开始用了&#xff0c;非常方便&#xff0c;麻雀虽小五脏俱全。所以一直非常喜欢&#xff0c;才写了几篇文章推荐给大家。最近在计算…

nfs 搭建

nfs 搭建yum install nfs-utils rpcbind 修改配置文件 /etc/exports格式 每个共享的文件系统需要独立一行目录客户端主机列表需要使用空格隔开配置文件中支持通配符 语法结构 共享路径 客户端主机&#xff08;选项&#xff09; 默认设置 ro,sync,wdelay,root_squash 对应表ro 只…