当我们要群发短信,而且需要替换里面的一些信息的时候(比如发送祝福短信,需要在短信里面加入对方姓名时),
做一个简单的群发短信的功能,并用通讯录里面的联系人姓名替换短信里面的#name#字符串,然后发送短信
代码如下:
main.java
package org.androidMarket.app;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;import android.app.Activity;import android.app.PendingIntent;import android.content.Intent;import android.database.Cursor;import android.os.Bundle;import android.provider.ContactsContract;import android.telephony.SmsManager;import android.view.Display;import android.view.View;import android.widget.Button;import android.widget.EditText;import android.widget.Toast;public class Main extends Activity { /** Called when the activity is first created. */ private EditText txtNo; private EditText txtContent; private Button btnSend; private Button btnCancel; private final static String PLACEHOLDER_NAME = "#name#"; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main);// txtNo = (EditText) findViewById(R.id.txNo); txtContent = (EditText) findViewById(R.id.txContent); btnSend = (Button) findViewById(R.id.btnSend);// btnCancel = (Button) findViewById(R.id.btnCancel); Display d = getWindowManager().getDefaultDisplay(); txtContent.setHeight((int) (d.getHeight() * 0.5)); btnSend.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) {// String strNo = txtNo.getText().toString(); String strContent = txtContent.getText().toString(); SmsManager smsManager = SmsManager.getDefault(); PendingIntent sentIntent = PendingIntent.getBroadcast(Main.this, 0, new Intent(), 0); // 如果字数超过70,需拆分成多条短信发送// if (strContent.length() > 70) {// List msgs = smsManager.divideMessage(strContent);// for (String msg : msgs) {// smsManager.sendTextMessage(strNo, null, msg, sentIntent, null);// }// } else {// smsManager.sendTextMessage(strNo, null, strContent, sentIntent, null);// } List
layout/main.xml
values/string.xml
SmartSMS 清空 群发
AndroidManifest.xml
已经在手机上实验成功了。