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