Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/220.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android短信按收件人ID显示联系人姓名_Android_Sms_Android Contacts - Fatal编程技术网

Android短信按收件人ID显示联系人姓名

Android短信按收件人ID显示联系人姓名,android,sms,android-contacts,Android,Sms,Android Contacts,下面的代码段返回基本的SMS会话数据: Cursor cursor = activity.getContentResolver().query(Uri.parse( "content://mms-sms/conversations?simple=true"), null, null, null, "normalized_date desc" ); if(cursor.moveToFirst()) String recipient_ids = cursor.getString(3); 我的问

下面的代码段返回基本的SMS会话数据:

Cursor cursor = activity.getContentResolver().query(Uri.parse( "content://mms-sms/conversations?simple=true"), null, null, null, "normalized_date desc" );
if(cursor.moveToFirst())
  String recipient_ids = cursor.getString(3);
我的问题是,如果有收件人的ID,我如何获取手机的联系信息?在这种情况下,我需要检索联系人号码和联系人姓名


非常感谢你的帮助。提前谢谢

这是,我的应用程序中的工作内容:

private String getContactNumber(final long recipientId) {
    String number = null;
    Cursor c = getContentResolver().query(ContentUris
            .withAppendedId(Uri.parse("content://mms-sms/canonical-address"), recipientId),
            null, null, null, null);
    if (c.moveToFirst()) {
        number = c.getString(0);
    }
    c.close();
    return number;
}
联系人号码保存在一个名为
规范地址
的表中
几年前有点像马车。联系人的更新未通过此表正确传播。但我想现在没问题了

您基本上需要将(列表)ID解析为单个ID。然后查询数据库中的每一项。

不过,您可以对所有ID一起使用一个查询。

这就是我的应用程序中的工作原理:

private String getContactNumber(final long recipientId) {
    String number = null;
    Cursor c = getContentResolver().query(ContentUris
            .withAppendedId(Uri.parse("content://mms-sms/canonical-address"), recipientId),
            null, null, null, null);
    if (c.moveToFirst()) {
        number = c.getString(0);
    }
    c.close();
    return number;
}
联系人号码保存在一个名为
规范地址
的表中
几年前有点像马车。联系人的更新未通过此表正确传播。但我想现在没问题了

您基本上需要将(列表)ID解析为单个ID。然后查询数据库中的每一项。
不过,您可以对所有ID一起使用一个查询。

试试以下方法:

public String getContactData(String id){
  String number;

  Cursor phones = fa.getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ id, null, null); 
    if(phones.moveToFirst()) { 
      number = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));       
    } 

  phones.close();
  return number;
}
希望有帮助

试试这个:

public String getContactData(String id){
  String number;

  Cursor phones = fa.getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ id, null, null); 
    if(phones.moveToFirst()) { 
      number = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));       
    } 

  phones.close();
  return number;
}

希望有帮助

感谢您的回答,但它给了我以下错误
java.lang.IllegalStateException:无法识别的URI:content://mms-sms/canonical-address
请尝试附加了/的uri。它在我的应用程序中已经运行了5年了尝试用选择的
“\u id=“+recipientId
替换contenturi,在某些手机中出现此异常致命异常:android.database.sqlite.SQLiteException无此列:data1(代码1):,编译时:从规范地址中选择data1,其中_id=1127感谢您的回答,但它会给我以下错误
java.lang.IllegalStateException:无法识别的URI:content://mms-sms/canonical-address
请尝试附加了/的uri。它在我的应用程序中已经运行了5年了尝试用选择的
“\u id=“+recipientId
替换contenturi,在某些手机中出现此异常致命异常:android.database.sqlite.SQLiteException无此列:data1(代码1):,编译时:从规范地址中选择数据1,其中_id=1127这似乎在Android 4.3的Galaxy Note 2上崩溃。该错误是由以下原因引起的:
:android.database.sqlite.SQLiteException:没有这样的列:normalized_date(代码1):,编译时:SELECT*FROM threads ORDER by normalized_date desc
。如何确定规范化的_date列是否不存在?我已经在其他版本的安卓系统上测试过了,没有问题。这在安卓4.3的Galaxy Note 2上似乎崩溃了。该错误是由以下原因引起的:
:android.database.sqlite.SQLiteException:没有这样的列:normalized_date(代码1):,编译时:SELECT*FROM threads ORDER by normalized_date desc
。如何确定规范化的_date列是否不存在?我已经在其他版本的android上测试过了,没有问题。