gpt4 book ai didi

Android 收到带有姓名或地址的短信对话

转载 作者:IT王子 更新时间:2023-10-28 23:32:58 27 4
gpt4 key购买 nike

我正在开发一个 SMS 程序,我想进行对话。
我写了下面的代码,它工作正常,但我想知道它是否可以更有效

这是为了获取对话线程

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

最后检查地址(如果在联系人列表中)并添加到列表中

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

}

有什么方法可以让这个过程更简单吗?

最佳答案

由于它是一个简单的 SQLite 查询,并且可以以类似的方式访问联系人提供程序,因此您应该尝试通过数字、时间戳和 thrad_id 进行分组,然后将结果与查询匹配以在 SQL 中完成工作联系人提供者(也通过 SQLite)

该文档对所有可用列进行了很好的描述。 https://developer.android.com/reference/android/provider/ContactsContract.PhoneLookupColumns.html

关于Android 收到带有姓名或地址的短信对话,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12760802/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com