Android 短信群发功能的实现

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

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

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

再使用 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);
                }
                // 提示短信群发完成

以下是源代码

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

但是存在一个风险,如果发送的联系人过多,容易出现严重的网络延迟。

作者:宋磊

https://blog.csdn.net/weixin_40599987/article/details/80697682

疯狂Android开发

猜你喜欢

转载自blog.csdn.net/fjnu_se/article/details/80715421