Android 从收件箱加载特定号码的短信

Android 从收件箱加载特定号码的短信,android,list,sms,Android,List,Sms,我已经实现了将收件箱中的短信发送到我的应用程序的代码。它会接收所有邮件。但我想从特定的号码加载邮件。我遵循了[阅读特定发件人的所有短信,它显示空视图]中的教程。我计算出了此代码 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.inbox); L

我已经实现了将收件箱中的短信发送到我的应用程序的代码。它会接收所有邮件。但我想从特定的号码加载邮件。我遵循了[阅读特定发件人的所有短信,它显示空视图]中的教程。我计算出了此代码

@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;
                }
@覆盖
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.inbox);
ListView列表=(ListView)findViewById(R.id.listView1);
List msgList=getSMS();
ArrayAdapter=新的ArrayAdapter(这是android.R.layout.simple\u list\u item\u 1,msgList);
list.setAdapter(适配器);
}
公共列表getSMS(){
列表sms=新的ArrayList();
//StringBuilder smsBuilder=新StringBuilder();
最终字符串SMS\u URI\u收件箱=”content://sms/inbox"; 
最后一个字符串SMS\U URI\U ALL=”content://sms/";  
试试{
Uri=Uri.parse(SMS\u Uri\u收件箱);
String[]projection=新字符串[]{“\u id”,“address”,“person”,“body”,“date”,“type”};
Cursor cur=getContentResolver().query(uri,投影,“address='5558',null,null);
if(cur.moveToFirst()){
int index_Address=cur.getColumnIndex(“地址”);
int index_Person=cur.getColumnIndex(“Person”);
int index_Body=cur.getColumnIndex(“Body”);
int index_Date=cur.getColumnIndex(“日期”);
int index_Type=cur.getColumnIndex(“Type”);
做{
String strAddress=cur.getString(索引地址);
int intPerson=cur.getInt(索引人);
字符串strbody=cur.getString(索引\正文);
long longDate=cur.getLong(索引日期);
int类型=cur.getInt(索引类型);
短信。添加(“号码:+StradAddress+”。信息:+Strabody);
//smsBuilder.append(“[”);
//smsBuilder.append(StradAddress+“,”);
//追加(intPerson+“,”);
//追加(strbody+“,”);
//追加(longDate+“,”);
//附加(int_类型);
//smsBuilder.append(“]\n\n”);
}while(cur.moveToNext());
如果(!cur.isClosed()){
cur.close();
cur=null;
}  
否则{
//追加(“无结果!”);
}//如果结束,则结束
}}catch(SQLiteException ex){
Log.d(“SQLiteException”,例如getMessage());
}
回复短信;
}
我将地址作为另一个仿真器传递。如果我将getContentResolver()的地址字段设为null,它将加载收件箱中的所有短信。有人能帮我修改哪些内容吗?

使用以下代码

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

使用以下代码

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

100%工作代码:

String[]projection=新字符串[]{“\u id”,“thread\u id”,“address”,“person”,“body”,“date”,“type”};
Uri=Uri.parse(“content://sms/");
游标c=cr.query(uri,投影,“address='9876543210',null,“date desc”);
int totalSMS=0;
如果(c!=null){
totalSMS=c.getCount();
if(c.moveToFirst()){
对于(int j=0;j
100%工作代码:

String[]projection=新字符串[]{“\u id”,“thread\u id”,“address”,“person”,“body”,“date”,“type”};
Uri=Uri.parse(“content://sms/");
游标c=cr.query(uri,投影,“address='9876543210',null,“date desc”);
int totalSMS=0;
如果(c!=null){
totalSMS=c.getCount();
if(c.moveToFirst()){
对于(int j=0;j