从您的主函数中调用此方法:
private void sendSMS() {
SMSSend smsSend = new SMSSend();
smsSend.execCommand();
}
那么下面这两个类将负责发送短信。
public class SMSSend {
private final String CLASS_NAME = this.getClass().getSimpleName();
private final static String SEND_SMS_FAILURE = "Send SMS command executed and status is failure";
private String phoneNumber;
private String messageText;
public SMSSend() {
super();
}
public void execCommand() {
try {
new Thread() {
public void run() {
sendSMS();
}
}.start();
} catch (Exception e) {
e.printStackTrace();
}
}
private void sendSMS() {
try {
SMSSendHelper smsHelper = new SMSSendHelper();
phoneNumber = "0123456789";
messageText = "Message Text smaple";
if (null == phoneNumber || phoneNumber.length() < 2) {
} else {
smsHelper.sendSMS(phoneNumber, messageText);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
}
}
}
public class SMSSendHelper {
private final String CLASS_NAME = this.getClass().getSimpleName();
private String SENT = "SMS_SENT";
private String DELIVERED = "SMS_DELIVERED";
private PendingIntent sentPI;
private BroadcastReceiver sentReceiver;
private BroadcastReceiver deliveredReceiver;
private PendingIntent deliveredPI;
private static final int SMS_LENGHT = 160;
private static String status = "";
public SMSSendHelper() {
sentPI = PendingIntent.getBroadcast(ConfigClass.getAppContext(), 0,
new Intent(SENT), 0);
deliveredPI = PendingIntent.getBroadcast(ConfigClass.getAppContext(),
0, new Intent(DELIVERED), 0);
sentReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context arg0, Intent arg1) {
switch (getResultCode()) {
case Activity.RESULT_OK:
try {
synchronized (lock) {
lock.notifyAll();
}
} catch (Exception e) {
e.printStackTrace();
}
setSendSMSSataus("SMS Sent");
break;
case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
try {
synchronized (lock) {
lock.notifyAll();
}
} catch (Exception e) {
e.printStackTrace();
}
setSendSMSSataus("Generic failure");
break;
case SmsManager.RESULT_ERROR_NO_SERVICE:
try {
synchronized (lock) {
lock.notifyAll();
}
} catch (Exception e) {
}
setSendSMSSataus("No service");
break;
case SmsManager.RESULT_ERROR_NULL_PDU:
try {
synchronized (lock) {
lock.notifyAll();
}
} catch (Exception e) {
e.printStackTrace();
}
setSendSMSSataus("Null PDU");
break;
case SmsManager.RESULT_ERROR_RADIO_OFF:
try {
synchronized (lock) {
lock.notifyAll();
}
} catch (Exception e) {
e.printStackTrace();
}
setSendSMSSataus("Radio off");
break;
default:
try {
synchronized (lock) {
lock.notifyAll();
}
} catch (Exception e) {
e.printStackTrace();
}
break;
}
ConfigClass.getAppContext().unregisterReceiver(sentReceiver);
}
};
ConfigClass.getAppContext().registerReceiver(sentReceiver,
new IntentFilter(SENT));
deliveredReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context arg0, Intent arg1) {
switch (getResultCode()) {
case Activity.RESULT_OK:
break;
case Activity.RESULT_CANCELED:
break;
}
ConfigClass.getAppContext().unregisterReceiver(
deliveredReceiver);
}
};
ConfigClass.getAppContext().registerReceiver(deliveredReceiver,
new IntentFilter(DELIVERED));
}
public static void setSendSMSSataus(String currentSataus) {
status = currentSataus;
}
public String getSendSMSSataus() {
int i = sentReceiver.getResultCode();
return status;
}
public void sendSMS(String phoneNumber, String message) {
if (message == null) {
message = " ";
}
SmsManager sms = SmsManager.getDefault();
if (message.length() > SMS_LENGHT) {
sendLongSMS(sms, message, phoneNumber);
} else {
Log.d("E++++D",phoneNumber+"======>>>>>SOS");
sms.sendTextMessage(phoneNumber, null, message, sentPI, deliveredPI);
}
try {
synchronized (lock) {
lock.wait();
}
} catch (Exception e) {
e.printStackTrace();
}
}
private void sendLongSMS(SmsManager sms, String message, String phoneNumber) {
ArrayList<String> parts = sms.divideMessage(message);
int numParts = parts.size();
ArrayList<PendingIntent> sentIntents = new ArrayList<PendingIntent>();
ArrayList<PendingIntent> deliveryIntents = new ArrayList<PendingIntent>();
for (int i = 0; i < numParts; i++) {
sentIntents.add(sentPI);
deliveryIntents.add(deliveredPI);
}
sms.sendMultipartTextMessage(phoneNumber, null, parts, sentIntents,
deliveryIntents);
}
private Object lock = new Object();
}