Android的手机短信获取的谈话名称或地址手机短信、名称、地址、Android

2023-09-04 10:06:15 作者:笑谈浮生流年

我开发一个SMS程序,我想获得的谈话。 我写的code以下,它工作正常,但我不知道它可能是更有效的。

I'm developing an SMS program and I want to get conversations. I wrote the code below and it works fine, but I wonder if it could be more efficient

这是歌厅对话线程

       Uri SMS_INBOX = Uri.parse("content://sms/conversations/");
    Cursor c = getContentResolver().query(SMS_INBOX, null, null, null, "date desc");

        startManagingCursor(c);
        String[] count = new String[c.getCount()];
        String[] snippet = new String[c.getCount()];
        String[] thread_id = new String[c.getCount()];

        c.moveToFirst(); 
        for (int i = 0; i < c.getCount(); i++) {
            count[i] = c.getString(c.getColumnIndexOrThrow("msg_count"))
                    .toString();
            thread_id[i] = c.getString(c.getColumnIndexOrThrow("thread_id"))
                    .toString();
            snippet[i] = c.getString(c.getColumnIndexOrThrow("snippet"))
                    .toString();
            //Toast.makeText(getApplicationContext(), count[i] + " - " + thread_id[i]+" - "+snippet[i] , Toast.LENGTH_LONG).show();
            c.moveToNext();
        }
        c.close();

根据会话线程获取地址

for getting addresses according to conversation thread

    for(int ad = 0; ad < thread_id.length ; ad++)
    {
    Uri uri = Uri.parse("content://sms/inbox");
    String where = "thread_id="+thread_id[ad]; 
    Cursor mycursor= getContentResolver().query(uri, null, where ,null,null); 
    startManagingCursor(mycursor);

    String[] number = new String[mycursor.getCount()];


    if(mycursor.moveToFirst()){
            for(int i=0;i<mycursor.getCount();i++){
                    number[i]=mycursor.getString(mycursor.getColumnIndexOrThrow("address")).toString();

                    mycursor.moveToNext();
            }
    }
    mycursor.close();

和最后检查不会忽略(如果联系人列表),并添加到列表中。

and finally checking the adresses (if in contact list) and adding to a list

     for(int i =0 ; i < numaralar.length ;i++)
    {


    String a = numaralar[i].substring(0,1);



    if(!a.equals("+")){ kisiismi = numaralar[i]; }


    ContentResolver localContentResolver = getApplicationContext().getContentResolver();
       Cursor contactLookupCursor =  
        localContentResolver.query(
                 Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, 
                 Uri.encode(numaralar[i])), 
                 new String[] {PhoneLookup.DISPLAY_NAME, PhoneLookup._ID}, 
                 null, 
                 null, 
                 null);
        try {
        while(contactLookupCursor.moveToNext()){
             String contactName = contactLookupCursor.getString(contactLookupCursor.getColumnIndexOrThrow(PhoneLookup.DISPLAY_NAME));
             kisiismi = contactName;
         }
        }catch (Exception e) {
         kisiismi = numaralar[i].toString(); 

        }
          finally {
              //Toast.makeText(getApplicationContext(), ad + kisiismi + " " + count[ad], Toast.LENGTH_LONG).show();
              myArr.add(kisiismi);
              contactLookupCursor.close();
         }

    }    

有没有办法让这个过程变得更简单?

Are there any way to make this process easier?

推荐答案

从奇巧开始,我们有一个特定的URI这个:内容://彩信短信/邮件/ byphone 。但在此之前,这是我们可以拿出:

From KitKat onwards, we have a specific Uri for this: content://mms-sms/messages/byphone. But before that, this is what we can come up with:

Set<Integer> conversationIds = new HashSet<Integer>();
String numbers = "+xxxxxxxxxx,+yyyyyyyyyy";

final String[] PROJECTION = { Sms._ID, Sms.THREAD_ID };
final String SELECTION = Sms.ADDRESS + " IN (" + numbers.replaceAll("[^,]+", "?") + ")";
final String[] selectionArgs = numbers.split(",");

Cursor cursor = context.getContentResolver().query(Sms.CONTENT_URI, PROJECTION, SELECTION, selectionArgs, null);
int threadColumn = cursor.getColumnIndexOrThrow(Sms.THREAD_ID);

while (cursor.moveToNext())
  conversationIds.add(cursor.getInt(threadColumn));
cursor.close();

return conversationIds;

有没有简单的方法做同样的MMS消息,因为他们不守他们的地址在数据库中,你必须单独查询每个人。如果你需要反复地做,一个可行的解决方案就是缓存彩信到手机号码的关系在自己的数据库中。

There is no easy way to do the same with MMS messages because they don't keep their address in the database, you have to query each and every one separately. If you need to do it repeatedly, a viable solution is to cache MMS-to-phone number relations in a database of your own.

您可以再使用 conversationIds 积累的标识符来查询个人交谈。请注意,如果要合并属于同一接触不同coversations,你可以重复使用相同的查询选择模式上面传递的所有ID为在(?,?,?)一次。

You can then use the identifiers accumulated in conversationIds to query the individual conversations. Note that if you want to merge different coversations belonging to the same contact, you can reuse the same query selection pattern above with passing in all ids as IN (?,...,?) at once.

 
精彩推荐
图片推荐