---
title: "【转】数据短信"
author: "gdfdfg"
date: "2016-01-22T21:49:49Z"
tags: []
categories: ["研发管理"]
slug: "untitled"
draft: false
lastmod: "2016-01-22T21:49:49Z"
id: 2273445
url: "https://gdfdfg-tech.iteye.com/blog/2273445"
---

#### **Objective**

Use SMS to send and receive raw data within your App.

#### **Issue**

When you want send information with the function [SMSManager#sendTextMessage](http://developer.android.com/reference/android/telephony/SmsManager.html#sendTextMessage%28java.lang.String,%20java.lang.String,%20java.lang.String,%20android.app.PendingIntent,%20android.app.PendingIntent%29), the sent SMS is stored in the sent messages list, then it triggers a notification when the recipient received this message and the system store it in the inbox in plain text.  
With this method, there is not security and it's annoying for the user, because he has to clean up its inbox.

#### **Solution**

##### **Steps**

1\. Send your message on a specific port with the [SMSManager#sendDataMessage]() function  
2\. Register a BroadcastReceiver on the same port  
3\. Add the required permissions in your manifest

##### **Details**

1. Send your message on a specific port with the [SMSManager#sendDataMessage]() function

[?](http://blog.fordemobile.com/2012/09/use-sms-to-send-and-receive-raw-data.html)

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

`public` `void` `sendMessage(final` `byte[] message) {`

`//define the phone number`

`final` `String port = "90";`

`final` `String phoneNumber = "5146792345"`

`//intent broadcasted when the SMS is sent`

`final` `PendingIntent sendIntent = PendingIntent.getBroadcast(`

`this.context, 0, new` `Intent(0), 0);`

`//intent broadcasted when the SMS is received`

`final` `PendingIntent delivery = PendingIntent.getBroadcast(`

`this.context, 0, new` `Intent(0), 0);`

`//send data`

`final` `SmsManager smsManager = SmsManager.getDefault();`

`smsManager.sendDataMessage(phoneNumber, "", port,`

`message, sendIntent, delivery);`

`}`

2\. Register a BroadcastReceiver on the same port

[?](http://blog.fordemobile.com/2012/09/use-sms-to-send-and-receive-raw-data.html)

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

`BroadcastReceiver receivedBroadcastReceiver = new` `BroadcastReceiver() {`

`@Override`

`public` `void` `onReceive(Context context, Intent intent) {`

`final` `Bundle bundle = intent.getExtras();`

`SmsMessage[] msgs = null;`

 

`if` `(null` `!= bundle) {`

`final` `Object[] pdus = (Object[]) bundle.get("pdus");`

`msgs = new` `SmsMessage[pdus.length];`

`byte[] data = null;`

`//read data`

`for` `(int` `i = 0; i < msgs.length; i++) {`

`msgs[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);`

`data = msgs[i].getUserData();`

`}`

`if` `(data != null) {`

`//use data`

`}`

`}`

`}`

`};`

`//register the receiver`

`final` `String port = "90";`

`final` `IntentFilter intentFilter = new` `IntentFilter(`

`"android.intent.action.DATA_SMS_RECEIVED");`

`intentFilter.addDataScheme("sms");`

`intentFilter.addDataAuthority("*", port);`

`this.context.registerReceiver(this.receivedBroadcastReceiver,`

`intentFilter);`

3\. Add the required permissions in your manifest.

[?](http://blog.fordemobile.com/2012/09/use-sms-to-send-and-receive-raw-data.html)

1

2

`<uses` `-permission="-permission"` `android:name="android.permission.SEND_SMS"` `/>`

`<uses` `-permission="-permission"` `android:name="android.permission.RECEIVE_SMS"` `/>`

 

转自：http://blog.fordemobile.com/2012/09/use-sms-to-send-and-receive-raw-data.html