2012-02-23 55 views
0

這是我用來在上下文菜單中按下查看按鈕時閱讀收件箱郵件的代碼,但我無法閱讀郵件,我只收到第一封郵件的正文。如何從收件箱中讀取郵件

public class Smsread extends Activity { 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    TextView view = new TextView(this); 
    Uri uri = Uri.parse("content://sms/inbox"); 
    Cursor c= getContentResolver().query(uri, null, null ,null,null); 
    startManagingCursor(c); 
    String sms = ""; 
    if(c.moveToFirst()){ 
     for(int i=0;i<c.getCount();i++){ 
    sms= c.getString(c.getColumnIndexOrThrow("body")).toString(); 
    // sms=c.getString(c.getColumnIndexOrThrow("address")).toString(); 

     } 
     c.close(); 
     c.moveToNext(); 

     /* 
    sms ="From :" + c.getString(2) + " : " + c.getString(11)+"\n";   
    */ 
    } 

    view.setText(sms); 
    setContentView(view); 

} 
}    

回答

2

試試這個:

if(c.moveToFirst()){ 

     for(int i=0;i<c.getCount();i++){ 

     sms= c.getString(c.getColumnIndexOrThrow("body")).toString(); 
     c.moveToNext(); 
     } 
} 

你必須移動到下一條記錄爲loop.then的每個迭代只,你會得到的所有消息there.Here,在for循環,之後你得到String sms,你可以使用那個短信顯示在某個地方。每次,它會抓住下一個短信到它!

希望你明白了!

+0

很好的回答短期和甜+1 – 2012-02-23 06:23:22

+0

@parag:感謝.. :) – Hiral 2012-02-23 06:27:57

+0

@parag :感謝parag你說的邏輯是正確的,但我嘗試在我的程序中應用相同的,但仍然當我按下查看按鈕爲e ach消息我得到相同的第一條消息的主體。 – dippu 2012-02-26 05:30:26

0

您必須更改

sms= c.getString(c.getColumnIndexOrThrow("body")).toString(); 

sms+= c.getString(c.getColumnIndexOrThrow("body")).toString(); 
0

試試這個:

String[] sms=new String[c.getCount()]; 

if(c.moveToFirst()){ 

     do{ 
     sms= c.getString(c.getColumnIndexOrThrow("body")).toString(); 

     } while(c.moveToNext()); 

} 
相關問題