【问题标题】:Read SMS from a specific number从特定号码读取短信
【发布时间】:2013-05-04 03:51:45
【问题描述】:

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

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

【问题讨论】:

    标签: android sms android-activity


    【解决方案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" />
    

    【讨论】:

    • @user2207848 你需要更具体。你是什​​么意思“它没有工作”??
    • @Mihir Shah: 有没有办法从特定的联系电话中读取所有短信?
    • String SMS_URI_INBOX = "content://sms/inbox"; 收到的短信 ↓(进来)String SMS_URI_ALL = "content://sms/"; 收到进来的短信,然后你发送到那个手机 ↑↓(全部)
    【解决方案2】:

    您已经非常接近了。我建议您查看ContentResolver.query 方法的参数,并特别注意selection 参数。您要做的只是选择特定列等于您要查找的数字的消息...

    类似

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

    我不知道具体的列名,但这应该可以让你朝着正确的方向开始......

    【讨论】:

    • @user2207848 你需要更具体。你是什​​么意思“它没有工作”??
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-29
    • 2019-03-05
    • 2016-02-25
    • 1970-01-01
    相关资源
    最近更新 更多