2012-10-17 42 views
0

可能重複所有消息:
How to access the SMS storage on Android?我怎麼能訪問在Android

你好我試圖訪問Android的所有消息。我有代碼,但我只通過此代碼訪問最後一條消息。怎麼讀全部?

public void onReceive(Context context, Intent intent) 
    { 
     //---get the SMS message passed in--- 
     Bundle bundle = intent.getExtras();   
     SmsMessage[] msgs = null;   
     if (bundle != null) 
     { 
      String header = ""; 
      String body = ""; 

      //---retrieve the SMS message received--- 
      Object[] pdus = (Object[]) bundle.get("pdus"); 
      msgs = new SmsMessage[pdus.length];    
      for (int i=0; i<msgs.length; i++) 
      { 
       msgs[i] = SmsMessage.createFromPdu((byte[])pdus[i]);     
       header = msgs[i].getOriginatingAddress();      
       body = msgs[i].getMessageBody().toString();  
      } 
      //---display the new SMS message--- 
      Toast.makeText(context,"Mesaj geldi", Toast.LENGTH_SHORT).show(); 
      Header.add(header); 
      Body.add(body); 
     } 
+0

可能重複[如何訪問Android上的短信存儲?](http://stackoverflow.com/questions/4809874/how-to-access-the-sms-storage-on-android)和http://stackoverflow.com/questions/848728/how-cani-i-從消息接收器讀取短消息從編程方式在Android – nandeesh

+0

這是一個消息接收器。訪問短信數據庫,請參閱http://stackoverflow.com/questions/4809874/how-to-access-the-sms-storage-on-android – njzk2

+0

謝謝,但我沒有在您的接收器中找到.. –

回答

0
Uri allMessage = Uri.parse("content://sms/"); 
ContentResolver cr = getContentResolver(); 
Cursor c = cr.query(allMessage, null, null, null, null); 
while (c.moveToNext()) { 
    String row = c.getString(1); 
} 

,您可以訪問所有的消息在Android中, 你可以在這裏獲得所有收件箱/發件箱,沒有帶測試,雖然它的

+0

不幸的是這個代碼沒有工作太多.. –