【转】数据短信
Objective
Use SMS to send and receive raw data within your App.
Issue
When you want send information with the function SMSManager#sendTextMessage, 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
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
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.
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