忙了这么长时间,毕业论文终于完成了,呵呵,现在就准备答辩了。然后,在平静中度过我最后的大学生活 哈哈,,,
这次介绍下有关短信的发送,在这里介绍的是发动普通的文本短信,还有一种是数据短信。
发送短信必须具有相应的权限:
1. 发送普通文本短信
SmsManager smsManager = SmsManager.getDefault(); //短信管理器
PendingIntent sentIntent = PendingIntent.getBroadcast(this, 0,new Intent(), 0);
// 电话号码验证,简单的数据验证
for (int count = 0; count < length; count++) {
Pattern pattern = Pattern.compile("^[0-9]+$");
Matcher matcher = pattern.matcher(phoneNumber);
if (!matcher.matches()) {
//弹出警告对话框
showDialog(DIALOG_NUMBER_ERROR);
return;
}
}
for (int i = 0; i < length; i++) {
if (smsContent.length() > 70) {
// 短信内容的字数大于70
List msgs = smsManager.divideMessage(smsContent);
for (String msg : msgs) {
smsManager.sendTextMessage(contactsNumber[i], null,msg, sentIntent, null);
}
} else {
smsManager.sendTextMessage(contactsNumber[i], null,smsContent, sentIntent, null);
}
// 保存发送短信的内容
ContentValues values = new ContentValues();
values.put("address", contactsNumber[i]);
values.put("body", smsContent);
getContentResolver().insert(Uri.parse("content://sms/sent"), values);
}
Toast.makeText(this, R.string.sendSmsSuccess,Toast.LENGTH_SHORT).show();
SmsManager smsManager = SmsManager.getDefault();
Intent sentIntent = new Intent("com.myself.action.SMS_SEND_RESULT");
PendingIntent dummySentEvent = PendingIntent.getBroadcast(this, 0, sentIntent, 0);
Intent deliveryIntent = new Intent("com.myself.action.SMS_DELIVERY_RESULT");
PendingIntent dummyDeliveryEvent = PendingIntent.getBroadcast(this, 0, deliveryIntent, 0);
int port=51234;
byte[] sms_data = myString.getBytes();
try {
smsManager.sendDataMessage(telNumStr, null, port, sms_data,dummySentEvent, dummyDeliveryEvent);
} catch (Exception e) {
Log.e("SmsSending", "SendException", e);
}
关于如何接收短信,也就是采用拦截广播方式BroadcastReceiver,二者在onReceive方法中的实现逻辑相似,
只是把getDisplayMessageBody()改成getUserData().
在AndroidManifest.xml文件中注册短信广播接收器时,有些差别。
接收文本短信的广播接收器的注册方式:
<<>uses-permission android:name="android.permission.RECEIVE_SMS" /> <<>receiver android:name="MessageDemo"> <<>intent-filter> <<>action android:name="android.provider.Telephony.SMS_RECEIVED" /> intent-filter> receiver>
<style type="text/css"> <!-- .csharpcode, .csharpcode pre {font-size:small; color:black; font-family:consolas,"Courier New",courier,monospace; background-color:#ffffff} .csharpcode pre {margin:0em} .csharpcode .rem {color:#008000} .csharpcode .kwrd {color:#0000ff} .csharpcode .str {color:#006080} .csharpcode .op {color:#0000c0} .csharpcode .preproc {color:#cc6633} .csharpcode .asp {background-color:#ffff00} .csharpcode .html {color:#800000} .csharpcode .attr {color:#ff0000} .csharpcode .alt {background-color:#f4f4f4; width:100%; margin:0em} .csharpcode .lnum {color:#606060} --> </style>
接收数据短信的广播接收器的注册方式:
<<>uses-permission android:name="android.permission.RECEIVE_SMS" />
<<>receiver android:name="MessageDemo">
<<>intent-filter>
<<>action android:name="android.intent.action.DATA_SMS_RECEIVED" />
<<>data android:scheme="sms" />
<<>data android:host="localhost" />
<<>data android:port="51234" />
intent-filter>
receiver>