2014-07-19 13 views
2

我想在android上創建一個應用程序,我需要像時尚聊天一樣顯示消息。我一直在閱讀SMS的4.4 API,我似乎無法弄清楚如何使用這些。目前,我只收到來自sms.Inbox Content Provider的收到消息。 有人能指點我在哪裏可以看到一些關於如何有效利用它們的例子?Android:使用android.provider.Telephony.Sms.Conversations

回答

1

使用BroadcastReceiver拿到轉播時,短信會被手機接收,如下圖所示:

public class SmsReceiver extends BroadcastReceiver { 
     @Override 
     public void onReceive(Context context, Intent intent) { 

       Bundle extras = intent.getExtras(); 
       if (extras == null) 
        return; 

       // To display a Toast whenever there is an SMS. 
       // Toast.makeText(context,"Recieved",Toast.LENGTH_LONG).show(); 

       Object[] pdus = (Object[]) extras.get("pdus"); 
       for (int i = 0; i < pdus.length; i++) { 
        SmsMessage SMessage = SmsMessage.createFromPdu((byte[]) pdus[i]); 
        String sender = SMessage.getOriginatingAddress(); 
        String body = SMessage.getMessageBody().toString(); 

        // A custom Intent that will used as another Broadcast 
        Intent in = new Intent("SmsMessage.intent.MAIN").putExtra(
            "get_msg", sender + ":" + body); 

        // You can place your check conditions here(on the SMS or the 
        // sender) 
        // and then send another broadcast 
        context.sendBroadcast(in); 

        // This is used to abort the broadcast and can be used to silently 
        // process incoming message and prevent it from further being 
        // broadcasted. Avoid this, as this is not the way to program an 
        // app. 
        // this.abortBroadcast(); 
       } 
     } 
+0

謝謝您的回答。但我目前需要的是已經存儲的數據。而不是傳入的消息。雖然我真的很感激你張貼一些代碼 – RavenXV