2013-02-04 36 views
-1

我想開發一個SMS計數器應用程序,我要在其中計算當前日期的所有SMS。我不想統計收件箱中的短信。我正在使用下面的代碼,但沒有得到確切的結果。它正在計算總收件箱SMS。如何計算固定日期收件箱SMS在Android

TextView view = new TextView(this);

Uri uriSMSURI = Uri.parse("content://sms/inbox"); 
    Cursor cur = getContentResolver().query(uriSMSURI, null, null, null,null); 
    /* 
    int nsm = 0; 
    while(cur.moveToNext()){ 
    nsm+= + cur.getCount(); 
    } 
    */ 
    String sms = ""; 

    while (cur.moveToNext());{ 
    // sms += "From :" + cur.getString(2) + " : " + cur.getString(11)+"\n"; 
    // sms += cur.getString(2); 
     sms += "Total SMS in the INBOX : "+cur.getCount(); 
    } 
    view.setText(sms); 

    setContentView(view); 

我是一個新的學習者。提前致謝。

回答

-1
Uri uriSMSURI = Uri.parse("content://sms/inbox"); 
    long now = System.currentTimeMillis(); 
    long last24 = now - 24*60*60*1000;//24h in millis 
    String[] selectionArgs = new String[]{Long.toString(last24)}; 
    String selection = "date" + ">?"; 
    String[] projection = new String[]{"date"}; 
    Cursor cur = getContentResolver().query(uriSMSURI, projection, selection, selectionArgs,null); 

    String sms = String.valueOf(cur.getCount()); 

這是確切的resul我想感謝給我快速回復

+0

你不需要複製粘貼我告訴你的只是接受你自己的答案。 – p4u144

+0

我想關閉這個問題如何關閉這個 – IPL10

+0

爲什麼它不工作在只有htc 600c的願望? –

4

如果你想算你可以做這樣的事情在過去24小時內收到短信的號碼(不知道這實際上是工作,因爲我沒有測試它,但你的想法):

Uri uriSMSURI = Uri.parse("content://sms/inbox"); 
long now = System.currentTimeMillis(); 
long last24 = now - 24*60*60*1000;//24h in millis 
String[] selectionArgs = new String[]{Long.toString(last24); 
String selection = "WHERE " + "date" + ">?"; 
Cursor cur = getContentResolver().query(uriSMSURI, null, selection, selectionArgs,null); 

然後一個簡單的cur.getCount()應該得到你收到的短信數量。

+0

感謝您的快速回復。我試過上面的代碼,但它不工作的模擬器強制關閉 – IPL10

+0

當關閉時,你的日誌是什麼? 我可能忘了2件事(如我說我沒有測試) 嘗試這樣: 'Uri uriSMSURI = Uri.parse(「content:// sms/inbox」); long now = System.currentTimeMillis(); long last24 = now - 24 * 60 * 60 * 1000; // 24h in millis String [] selectionArgs = new String [] {Long.toString(last24)}; String selection =「date」+「>?」; String [] projection = new String [] {「date」} Cursor cur = getContentResolver()。query(uriSMSURI,projection,selection,selectionArgs,null);' – p4u144

+0

感謝您的回覆,現在不強迫關閉 但在你的代碼中,將存儲短信的計數並提供結果?在我的代碼中我使用String sms =「」;獲得UI輸出 – IPL10