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


		})
	});
相关推荐
云博客-资源宝1 小时前
Android Manifest 权限描述大全
android·开发语言·php
xzkyd outpaper1 小时前
Android DataBinding 与 MVVM
android·计算机八股
zzq19962 小时前
Android 14.0 framework默认将三按钮的导航栏修改为手势导航。
android
ii_best2 小时前
[按键精灵安卓/ios脚本插件开发] 遍历获取LuaAuxLib函数库命令辅助工具
android·ios
峥嵘life3 小时前
Android Java语言转Kotlin语言学习指导实用攻略
android·java·kotlin
菌菇汤3 小时前
微信小程序传参过来了,但是数据没有获取到
微信小程序·小程序·uniapp
bubiyoushang8884 小时前
Kotlin中快速实现MVI架构
android·开发语言·kotlin
雨白5 小时前
Service 指南:从 Handler 机制到 Kotlin 协程
android
今阳6 小时前
鸿蒙开发笔记-17-ArkTS并发
android·前端·harmonyos
帅次6 小时前
Flutter动画全解析:从AnimatedContainer到AnimationController的完整指南
android·flutter·ios·小程序·kotlin·android studio·iphone