2012-03-01 4 views
1

私のコードをチェックアウト私はSMSアプリケーションでいくつかの作業をしていますが、私は現在、SMSに付属しているアドレスと連絡先に格納されている番号を照合しようとしています...ヘルプが必要... SMSの連絡先の照合

エミュレータには2つの連絡先しか含まれていないため、これまでの連絡先と一致することができず、送信者の名前が表示されていませんでした。接触...

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; 

    } 

答えて

0

私は完全にあなたが私が得たhere.Butをコード化されたものを理解していなかったあなたのrequirement.Soこのように(私がBにそれを試していない、しようとしている私の提案グループ化するには

各接点からのすべてのSMS:EDIT

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

:今の時間の不足が、論理的にそれが動作するはず)のecause

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(); 
     } 
    } 
+0

:) ....それを一般化するのに役立ちます:私の編集した答え多くは、私が探していたものthatsの – Hiral

+0

はい感謝を参照してください。 ...しかし、今私はすべてのSMSを同じ連絡先からグループ化する必要があります...そして、保存されている連絡先もいくつかあります...国コードだけが違いますが、表示されません。私はこれらの2つのことを成し遂げますか? – kashifmehmood

+0

@kashifmehmood:私は自分の答えを更新しました。そして、あなた自身もコードを作成してください。完全なコードを書くことはできません。上記のコードより、どのように動作するか考えてみましょう。 。 – Hiral

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

で、このコードが実行されることはありません、なぜ...私は私のギャラクシー上のコードを実行しています...把握カント...

助手席... ...?