UniAppx 跳转Android 系统通讯录

工作中遇到的问题浅浅记录一下

跳转方法

TypeScript 复制代码
	//跳转系统 通讯录
	function jumpContacts(tag : number) {
		const context = UTSAndroid.getUniActivity()!;
		const intent = new Intent(Intent.ACTION_PICK);
		intent.setData(Uri.parse("content://com.android.contacts/data/phones"));
		intent.setType("vnd.android.cursor.dir/phone_v2");
		context.startActivityForResult(intent, tag.toInt());
	}

回调监听

TypeScript 复制代码
	onLoad(() => {
		console.log("sun,onLoad");
		httpGetInfoData()

		UTSAndroid.onAppActivityResult((requestCode : Int, resultCode : Int, data : Intent | null) => {
			if (resultCode != -1) return;

			if (data == null) return;

			const context = UTSAndroid.getUniActivity()!;
			const contactData = data.getData();
			if (context == null || contactData == null) return;

			const contentResolver = context.getContentResolver();

			// const projection : Array<string> = [
			// 	"data1",     // 电话号码(Phone.NUMBER)
			// 	"display_name" // 显示名称(Phone.DISPLAY_NAME)
			// ];

			const cursor = contentResolver.query(contactData, null, null, null, null);

			let name = "";
			let number = "";

			if (cursor != null && cursor.moveToFirst()) {
				try {
					number = cursor.getString(cursor.getColumnIndexOrThrow("data1"));
					name = cursor.getString(cursor.getColumnIndexOrThrow("display_name"));

					// 清理电话号码中的空格和特殊字符
					number = number.replace(/[()\s\-+]/g, '');
				} catch (e) {
					console.error("读取联系人失败", e);
				} finally {
					cursor.close();
				}
			}
			console.log("name", name)
			console.log("number", number)

			if (requestCode == contact1Tag) {
				contact1.value = number;
				contact1Name.value = name;
			} else if (requestCode == contact2Tag) {
				contact2.value = number;
				contact2Name.value = name;
			}


		})
	});
相关推荐
似霰24 分钟前
传统 Hal 开发笔记5 —— 添加硬件访问服务
android·framework·hal
恋猫de小郭27 分钟前
Android 宣布 Runtime 编译速度史诗级提升:在编译时间上优化了 18%
android·前端·flutter
csj5041 分钟前
安卓基础之《(4)—Activity组件》
android
游戏开发爱好者81 小时前
H5 混合应用加密 Web 资源暴露到 IPA 层防护的完整技术方案
android·前端·ios·小程序·uni-app·iphone·webview
2501_915106321 小时前
最新版本iOS系统设备管理功能全面指南
android·macos·ios·小程序·uni-app·cocoa·iphone
走在路上的菜鸟2 小时前
Android学Dart学习笔记第十四节 库和导库
android·笔记·学习·flutter
姜西西_2 小时前
自动化测试框架pytest之fixture
android·java·pytest
尐葮阿譽2 小时前
Ubuntu 25.10 安装 Waydroid 安卓模拟器
android·ubuntu·安卓模拟器·waydroid
_李小白2 小时前
【Android FrameWork】实战:跟踪Android APP Surface创建流程
android
2501_915918413 小时前
iOS 性能监控 运行时指标与系统行为的多工具协同方案
android·macos·ios·小程序·uni-app·cocoa·iphone