本文主要是介绍Android SmsManager 短信群发,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
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>();@Overrideprotected 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() {@Overridepublic 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() {@Overridepublic void onClick(View v) {// 查询联系人的电话号码final Cursor cursor = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null);BaseAdapter adapter = new BaseAdapter() {@Overridepublic int getCount() {return cursor.getCount();}@Overridepublic Object getItem(int position) {return position;}@Overridepublic long getItemId(int position) {return position;}@Overridepublic 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布局文件对应的ViewView 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(){@Overridepublic 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;}}
这篇关于Android SmsManager 短信群发的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!