android 几种发送短信的方法【转】
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来实现
1 private void send2(String number, String message){ 2 String SENT = "sms_sent"; 3 String DELIVERED = "sms_delivered"; 4 5 PendingIntent sentPI = PendingIntent.getActivity(this, 0, new Intent(SENT), 0); 6 PendingIntent deliveredPI = PendingIntent.getActivity(this, 0, new Intent(DELIVERED), 0); 7 8 registerReceiver(new BroadcastReceiver(){ 9 10 @Override 11 public void onReceive(Context context, Intent intent) { 12 switch(getResultCode()) 13 { 14 case Activity.RESULT_OK: 15 Log.i("====>", "Activity.RESULT_OK"); 16 break; 17 case SmsManager.RESULT_ERROR_GENERIC_FAILURE: 18 Log.i("====>", "RESULT_ERROR_GENERIC_FAILURE"); 19 break; 20 case SmsManager.RESULT_ERROR_NO_SERVICE: 21 Log.i("====>", "RESULT_ERROR_NO_SERVICE"); 22 break; 23 case SmsManager.RESULT_ERROR_NULL_PDU: 24 Log.i("====>", "RESULT_ERROR_NULL_PDU"); 25 break; 26 case SmsManager.RESULT_ERROR_RADIO_OFF: 27 Log.i("====>", "RESULT_ERROR_RADIO_OFF"); 28 break; 29 } 30 } 31 }, new IntentFilter(SENT)); 32 33 registerReceiver(new BroadcastReceiver(){ 34 @Override 35 public void onReceive(Context context, Intent intent){ 36 switch(getResultCode()) 37 { 38 case Activity.RESULT_OK: 39 Log.i("====>", "RESULT_OK"); 40 break; 41 case Activity.RESULT_CANCELED: 42 Log.i("=====>", "RESULT_CANCELED"); 43 break; 44 } 45 } 46 }, new IntentFilter(DELIVERED)); 47 48 SmsManager smsm = SmsManager.getDefault(); 49 smsm.sendTextMessage(number, null, message, sentPI, deliveredPI); 50 }
在模拟器上是看不到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:
调用系统的短信界面,这个方法需要用户自己输入接收方的电话号码
1 private void send(String message){ 2 Intent sendIntent = new Intent(Intent.ACTION_VIEW); 3 sendIntent.putExtra("sms_body", message); 4 sendIntent.setType("vnd.android-dir/mms-sms"); 5 }
这个方法自动设置接收方的号码
1 private void send1(String number, String message){ 2 Uri uri = Uri.parse("smsto:" + number); 3 Intent sendIntent = new Intent(Intent.ACTION_VIEW, uri); 4 sendIntent.putExtra("sms_body", message); 5 startActivity(sendIntent); 6 }
短信的接受,需要实现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发送:
1 public class SmsReceiver extends BroadcastReceiver { 2 3 @Override 4 public void onReceive(Context context, Intent intent) { 5 Bundle bundle = intent.getExtras(); 6 SmsMessage[] msgs = null; 7 String phone; 8 String message; 9 10 if(bundle != null){ 11 Object[] pdus = (Object[])bundle.get("pdus"); 12 msgs = new SmsMessage[pdus.length]; 13 for(int i = 0; i < msgs.length; i++){ 14 msgs[i] = SmsMessage.createFromPdu((byte[])pdus[i]); 15 phone = msgs[i].getOriginatingAddress(); 16 byte data[] = SmsMessage.createFromPdu((byte[])pdus[i]).getUserData(); 17 message = new String(data); 18 } 19 } 20 } 21 }
不一样的地方是message的获取时使用getUserData()方法。