Android 的SMS读取短信,可以获取发信人/收信人的手机号码(address),Contacts的联系人,可以过滤手机号码(address),因此SMS可以通过手机号码(address)关联到Contacts联系人

SMS - Contacts 关联代码

// 通过address手机号关联Contacts联系人的显示名字 private String getPeopleNameFromPerson(String address){ if(address == null || address == ""){ return "( no address )\n"; } String strPerson = "null"; String[] projection = new String[] {Phone.DISPLAY_NAME, Phone.NUMBER}; Uri uri_Person = Uri.withAppendedPath(ContactsContract.CommonDataKinds.Phone.CONTENT_FILTER_URI, address); // address 手机号过滤 Cursor cursor = getContentResolver().query(uri_Person, projection, null, null, null); if(cursor.moveToFirst()){ int index_PeopleName = cursor.getColumnIndex(Phone.DISPLAY_NAME); String strPeopleName = cursor.getString(index_PeopleName); strPerson = strPeopleName; } cursor.close(); return strPerson; }
SMS - Contacts 关联示例代码:package com.homer.phone; import java.sql.Date; import java.text.SimpleDateFormat; import android.app.Activity; import android.database.Cursor; import android.database.sqlite.SQLiteException; import android.net.Uri; import android.os.Bundle; import android.provider.ContactsContract; import android.provider.ContactsContract.CommonDataKinds.Phone; import android.util.Log; import android.widget.ScrollView; import android.widget.TextView; public class phoneRead2 extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); TextView tv = new TextView(this); tv.setText(getSmsInPhone()); ScrollView sv = new ScrollView(this); sv.addView(tv); setContentView(sv); } public String getSmsInPhone() { final String SMS_URI_ALL = "content://sms/"; final String SMS_URI_INBOX = "content://sms/inbox"; final String SMS_URI_SEND = "content://sms/sent"; final String SMS_URI_DRAFT = "content://sms/draft"; final String SMS_URI_OUTBOX = "content://sms/outbox"; final String SMS_URI_FAILED = "content://sms/failed"; final String SMS_URI_QUEUED = "content://sms/queued"; StringBuilder smsBuilder = new StringBuilder(); try { Uri uri = Uri.parse(SMS_URI_ALL); String[] projection = new String[] { "_id", "address", "person", "body", "date", "type" }; Cursor cur = getContentResolver().query(uri, projection, null, null, "date desc"); // 获取手机内部短信 if (cur.moveToFirst()) { int index_Address = cur.getColumnIndex("address"); int index_Person = cur.getColumnIndex("person"); int index_Body = cur.getColumnIndex("body"); int index_Date = cur.getColumnIndex("date"); int index_Type = cur.getColumnIndex("type"); do { String strAddress = cur.getString(index_Address); int intPerson = cur.getInt(index_Person); String strbody = cur.getString(index_Body); long longDate = cur.getLong(index_Date); int intType = cur.getInt(index_Type); SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); Date d = new Date(longDate); String strDate = dateFormat.format(d); String strType = ""; if (intType == 1) { strType = "接收"; } else if (intType == 2) { strType = "发送"; } else { strType = "null"; } String strAddress2 = getPeopleNameFromPerson(strAddress); smsBuilder.append("[ "); // smsBuilder.append(strAddress + ", "); smsBuilder.append(strAddress + " : " + strAddress2 + ", "); smsBuilder.append(intPerson + ", "); smsBuilder.append(strbody + ", "); smsBuilder.append(strDate + ", "); smsBuilder.append(strType); smsBuilder.append(" ]\n\n"); } while (cur.moveToNext()); if (!cur.isClosed()) { cur.close(); cur = null; } } else { smsBuilder.append("no result!"); } // end if smsBuilder.append("getSmsInPhone has executed!"); } catch (SQLiteException ex) { Log.d("SQLiteException in getSmsInPhone", ex.getMessage()); } return smsBuilder.toString(); } // 通过address手机号关联Contacts联系人的显示名字 private String getPeopleNameFromPerson(String address){ if(address == null || address == ""){ return "( no address )\n"; } String strPerson = "null"; String[] projection = new String[] {Phone.DISPLAY_NAME, Phone.NUMBER}; Uri uri_Person = Uri.withAppendedPath(ContactsContract.CommonDataKinds.Phone.CONTENT_FILTER_URI, address); // address 手机号过滤 Cursor cursor = getContentResolver().query(uri_Person, projection, null, null, null); if(cursor.moveToFirst()){ int index_PeopleName = cursor.getColumnIndex(Phone.DISPLAY_NAME); String strPeopleName = cursor.getString(index_PeopleName); strPerson = strPeopleName; } cursor.close(); return strPerson; } }

AndroidManifest.xml 权限

记得在AndroidManifest.xml中加入android.permission.READ_SMSandroid.permission.READ_CONTACTS这两个permission

<uses-permission android:name="android.permission.READ_SMS" />
<uses-permission android:name="android.permission.READ_CONTACTS" />

运行结果:

示例代码

参考推荐:

Android 之 Contacts 联系人读取

转载于:https://www.cnblogs.com/springmvc-hibernate/archive/2012/03/07/2484019.html

Android Contacts(二)—— SMS 短信 与 Contacts 联系人关联相关推荐

  1. 解忧云SMS短信服务平台系统 短信发送系统源码 全解密随时可以二开无后门

    解忧云SMS短信服务平台系统 短信发送系统 全解密完美版 经过一系列修复现在程序已经可以完全使用. 并且是全解密随时可以二开.无后门. 一些bug已经完全修复 安装教程 数据库配置文件路径 .env ...

  2. Android实时监听短信并上传服务器

    短信监听 Android监听手机短信的方法有两种,分别为: 1.接受系统的短信广播:当手机收到新消息时,会发送一条广播,通过该广播就可以获取短信内容: 2.监听短信数据库:利用观察者模式监听短信数据库 ...

  3. Android 接收和收发短信

    收发短信应该是每个手机最基本的功能之一了,即使是许多年前的老手机也都会具备这项功能,而Android作为出色的智能手机操作系统,自然也少不了在这方面的支持.每个Android手机都会内置一个短信应用程 ...

  4. Android 类似未读短信图标显示数字效果的分析

     之前一直以为是应用本身在对图标进行修改,看了源码之后发现其实主要的工作并不是应用自己完成的,主要的工作在是launcher里面完成的. 关于系统里面类似未读短信的具体处理流程如下,   原理 一个应 ...

  5. Android 类似未读短信消息图标显示的实现分析

    这里面主要的工作在是launcher里面完成的. 关于系统里面类似未读短信的具体处理流程如下, 原理 一个应用要实现这个效果,就要在自己有未读的消息的时候发送一个广播告诉系统我有未处理的事件了(例如: ...

  6. 中国网建java发送短信_短信验证登陆-中国网建提供的SMS短信平台

    一.JAVA发送手机短信常见的有三种方式(如下所列): 使用webservice接口发送手机短信,这个可以使用sina提供的webservice进行发送,但是需要进行注册 使用短信mao的方式进行短信 ...

  7. SMS短信通API下行接口参数

    为什么80%的码农都做不了架构师?>>>    JAVA发送手机短信 方法: (1)使用webservice接口发送手机短信,这个可以使用sina提供的webservice进行发送, ...

  8. android 脚本发短信,Android使用SmsManager实现短信发送功能

    安卓使用SmsManager实现发送短信,供大家参考,具体内容如下 关键代码实现 package com.example.sms; import java.util.ArrayList; import ...

  9. 华信云SMS短信平台介绍

    一.平台介绍 华信云SMS短信平台(简称:华信通)是一套高性能.易部署.高灵活度的短信网关平台,是面向大型企事业单位.政府机关等综合性的服务平台,提供面向客户的短信接入.发送.管理.统计.数据处理等综 ...

  10. java通过SMS短信平台实现发短信的功能

    一.使用java发送短信的方法: java发送手机短信,通常有几种方法: (1)使用webserive接口发送手机短信: (2)使用短信猫的方式进行短信的发送,比较常用的方式,不过需要购买硬件设备: ...

最新文章

  1. 天池供应链大赛来了!
  2. python操作excel-Python对Excel的读写等操作(转)
  3. SAP CRM WebClient UI配置的加载逻辑
  4. Pycharm常用高效技巧总结
  5. android:layout 冒号,android-json解析及简单例子(补汉6个汉字字).pdf
  6. 柔性数组和环形队列之间的故事
  7. c 语言 00字符串 截断,c语言截断字符串
  8. 建筑工程计算机的应用,计算机对建筑工程的应用
  9. VS+Qt应用开发-设置鼠标光标
  10. 【FPGA教程案例66】硬件开发板调试6——基于FPGA的UDP网口通信和数据传输
  11. 编码:decode()/encode()、quote()/unquote()
  12. win教程:如何查看本机的IP地址
  13. Android SeekBar 自定义 背景图片 和 滑块图片
  14. 局部连接层(Locally-Connected Layer)
  15. 解决127.0.0.1 已拒绝连接的方法
  16. 浅谈二层交换安全攻击与防御
  17. python可视化的优势_「数据可视化」数据可视化的优势有哪些?
  18. VS2017中自用部分插件的设置的翻译或功能介绍——Word Highlight With Margin
  19. vue+webpack5:If you want to include a polyfill, you need to
  20. 带权并查集【bzoj3362】: [Usaco2004 Feb]Navigation Nightmare 导航噩梦

热门文章

  1. session与cookie之间的关系
  2. 阶段5 3.微服务项目【学成在线】_day01 搭建环境 CMS服务端开发_09-CMS服务端工程搭建-开发工具配置...
  3. redis 部署方式及常见特性
  4. codeforces 597C (树状数组+DP)
  5. (转)谷歌安卓官方教程中文版
  6. Linux下启动Oracle数据库
  7. [转]请不要和陌生女人说话
  8. Beta冲刺提交-星期五
  9. 网络流24题 洛谷 2763 试题库问题
  10. [数据清洗]- Pandas 清洗“脏”数据(三)