android 几种发送短信的方法

android中发送短信很简单,

首先要在Mainfest.xml中加入所需要的权限:

1 <uses-permission android:name="android.permission.SEND_SMS"></uses-permission>
2 <uses-permission android:name="android.permission.READ_SMS"></uses-permission>
3 <uses-permission android:name="android.permission.RECEIVE_SMS"></uses-permission>

为了后面测试方便,干脆把SMS的所有权限加上。

方法1:

01 public class testSms extends Activity {
02  
03 ...
04  
05     private void send1(String phone, String message){
06  
07         PendingIntent pi = PendingIntent.getActivity(this0new Intent(this, testSms.class), 0);
08  
09         SmsManager sms = SmsManager.getDefault();
10  
11         sms.sendTextMessage(phone, null, message, pi, null);
12  
13     }
14  
15 }

方法2:

如果想知道短信发送后的状态,需要两个Receiver来实现

01 private void send2(String number, String message){
02     String SENT = "sms_sent";
03     String DELIVERED = "sms_delivered";
04     
05     PendingIntent sentPI = PendingIntent.getActivity(this0new Intent(SENT),0);
06     PendingIntent deliveredPI = PendingIntent.getActivity(this0newIntent(DELIVERED), 0);
07     
08     registerReceiver(new BroadcastReceiver(){
09  
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:

上面都是发送文本文件,如果想发送一些非文本,比如加密数据,可以用以下方法:

1 private void send2(String number, String message){
2     SmsManager smsm = SmsManager.getDefault();
3     short port = 1000;
4     PendingIntent pi = PendingIntent.getBroadcast(test.this0new Intent(), 0);
5     smsm.sendDataMessage(number, null, port, message.getBytes(), pi, null);
6 }

方法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为实现类

1 <receiver android:name=".SmsReceiver">
2  
3 <intent-filter>
4     <action android:name="android.provider.Telephony.SMS_RECEIVED" />
5 </intent-filter>
6  
7 </receiver>

 

如果是方法3,使用sendDataMessage时需要加入

01 <receiver android:name=".SmsReceiver">
02  
03 <intent-filter>
04     <action android:name="android.intent.action.DATA_SMS_RECEIVED" />
05     <data android:scheme="sms" />
06     <data android:host="localhost" />
07     <data android:port="1000" />
08 </intent-filter>
09  
10 </receiver>

将其中的dat数据注释掉,模拟器上也可以接收到短信; port跟sendDataMessage中的端口值不一致时,也没有问题,太神奇了

01 public class SmsReceiver extends BroadcastReceiver {
02  
03     @Override
04     public void onReceive(Context context, Intent intent) {
05         Bundle bundle = intent.getExtras();
06         SmsMessage[] msgs = null;
07         String phone;
08         String message;
09                 
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                 message = msgs[i].getMessageBody();
17             }
18         }
19     }
20 }

如果是sendDataMessage发送:

01 public class SmsReceiver extends BroadcastReceiver {
02  
03     @Override
04     public void onReceive(Context context, Intent intent) {
05         Bundle bundle = intent.getExtras();
06         SmsMessage[] msgs = null;
07              String phone;
08              String message;
09                 
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()方法。

原文转自:http://blog.csdn.net/rangq1/article/details/5793953

你可能感兴趣的:(android 几种发送短信的方法)