技术库 > Java

Android收发短信实现

技术库:tec.5lulu.com

调用短信编辑界面  

Intent smsIntent = new Intent(Intent.ACTION_SENDTO,  
                   Uri.parse("sms:5556"));  
           smsIntent.putExtra("sms_body", "Press send to send me");  
           startActivity(smsIntent);  

from:tec.5lulu.com


发送短信   

String SENT_SMS_ACTION = "SENT_SMS_ACTION";  
       String DELIVERED_SMS_ACTION = "DELIVERED_SMS_ACTION";   
       SmsManager smsManager = SmsManager.getDefault();  
       String sendTo = "5556";  
       String myMessage = "Android supports programmatic SMS messaging!";  
         
       // Create the sentIntent parameter  
       Intent sentIntent = new Intent(SENT_SMS_ACTION);  
       PendingIntent sentPI = PendingIntent.getBroadcast(getApplicationContext(),  
               0,  sentIntent, 0);  
         
       // Create the deliveryIntent parameter  
       Intent deliveryIntent = new Intent(DELIVERED_SMS_ACTION);  
       PendingIntent deliverPI = PendingIntent.getBroadcast(getApplicationContext(),  
                               0,  deliveryIntent, 0);  
         
       // Register the Broadcast Receivers  
       registerReceiver(new BroadcastReceiver()  
       {  
           public void onReceive(Context _context, Intent _intent)  
           {  
               switch (getResultCode()) {  
               case Activity.RESULT_OK:  
                    break;  
               case SmsManager.RESULT_ERROR_GENERIC_FAILURE:  
                    break;  
               case SmsManager.RESULT_ERROR_RADIO_OFF:  
                   break;  
               case SmsManager.RESULT_ERROR_NULL_PDU:  
                   break;  
               }  
           }  
       },  
       new IntentFilter(SENT_SMS_ACTION));  
         
       registerReceiver(new BroadcastReceiver() {  
           @Override  
           public void onReceive(Context _context, Intent _intent)  
           {  
                 
           }  
       },  
       new IntentFilter(DELIVERED_SMS_ACTION));  
       // Send the message  
       smsManager.sendTextMessage(sendTo, null, myMessage, sentPI, deliverPI);  



接收短信要注册一个listener: 

final String SMS_RECEIVED = "android.provider.Telephony.SMS_RECEIVED";  
IntentFilter filter = new IntentFilter(SMS_RECEIVED);  
BroadcastReceiver receiver = new IncomingSMSReceiver();  
registerReceiver(receiver, filter);   

自定义的listener:

class IncomingSMSReceiver extends BroadcastReceiver  
{  
   private static final String queryString = "@echo";  
   private static final String SMS_RECEIVED = "android.provider.Telephony.SMS_RECEIVED";  
     
   public void onReceive(Context _context, Intent _intent)  
   {  
       if (_intent.getAction().equals(SMS_RECEIVED))  
       {  
           SmsManager sms = SmsManager.getDefault();  
           Bundle bundle = _intent.getExtras();  
           if (bundle != null) {  
               Object[] pdus = (Object[]) bundle.get("pdus");  
               SmsMessage[] messages = new SmsMessage[pdus.length];  
               for (int i = 0; i < pdus.length; i++)  
                   messages[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);  
               for (int i=0; i<messages.length; i++)  
               {  
                   SmsMessage message = messages[i];  
                   String msg = message.getMessageBody();  
                   String to = message.getOriginatingAddress();  
                     
                   if (msg.toLowerCase().startsWith(queryString))  
                   {  
                       String out = msg.substring(queryString.length());  
                       sms.sendTextMessage(to, null, out, null, null);  
                   }  
               }  
           }  
       }  
   }  
}  

Android收发短信实现


标签: intent string本文链接 http://tec.5lulu.com/detail/110k3n1h9n13t8y22.html

我来评分 :6.1
0

转载注明:转自5lulu技术库

本站遵循:署名-非商业性使用-禁止演绎 3.0 共享协议

www.5lulu.com