这是收短信的功能.
读短信的功能.
收到短信的广播处理.
public class SMSBroadcastReceiver extends BroadcastReceiver {
public void onReceive(Context context, Intent intent) {
SmsMessage msg = null;
Bundle bundle = intent.getExtras();
if (bundle != null) {
Object[] pdusObj = (Object[]) bundle.get(“pdus”);
for (Object p : pdusObj) {
msg= SmsMessage.createFromPdu((byte[]) p);
String msgTxt =msg.getMessageBody();//得到消息的内容
Date date = new Date(msg.getTimestampMillis());//时间
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String receiveTime = format.format(date);
String senderNumber = msg.getOriginatingAddress();
if (msgTxt.equals("Testing!")) {
Toast.makeText(context, "success!", Toast.LENGTH_LONG)
.show();
System.out.println("success!");
return;
} else {
Toast.makeText(context, msgTxt, Toast.LENGTH_LONG).show();
System.out.println("发送人:"+senderNumber+" 短信内容:"+msgTxt+"接受时间:"+receiveTime);
return;
}
}
return;
}
}
发送短信.
** public void send(View view ) {
String strNo=num.getText().toString();
String strContent=content.getText().toString();
SmsManager smsManager = SmsManager.getDefault();
//如果字数超过5,需拆分成多条短信发送
if (strContent.length() > 5) {
ArrayList<String> msgs = smsManager.divideMessage(strContent);
for (String msg : msgs) {
smsManager.sendTextMessage(strNo, null, msg, null, null);
}
} else {
smsManager.sendTextMessage(strNo, null, strContent, null, null);
}
num.setText("");
content.setText("");
Toast.makeText(SendSMS.this, "短信发送完成", Toast.LENGTH_LONG).show();
}
**