【转】数据短信

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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值