Android项目实战训练:通讯录(一)——获取手机通讯录信息(IMUDGES)

在MainActivity的同一级建立getNumber.java 文件

写入获取信息的代码:

package com.example.address_list;

import android.content.Context;
import android.database.Cursor;
import android.provider.ContactsContract;

public class getNumber {
    public static String getNumber(Context context){
        Cursor cursor = context.getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                null,null,null,null);
        String phoneNumber;
        String phoneName;
        while (cursor.moveToNext()){
            phoneNumber=cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
            phoneName = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
            System.out.println(phoneName + "  "+ phoneNumber);
        }
        return null;
    }
}

接下来,在主函数中载入:在MainActivity.java中加入一句“getNumber.getNumber(this);“。

最后,,

添加权限:

目录  app-> manifests-> AndroidManifest.xml

加入一句”<uses-permission android:name="android.permission.READ_CONTACTS"/>“

大功告成!

来看看结果:

前提是你的通讯录中有信息

 

### 如何通过编程读取手机通讯录 #### Python 实现方案 在 Python 中,可以通过 Android 的 `adb` 工具来访问设备上的联系人信息。以下是具体方法: 1. **安装 ADB 工具** 需要先安装 Android Debug Bridge (ADB),并确保可以连接到目标设备。 2. **编写脚本提取联系人信息** 使用 adb 命令获取联系人数据库中的数据,并解析其内容[^1]。 ```python import subprocess def get_contacts(): try: result = subprocess.run(['adb', 'shell', 'content', 'query', '--uri', 'content://contacts/phones/'], stdout=subprocess.PIPE) output = result.stdout.decode('utf-8') lines = output.splitlines() contacts = [] for line in lines: if "name=" in line and "number=" in line: name_start = line.find("name=") + 5 number_start = line.find("number=") + 7 name_end = line.find(",", name_start) number_end = line.find(",", number_start) contact_name = line[name_start:name_end].strip('"') contact_number = line[number_start:number_end].strip('"') contacts.append((contact_name, contact_number)) return contacts except Exception as e: print(f"Error occurred while fetching contacts: {e}") return [] if __name__ == "__main__": phonebook = get_contacts() for name, number in phonebook: print(f"{name}: {number}") ``` 此代码利用了 `adb shell content query` 来查询 Android 设备上存储的联系人记录。 --- #### Java 实现方案 对于 Android 应用开发而言,在 Java 或 Kotlin 编程环境中可以直接调用 Android SDK 提供的功能接口来操作系统的联系人数据库[^2]。 下面是个基于 Java 的简单例子展示如何从 Android 手机中检索联系人的姓名和电话号码: ```java package com.example.contactreader; import android.content.ContentResolver; import android.database.Cursor; import android.provider.ContactsContract; import java.util.ArrayList; import java.util.List; public class ContactReader { public static List<String> readContacts(ContentResolver resolver){ List<String> contactsList = new ArrayList<>(); Cursor cursor = null; try{ String[] projectionFields = new String[]{ ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME, ContactsContract.CommonDataKinds.Phone.NUMBER}; cursor = resolver.query( ContactsContract.CommonDataKinds.Phone.CONTENT_URI, projectionFields,null,null,null); int nameIndex = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME); int phoneNumberIndex = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER); if(cursor !=null &&cursor.getCount()>0 ){ while(cursor.moveToNext()){ String contactName=cursor.getString(nameIndex); String phoneNumber=cursor.getString(phoneNumberIndex).replaceAll("-",""); StringBuilder sb=new StringBuilder(); sb.append(contactName).append(":").append(phoneNumber); contactsList.add(sb.toString()); } } }finally{ if(cursor!=null){ cursor.close(); } } return contactsList; } } ``` 上述程序片段展示了如何借助 ContentProvider API 访问本地地址簿条目。 --- #### 注意事项 无论是采用哪种方式都需要特别注意权限管理问题。例如针对安卓应用来说可能需要声明如下权限才能正常工作: ```xml <uses-permission android:name="android.permission.READ_CONTACTS"/> ``` 同时还需要动态请求运行时授权以便于兼容更高版本的操作系统环境设置需求。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值