android 几种发送短信的方法
android中发送短信很简单,
首先要在Mainfest.xml中加入所需要的权限:
1
2
3
|
< 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:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
|
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)); SmsManager smsm = SmsManager.getDefault(); smsm.sendTextMessage(number, null , message, sentPI, deliveredPI); } |
在模拟器上是看不到Receiver中的log信息的,据网上说在手机上是可以实现的,只可惜手中无机,也只能纸上谈兵了。
方法3:
上面都是发送文本文件,如果想发送一些非文本,比如加密数据,可以用以下方法:
1
2
3
4
5
6
|
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
2
3
4
5
|
private void send(String message){ Intent sendIntent = new Intent(Intent.ACTION_VIEW); sendIntent.putExtra( "sms_body" , message); sendIntent.setType( "vnd.android-dir/mms-sms" ); } |
这个方法自动设置接收方的号码
1
2
3
4
5
6
|
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为实现类
1
2
3
4
5
6
7
|
< receiver android:name = ".SmsReceiver" > < intent-filter > < action android:name = "android.provider.Telephony.SMS_RECEIVED" /> </ intent-filter > </ receiver > |
如果是方法3,使用sendDataMessage时需要加入
1
2
3
4
5
6
7
8
9
10
|
< 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中的端口值不一致时,也没有问题,太神奇了
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
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()方法。