android中发送短信很简单,
首先要在Mainfest.xml中加入所需要的权限:
为了后面测试方便,干脆把SMS的所有权限加上。
方法1:
发送短信 需要真机来测试
public class testSms extends Activity {
…
private void send1(String phone, String message){
PendingIntent pi = PendingIntent.getActivity(this, 0, new Intent(this, testSms.class), 0); //意图
/*
PendingIntent.getBroadcast(context, requestCode, intent, flags);
PendingIntent.getActivity(context, requestCode, intent, flags);
PendingIntent.getService(context, requestCode, intent, flags);
*/
SmsManager sms = SmsManager.getDefault(); // 短信管理
sms.sendTextMessage(phone, null, message, pi, null);
// 发送短信 phone 电话号码 ,message 短信 ,pi 意图
}
}
方法2:
如果想知道短信发送后的状态,需要两个Receiver来实现
private void send2(String number, String message){
String SENT = “sms_sent”;
String DELIVERED = “sms_delivered”;
PendingIntent sentPI = PendingIntent.getActivity(this, 0, new Intent(SENT), 0);
PendingIntent deliveredPI = PendingIntent.getActivity(this, 0, new Intent(DELIVERED), 0);
registerReceiver(new BroadcastReceiver(){ // 实现一个匿名类广播
@Override
public void onReceive(Context context, Intent intent) {
switch(getResultCode())
{
case Activity.RESULT_OK:
Log.i("====>", "Activity.RESULT_OK");
break;
case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
Log.i("====>", "RESULT_ERROR_GENERIC_FAILURE");
break;
case SmsManager.RESULT_ERROR_NO_SERVICE:
Log.i("====>", "RESULT_ERROR_NO_SERVICE");
break;
case SmsManager.RESULT_ERROR_NULL_PDU:
Log.i("====>", "RESULT_ERROR_NULL_PDU");
break;
case SmsManager.RESULT_ERROR_RADIO_OFF:
Log.i("====>", "RESULT_ERROR_RADIO_OFF");
break;
}
}
}, new IntentFilter(SENT));
registerReceiver(new BroadcastReceiver(){
@Override
public void onReceive(Context context, Intent intent){
switch(getResultCode())
{
case Activity.RESULT_OK:
Log.i("====>", "RESULT_OK");
break;
case Activity.RESULT_CANCELED:
Log.i("=====>", "RESULT_CANCELED");
break;
}
}
}, new IntentFilter(DELIVERED)); // new IntentFilter(deliver); 过滤器
SmsManager smsm = SmsManager.getDefault();
smsm.sendTextMessage(number, null, message, sentPI, deliveredPI);
}
方法3:
上面都是发送文本文件,如果想发送一些非文本,比如加密数据,可以用以下方法:
private void send2(String number, String message){
SmsManager smsm = SmsManager.getDefault();
short port = 1000;
PendingIntent pi = PendingIntent.getBroadcast(test.this, 0, new Intent(), 0);
smsm.sendDataMessage(number, null, port, message.getBytes(), pi, null);
}
方法4:
调用系统的短信界面,这个方法需要用户自己输入接收方的电话号码
// 会跳转到系统的发送短信界面
private void send(String message){
Intent sendIntent = new Intent(Intent.ACTION_VIEW);
sendIntent.putExtra(“sms_body”, message);
sendIntent.setType(“vnd.android-dir/mms-sms”);
}
这个方法自动设置接收方的号码
private void send1(String number, String message){
Uri uri = Uri.parse(“smsto:” + number);
Intent sendIntent = new Intent(Intent.ACTION_VIEW, uri);
sendIntent.putExtra(“sms_body”, message);
startActivity(sendIntent);
}
短信的接受,需要实现BroadcastReceiver类,监听系统消息
首先在Mainfest.xml中加入申明,SmsReceiver为实现类
如果是方法3,使用sendDataMessage时需要加入
将其中的data数据注释掉,模拟器上也可以接收到短信; port跟sendDataMessage中的端口值不一致时,也没有问题,太神奇了
public class SmsReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Bundle bundle = intent.getExtras();
SmsMessage[] msgs = null;
String phone;
String message;
if(bundle != null){
Object[] pdus = (Object[])bundle.get("pdus"); // 通过pdu 来接收信息
msgs = new SmsMessage[pdus.length];
for(int i = 0; i < msgs.length; i++){
msgs[i] = SmsMessage.createFromPdu((byte[])pdus[i]);
phone = msgs[i].getOriginatingAddress(); // 电话号码
message = msgs[i].getMessageBody();// 短信内容
/*
同一个号码的短信放在一起
phone = msgs[0].getOriginatingAddress(); // 电话号码
String fullmessage = new String();
for(SmsMessage x:smsmessage){
fullmessage+=x.getMessageBody();
*/
}
}
}
}
}
如果是sendDataMessage发送:
public class SmsReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Bundle bundle = intent.getExtras();
SmsMessage[] msgs = null;
String phone;
String message;
if(bundle != null){
Object[] pdus = (Object[])bundle.get("pdus");
msgs = new SmsMessage[pdus.length];
for(int i = 0; i < msgs.length; i++){
msgs[i] = SmsMessage.createFromPdu((byte[])pdus[i]);
phone = msgs[i].getOriginatingAddress();
byte data[] = SmsMessage.createFromPdu((byte[])pdus[i]).getUserData();
message = new String(data);
}
}
}
}
不一样的地方是message的获取时使用getUserData()方法。