Android Contacts(二) SMS短信与Contacts联系人关联

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

}

以上是小编为您精心准备的的内容,在的博客、问答、公众号、人物、课程等栏目也有的相关内容,欢迎继续使用右上角搜索按钮进行搜索string
, scrollviewer
, scrollview
, cursor
, uri
, android 过滤
, null
, import
, sms
, android scrollview
, android cursor
, 手机sms怎样取得
, address
android databaseandroid cursor
com.android.contacts、android contacts、android contacts2.db、android contacts详解、android 6.0 contacts,以便于您获取更多的相关知识。

时间: 2024-08-07 06:02:58

Android Contacts(二) SMS短信与Contacts联系人关联的相关文章

Android API开发之SMS短信服务处理和获取联系人的方法_Android

本文实例讲述了Android API开发之SMS短信服务处理和获取联系人的方法.分享给大家供大家参考,具体如下: Android API支持开发可以发送和接收SMS消息的应用程序.目前我们开发过程中使用的Android模拟器还不支持发送SMS,但它可以接收SMS.现在我们来探索一下Android对SMS的支持,我们将会构建一个小小的应用程序来监听移动设备(或模拟器)上接收到的SMS消息,并将它显示出来. 我们来定义一个Intent接收器来处理SMS接收事件: package com.wissen

Android API开发之SMS短信服务处理和获取联系人的方法

本文实例讲述了Android API开发之SMS短信服务处理和获取联系人的方法.分享给大家供大家参考,具体如下: Android API支持开发可以发送和接收SMS消息的应用程序.目前我们开发过程中使用的Android模拟器还不支持发送SMS,但它可以接收SMS.现在我们来探索一下Android对SMS的支持,我们将会构建一个小小的应用程序来监听移动设备(或模拟器)上接收到的SMS消息,并将它显示出来. 我们来定义一个Intent接收器来处理SMS接收事件: package com.wissen

代码-安卓 sms短信接收功能

问题描述 安卓 sms短信接收功能 Intent intent = new Intent(); intent.setClassName("com.android.mms","com.android.mms.transaction.SmsReceiverService"); // 准备调用com.android.mms.transaction.SmsReceiverService intent.setAction("android.provider.Tele

Android中用Bmob实现短信验证码功能的方法详解_Android

 这篇文章主要介绍发送验证码和校验验证码的功能,用到一个第三方平台Bmob,那Bmob是什么呢?Bmob可以开发一个云存储的移动应用软件,他提供了大量的标准的API接口,根据需要接入相关服务,开发者可以更加专注于应用的开发,让产品交付更快速,验证码功能就是其中一个. 一.跟其他第三方一样,我们开发之前要做一些准备工作. 1.首先,去官网注册一个帐号:http://www.bmob.cn/: 2.然后就可以创建应用了:具体怎么做Bmob说得很清楚了(官方操作介绍),如果你不想看,我简单说一下:点击

短信拦截-android BroadcastReceiver中拦截短信后如何给回调activity

问题描述 android BroadcastReceiver中拦截短信后如何给回调activity (4.3之前的)应该是调用接口,但具体的回调接口不清楚,也不清楚如何自动刷新 解决方案 Android-拦截短信(BroadcastReceiver)Android手机短信拦截---BroadcastReceiver(一) 解决方案二: // 注册接收.监听短信receiver smsReceiver = new SMSReceiver(); IntentFilter receiverFilter

android 广播机制,短信电话 拦截

问题描述 android 广播机制,短信电话 拦截 我需要做的是一个安卓小项目,广播机制实现对短信.电话 的拦截,电话短信 均为sqlite数据库的两张表 ,求大神指定? 解决方案 android 关于特定短信电话拦截android短信和广播机制android--广播及短信拦截 解决方案二: 电话 contacts2.db 短信 mmssms.db 解决方案三: http://blog.sina.com.cn/s/blog_a28e3dd901018wky.html

Android编程实现拦截短信并屏蔽系统Notification的方法_Android

本文实例讲述了Android编程实现拦截短信并屏蔽系统Notification的方法.分享给大家供大家参考,具体如下: 拦截短信有几个关键点: 1.android接收短信时是以广播的方式 2.程序只要在自己的Manifest.xml里加有"接收"SMS的权限 <uses-permission android:name="android.permission.RECEIVE_SMS"> </uses-permission> <uses-p

Android使用Intent发送短信的实现方法_Android

本文实例讲述了Android使用Intent发送短信的实现方法.分享给大家供大家参考,具体如下: 在一个Activity中使用Intent发送短信 package com.zhuguangwei; import android.app.Activity; import android.content.Intent; import android.net.Uri; import android.os.Bundle; import android.view.View; import android.

谷歌在非洲地区战略:推动SMS短信搜索和SMS短信交易

北京时间6月30日凌晨消息,据国外媒体报道,著名科技博客作者埃里克·施恩菲尔德(Erick Schonfeld)今天撰文称,谷歌在非洲地区的战略是推动SMS短信搜索和通过SMS短信进行的交易. 施恩菲尔德写道:"谷歌不只是希望将世界上所有的信息组织到一起,同时也希望让世界所有人都能获得所有信息.从全球大多数人的角度来说,这意味着谷歌要通过普通手机来传递信息,而不是通过价格昂贵的iPhone或带有网络浏览器的Android手机.我所说的普通手机是指10美元就能买到的手机,这种手机除了语音和SMS短