android中发送短信
首先要在Mainfest.xml中加入所需要的权限:
<uses-permission android:name="android.permission.SEND_SMS"></uses-permission>
<uses-permission android:name="android.permission.READ_SMS"></uses-permission>
<uses-permission android:name="android.permission.RECEIVE_SMS"></uses-permission>
为了后面测试方便,干脆把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);
SmsManager sms = SmsManager.getDefault();
sms.sendTextMessage(phone, null, message, pi, null);
}
}
方法2:
如果想知道短信发送后的状态,需要两个Receiver来实现
//---sends an SMS message to another device---
private void sendSMS(String phoneNumber, String message)
{
String SENT = "SMS_SENT";
String DELIVERED = "SMS_DELIVERED";
PendingIntent sentPI = PendingIntent.getBroadcast(this, 0,
new Intent(SENT), 0);
PendingIntent deliveredPI = PendingIntent.getBroadcast(this, 0,
new Intent(DELIVERED), 0);
//---when the SMS has been sent---
registerReceiver(new BroadcastReceiver(){
@Override
public void onReceive(Context arg0, Intent arg1) {
switch (getResultCode())
{
case Activity.RESULT_OK:
Toast.makeText(getBaseContext(), "SMS sent",
Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
Toast.makeText(getBaseContext(), "Generic failure",
Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_NO_SERVICE:
Toast.makeText(getBaseContext(), "No service",
Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_NULL_PDU:
Toast.makeText(getBaseContext(), "Null PDU",
Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_RADIO_OFF:
Toast.makeText(getBaseContext(), "Radio off",
Toast.LENGTH_SHORT).show();
break;
}
}
}, new IntentFilter(SENT));
//---when the SMS has been delivered---
registerReceiver(new BroadcastReceiver(){
@Override
public void onReceive(Context arg0, Intent arg1) {
switch (getResultCode())
{
case Activity.RESULT_OK:
Toast.makeText(getBaseContext(), "SMS delivered",
Toast.LENGTH_SHORT).show();
break;
case Activity.RESULT_CANCELED:
Toast.makeText(getBaseContext(), "SMS not delivered",
Toast.LENGTH_SHORT).show();
break;
}
}
}, new IntentFilter(DELIVERED));
SmsManager sms = SmsManager.getDefault();
sms.sendTextMessage(phoneNumber, null, message, sentPI, deliveredPI);
}
在模拟器上是看不到Receiver中的log信息的,据网上说在手机上是可以实现的,只可惜手中无机,也只能纸上谈兵了。
方法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为实现类
<receiver android:name=".SmsReceiver">
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
如果是方法3,使用sendDataMessage时需要加入
<receiver android:name=".SmsReceiver">
<intent-filter>
<action android:name="android.intent.action.DATA_SMS_RECEIVED" />
<data android:scheme="sms" />
<data android:host="localhost" />
<data android:port="1000" />
</intent-filter>
</receiver>
将其中的dat数据注释掉,模拟器上也可以接收到短信; 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");
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();
}
}
}
}
如果是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()方法。