从收件箱中加载特定数量的Sms

时间:2013-11-20 11:47:58

标签: android list sms

我已经实现了代码,以便从收件箱中获取短信到我的应用程序。它获取所有消息。但我想从特定号码加载消息。我按照[阅读特定发件人的所有短信显示空视图的教程。我制定了这段代码。

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.inbox);

        ListView list = (ListView) findViewById(R.id.listView1);
        List<String> msgList = getSMS();
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, msgList);
        list.setAdapter(adapter);

 }
        public List<String> getSMS() {
            List<String> sms = new ArrayList<String>();
            // 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='5558'", null, null);


         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); 



                                 sms.add("Number: " + strAddress + " .Message: " + strbody);

                               //  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!");  
                         } // end if  

                     }} catch (SQLiteException ex) {  
                         Log.d("SQLiteException", ex.getMessage());  

                     }
                     return sms;
                }

我通过地址作为我的另一个模拟器。如果我将null替换为getContentResolver()的地址字段,它将加载收件箱中的所有短信。任何人都可以帮助我修改我的位置吗?

2 个答案:

答案 0 :(得分:6)

使用以下代码

Uri uri = Uri.parse("content://sms/");

ContentResolver contentResolver = getContentResolver();

String phoneNumber = "+911234567890";
String sms = "address='"+ phoneNumber + "'";
Cursor cursor = contentResolver.query(uri, new String[] { "_id", "body" }, sms, null,   null);

System.out.println ( cursor.getCount() );

while (cursor.moveToNext()) 
{
    String strbody = cursor.getString( cursor.getColumnIndex("body") );
    System.out.println ( strbody );
}

需要获得以下许可,

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

答案 1 :(得分:0)

  

100%工作代码:

   String[] projection = new String[] { "_id", "thread_id","address", "person", "body", "date", "type" };
    Uri uri = Uri.parse("content://sms/");



    Cursor c = cr.query(uri, projection,"address='9876543210'",null, "date desc");

    int totalSMS = 0;
    if (c != null) {
        totalSMS = c.getCount();
        if (c.moveToFirst()) {
            for (int j = 0; j < totalSMS; j++) {

                String id = c.getString(c.getColumnIndexOrThrow(Telephony.Sms._ID));
                String thread_id = c.getString(c.getColumnIndexOrThrow(Telephony.Sms.THREAD_ID));
                String smsDate = c.getString(c.getColumnIndexOrThrow(Telephony.Sms.DATE));
                String number = c.getString(c.getColumnIndexOrThrow(Telephony.Sms.ADDRESS));
                String body = c.getString(c.getColumnIndexOrThrow(Telephony.Sms.BODY));


                String type;

                switch (Integer.parseInt(c.getString(c.getColumnIndexOrThrow(Telephony.Sms.TYPE)))) {
                    case Telephony.Sms.MESSAGE_TYPE_INBOX:
                        type = "inbox";
                        messageList.add(new Message(id, thread_id, number, body, smsDate, type));
                        break;
                    case Telephony.Sms.MESSAGE_TYPE_SENT:
                        type = "sent";
                        messageList.add(new Message(id, thread_id, number, body, smsDate, type));
                        break;
                    case Telephony.Sms.MESSAGE_TYPE_OUTBOX:
                        break;
                    default:
                        break;
                }

                c.moveToNext();
            }
        }

        c.close();