【问题标题】:Load Sms from inbox for specific number从收件箱加载特定号码的短信
【发布时间】:2013-11-20 11:47:57
【问题描述】:

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

@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;
                }

我将地址作为我的另一个模拟器传递。如果我将 getContentResolver() 的地址字段替换为空,它将加载收件箱中的所有短信。有人可以帮我修改哪里吗?

【问题讨论】:

    标签: android list sms


    【解决方案1】:

    使用以下代码,

    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"/>
    

    【讨论】:

    • 可以用5558这样的模拟器端口号代替手机号吗?
    • 你的模拟器收件箱里有短信吗?
    • 这行System.out.println ( cursor.getCount() ); 打印什么?
    • 是的,我收到了来自另一个模拟器的短信...如果我将 getContent Resolver 的所有值都设置为 null,它将加载所有消息...
    • 哦,我认为您应该指定发送短信的模拟器号码。我假设您正在使用当前模拟器的端口号。
    【解决方案2】:

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

    【讨论】:

    • 虽然此代码 sn-p 可能是解决方案,但 including an explanation 确实有助于提高您的帖子质量。请记住,您是在为将来的读者回答问题,而这些人可能不知道您提出代码建议的原因。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多