【问题标题】:need help...contact matching for SMS需要帮助...短信联系人匹配
【发布时间】:2012-03-01 05:35:10
【问题描述】:

检查我的代码我一直在做一些关于短信应用程序的工作,现在我正在尝试将短信随附的地址与联系人中存储的号码进行匹配...

到目前为止,我无法匹配联系人并显示发件人的姓名,因为在模拟器上仅包含两个联系人...它匹配第一个并显示其姓名,而在电话上却找不到单个联系人...

public List<String> getSMS() {

        List<String> sms = new ArrayList<String>();
        Uri uriSMSURI = Uri.parse("content://sms/inbox");
        Cursor cur = getContentResolver().query(uriSMSURI, null, null, null,
                null);

        ContentResolver cr = getContentResolver();
        Cursor contactsCur = cr.query(ContactsContract.Contacts.CONTENT_URI,
                null, null, null, null);
        Cursor phoneCur = cr.query(
                ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
                "DISPLAY_NAME = '" + name + "'", null, null);

        Log.v(LOG_TAG, "going in loop");
        if (contactsCur.getCount() > 0) {
            Log.v(LOG_TAG, "inside loop");
            while (contactsCur.moveToNext()) {
                Log.v(LOG_TAG, "cursor moving to next");
                id = contactsCur.getString(contactsCur
                        .getColumnIndex(ContactsContract.Contacts._ID));
                Log.i(LOG_TAG,"this is id: " + id);

                name = contactsCur
                        .getString(contactsCur
                                .getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
                Cursor phones = cr.query(Phone.CONTENT_URI, null,
                        Phone.CONTACT_ID + " = " + id, null, null);
                Log.i(LOG_TAG, "This is name "+name);
                while (phones.moveToNext()) 
                {
                     number = phones.getString(phones
                            .getColumnIndex(Phone.NUMBER));
                     String aNumber=number.replaceAll("-", "");
                     Log.i(LOG_TAG,"this is number :"+aNumber);

                    Log.v(LOG_TAG, "getting address and message");

                    while (cur.moveToNext()) {
                        String address = cur.getString(cur
                                .getColumnIndex("address"));
                        if(address.equals(aNumber))
                            address=name;
                        String body = cur.getString(cur
                                .getColumnIndexOrThrow("body"));

                        sms.add("Number: " + address+ " .Message: " + body);
                    }
                    Log.v(LOG_TAG, "closing phone cursor");
                    phoneCur.close();
                }

            }
        }

        return sms;

    }

【问题讨论】:

    标签: android


    【解决方案1】:

    查看此代码....

    String phone_number = contacts
                            .getString(contacts
                                    .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                    Log.i(null, "Stage 12");
                    // retrieve sms of phone_number from inbox
                    Cursor sms = getContentResolver().query(
                            Uri.parse("content://sms/inbox"), null,
                            "address='" + phone_number + "'", null, null);
                    Log.i(null, "Stage 13");
    
    
                    if (sms.getCount() > 0) {
                        Log.i(null, "Stage 14");
    
                        sms.moveToFirst();
                        for (int j = 0; j < sms.getCount(); j++) {
                            Log.i(null, "Stage 15");
    
                            // get sms body
                            String text = sms.getString(sms.getColumnIndex("body"));
                            // add "phone_number" and "text" to a list
                            groupSms.add(phone_number + ":" + text);
                            Log.i(null, "Stage 16");
    
                            // move to next sms from phone_number
                            sms.moveToNext();
                        }
                    }
                    // move to next contact
                    contacts.moveToNext();
                }
            }
            Log.i(null, "Stage 9");
            return groupSms;
    

    下面提到的代码永远不会执行......它是上面代码的一部分......

    if (sms.getCount() > 0) {
                            Log.i(null, "Stage 14");
    
                            sms.moveToFirst();
                            for (int j = 0; j < sms.getCount(); j++) {
                                Log.i(null, "Stage 15");
    
                                // get sms body
                                String text = sms.getString(sms.getColumnIndex("body"));
                                // add "phone_number" and "text" to a list
                                groupSms.add(phone_number + ":" + text);
                                Log.i(null, "Stage 16");
    
                                // move to next sms from phone_number
                                sms.moveToNext();
                            }
    

    这让我很头疼...我无法弄清楚为什么这段代码永远不会执行...我正在我的 Galaxy 上运行代码...

    任何帮助...?

    【讨论】:

      【解决方案2】:

      我没有完全理解你在这里编码的内容。但是我得到了你的要求。所以我的建议是-尝试这种方式(因为现在没有时间,我没有尝试过,但从逻辑上它应该可以工作) :

      public List<String> getSMS() {
      
              List<String> sms = new ArrayList<String>();
              //fetch all inbox sms
              Cursor inboxCursor = getContentResolver().query(Uri.parse("content://sms/inbox"), null, null, null,null);
      
              if(inboxCursor.getCount()>0)
              {
                  inboxCursor.moveToFirst();
                  for(int i=0;i<inboxCursor.getCount();i++)
                  {
                      //get phone-number of sms
                      String address=inboxCursor.getString(inboxCursor.getColumnIndex("address"));
      
                      //fetch contacts with same phone-number
                      Cursor contactCursor=getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,ContactsContract.CommonDataKinds.Phone.NUMBER+"='"+address+"'",null,null);
      
                      if(contactCursor.getCount()>0)
                      {
                          contactCursor.moveToFirst();
      
                          // fetch the name associated with that number in contacts saved
                          String name=contactCursor.getString(contactCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
                          //add number,name,message to list   
                          sms.add("Number: " + address+ " .Name: "+ name +".Message: " + body);
                      }
                      else 
                      {  
                          // for unknown contact
                          // add number,name,message to list                     
                          sms.add("Number: " + address+ " .Name: Unknown .Message: " + body);
                      }
                      inboxCursor.moveToNext();
                  }
              }
              return sms;
      }
      

      编辑:

      将来自每个联系人的所有短信分组:

          List<String> groupSms=new List<String>(); 
          // retrieve all contacts
          Cursor contacts=getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                          null,null,null,null);     
          if(contacts.getCount()>0)
          {
              contacts.moveToFirst();
              for(int i=0;i<contacts.getCount();i++)
              {    
                   // get phone number       
                   String phone_number=contacts.getString(contacts.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)) ;
                   // retrieve sms of phone_number from inbox
                   Cursor sms=getContentResolver().query(Uri.parse("content://sms/inbox"), null, "address='"+phone_number+"'", null,null);
                   if(sms.getCount()>0)
                   {
                        sms.moveToFirst();
                        for(int j=0;j<sms.getCount();j++)
                        {
                            // get sms body
                            String text=sms.getString(sms.getColumnIndex("body"));    
                            // add "phone_number" and "text" to a list
                            groupSms.add(phone_number+":"+text);
                            // move to next sms from phone_number
                            sms.moveToNext();
                        }
                   }
                   //move to next contact
                   contacts.moveToNext();
              }
          }
      

      【讨论】:

      • 是的,非常感谢,这就是我一直在寻找的......但现在我需要将来自同一个联系人的所有短信分组......并且有一些保存的联系人也包含国家代码...它只是国家代码的区别,但它不会显示..我怎样才能完成这两件事?
      • @kashifmehmood:我已经更新了我的答案。您也应该尝试自己编写代码。我们无法为您编写完整的代码。从上面的代码中,您将了解它是如何工作的。尝试编写代码,然后给 SO 带来问题。
      • 该代码不起作用....它不会对来自相同号码的短信进行分组,,,,有时会给出 nullpointerexception..但有时工作正常,但不要对来自相同号码的短信进行分组。 ...
      • 你在 groupSms 对象中最后得到了什么?把它们列出来。
      • 你的意思是将 groupSms 对象返回到列表中......?好吧,我也这样做了,但什么也没发生....
      【解决方案3】:

      当您处理电话号码时,它 (https://code.google.com/p/libphonenumber/) 可能有助于概括它.... :)

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-09-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-08-09
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多