packagecom.javen.utils;importjava.util.ArrayList;importandroid.app.Activity;importandroid.app.PendingIntent;importandroid.content.BroadcastReceiver;importandroid.content.Context;importandroid.content.Intent;importandroid.content.IntentFilter;importandroid.telephony.SmsManager;importandroid.widget.Toast;/***@authorJaven
**/
public classSendMessageUtil {/**发送与接收的广播 **/
private static String SENT_SMS_ACTION = "SENT_SMS_ACTION";private static String DELIVERED_SMS_ACTION = "DELIVERED_SMS_ACTION";/*** 实现发送短信
*@paramcontext
*@paramtext 短信的内容
*@paramphoneNumber 手机号码*/
public static voidsendMessage(Context context, String text,
String phoneNumber) {
context.registerReceiver(sendMessage,newIntentFilter(SENT_SMS_ACTION));
context.registerReceiver(receiver,newIntentFilter(
DELIVERED_SMS_ACTION));//create the sentIntent parameter
Intent sentIntent = newIntent(SENT_SMS_ACTION);
PendingIntent sentPI= PendingIntent.getBroadcast(context, 0, sentIntent,0);//create the deilverIntent parameter
Intent deliverIntent = newIntent(DELIVERED_SMS_ACTION);
PendingIntent deliverPI= PendingIntent.getBroadcast(context, 0,deliverIntent, 0);
SmsManager smsManager=SmsManager.getDefault();//如果字数超过5,需拆分成多条短信发送
if (text.length() > 70) {
ArrayList msgs =smsManager.divideMessage(text);for(String msg : msgs) {
smsManager.sendTextMessage(phoneNumber,null, msg, sentPI, deliverPI);
}
}else{
smsManager.sendTextMessage(phoneNumber,null, text, sentPI, deliverPI);
}
}private static BroadcastReceiver sendMessage = newBroadcastReceiver() {
@Overridepublic voidonReceive(Context context, Intent intent) {//判断短信是否发送成功
switch(getResultCode()) {caseActivity.RESULT_OK:
Toast.makeText(context,"短信发送成功", Toast.LENGTH_SHORT).show();break;default:
Toast.makeText(context,"发送失败", Toast.LENGTH_LONG).show();break;
}
}
};private static BroadcastReceiver receiver = newBroadcastReceiver() {
@Overridepublic voidonReceive(Context context, Intent intent) {//表示对方成功收到短信
Toast.makeText(context, "对方接收成功", Toast.LENGTH_LONG).show();
}
};
}