2013-05-03 56 views
1

我想阅读收件箱中的特定短信。我在互联网上发现如何阅读收件箱中的所有短信。这就是我所做的。请帮我阅读一个特定号码的短信。谢谢从特定号码读取短信

package com.example.liresms; 

import android.app.Activity; 
import android.database.Cursor; 
import android.net.Uri; 
import android.os.Bundle; 
import android.widget.TextView; 

public class ReadSMS extends MainActivity { 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     TextView view = new TextView(this); 
     Uri uriSMSURI = Uri.parse("content://sms/inbox"); 
     Cursor cur = getContentResolver().query(uriSMSURI, null, null, null,null); 
     String sms = ""; 
     while (cur.moveToNext()) { 
      sms += "From :" + cur.getString(2) + " : " + cur.getString(11)+"\n";   
     } 
     view.setText(sms); 
     setContentView(view); 
    } 
} 

回答

0

你已经非常接近已经这样做。我建议你看一下ContentResolver.query方法的参数,并特别注意selection参数。什么你希望做的是只选择讯息,其中一个特定列等于你正在寻找...

喜欢的东西

Cursor cur = getContentResolver().query(uriSMSURI, null, "from=6159995555", null,null); 

我不知道具体列名的数量把我的头顶部,但应该让你在正确的方向开始......

+0

不,它没有工作! – user2207848 2013-05-03 23:34:31

+1

@ user2207848你需要更具体。你是什​​么意思“它没有工作”? – 2013-05-04 12:54:46

1

试试这个:

StringBuilder smsBuilder = new StringBuilder(); 
final String SMS_URI_INBOX = "content://sms/inbox"; 
final String SMS_URI_ALL = "content://sms/"; 
try 
{ 
    Uri uri = Uri.parse(SMS_URI_INBOX); 
    String[] projection = new String[] { "_id", "address", "person", "body", "date", "type" }; 
    Cursor cur = getContentResolver().query(uri, projection, "address=123456789", null, "date desc"); 
    if (cur.moveToFirst()) 
    { 
     int index_Address = cur.getColumnIndex("address"); 
     int index_Person = cur.getColumnIndex("person"); 
     int index_Body = cur.getColumnIndex("body"); 
     int index_Date = cur.getColumnIndex("date"); 
     int index_Type = cur.getColumnIndex("type");   
     do 
     { 
      String strAddress = cur.getString(index_Address); 
      int intPerson = cur.getInt(index_Person); 
      String strbody = cur.getString(index_Body); 
      long longDate = cur.getLong(index_Date); 
      int int_Type = cur.getInt(index_Type); 

      smsBuilder.append("[ "); 
      smsBuilder.append(strAddress + ", "); 
      smsBuilder.append(intPerson + ", "); 
      smsBuilder.append(strbody + ", "); 
      smsBuilder.append(longDate + ", "); 
      smsBuilder.append(int_Type); 
      smsBuilder.append(" ]\n\n"); 
     }while (cur.moveToNext()); 
     if (!cur.isClosed()) 
     { 
      cur.close(); 
      cur = null; 
     } 
    } 
    else 
    { 
     smsBuilder.append("no result!"); 
    } 
} 
catch (SQLiteException ex) 
{ 
    Log.d("SQLiteException", ex.getMessage()); 
} 

在AndroidManifest.xml中包括此权限:

<uses-permission android:name="android.permission.READ_SMS" /> 
+0

thx为你的帮助,但它没有工作 – user2207848 2013-05-03 23:35:41

+1

@ user2207848你需要更具体。你是什​​么意思“它没有工作”? – 2013-05-04 12:54:21

+0

@Mhhir Shah:有什么方法可以读取特定联系人号码中的所有短信吗? – 2015-02-19 12:01:28