添加联系人编辑跳转接口
This commit is contained in:
		| @@ -18,13 +18,13 @@ def genVersionName(def versionName){ | ||||
| } | ||||
|  | ||||
| android { | ||||
|     compileSdkVersion 32 | ||||
|     buildToolsVersion "32.0.0" | ||||
|     compileSdkVersion 28 | ||||
|     buildToolsVersion "28.0.3" | ||||
|  | ||||
|     defaultConfig { | ||||
|         applicationId "cc.winboll.studio.contacts" | ||||
|         minSdkVersion 24 | ||||
|         targetSdkVersion 30 | ||||
|         targetSdkVersion 28 | ||||
|         versionCode 1 | ||||
|         // versionName 更新后需要手动设置  | ||||
|         // 项目模块目录的 build.gradle 文件的 stageCount=0 | ||||
|   | ||||
| @@ -1,8 +1,8 @@ | ||||
| #Created by .winboll/winboll_app_build.gradle | ||||
| #Sat Sep 27 15:23:51 HKT 2025 | ||||
| #Sat Oct 18 04:38:02 GMT 2025 | ||||
| stageCount=18 | ||||
| libraryProject= | ||||
| baseVersion=15.3 | ||||
| publishVersion=15.3.17 | ||||
| buildCount=0 | ||||
| buildCount=6 | ||||
| baseBetaVersion=15.3.18 | ||||
|   | ||||
| @@ -76,6 +76,9 @@ public class CallLogAdapter extends RecyclerView.Adapter<CallLogAdapter.CallLogV | ||||
| 									// Set the clipboard's primary clip. | ||||
| 									clipboard.setPrimaryClip(clip); | ||||
| 									Toast.makeText(mContext, "Copy to clipboard.", Toast.LENGTH_SHORT).show(); | ||||
| 								} else if (nItemId == R.id.item_calllog_phonenumber_add_contact) { | ||||
| 									//ToastUtils.show(callLog.getPhoneNumber()); | ||||
| 									ContactUtils.jumpToAddContact(mContext, callLog.getPhoneNumber()); | ||||
| 								} | ||||
|  | ||||
| 								return true; | ||||
|   | ||||
| @@ -24,6 +24,7 @@ import cc.winboll.studio.contacts.beans.ContactModel; | ||||
| import cc.winboll.studio.libaes.views.AOHPCTCSeekBar; | ||||
| import com.hjq.toast.ToastUtils; | ||||
| import java.util.List; | ||||
| import cc.winboll.studio.contacts.utils.ContactUtils; | ||||
|  | ||||
| public class ContactAdapter extends RecyclerView.Adapter<ContactAdapter.ContactViewHolder> { | ||||
|  | ||||
| @@ -69,6 +70,9 @@ public class ContactAdapter extends RecyclerView.Adapter<ContactAdapter.ContactV | ||||
| 									// Set the clipboard's primary clip. | ||||
| 									clipboard.setPrimaryClip(clip); | ||||
| 									Toast.makeText(mContext, "Copy to clipboard.", Toast.LENGTH_SHORT).show(); | ||||
| 								} else if (nItemId == R.id.item_calllog_phonenumber_edit_contact) { | ||||
| 									Long nContactId = ContactUtils.getContactIdByPhone(mContext, contact.getNumber()); | ||||
| 									ContactUtils.jumpToEditContact(mContext, contact.getNumber(), nContactId); | ||||
| 								} | ||||
|  | ||||
| 								return true; | ||||
|   | ||||
| @@ -25,7 +25,6 @@ import androidx.recyclerview.widget.RecyclerView; | ||||
| import cc.winboll.studio.contacts.R; | ||||
| import cc.winboll.studio.contacts.adapters.CallLogAdapter; | ||||
| import cc.winboll.studio.contacts.beans.CallLogModel; | ||||
| import com.hjq.toast.ToastUtils; | ||||
| import java.util.ArrayList; | ||||
| import java.util.Date; | ||||
| import java.util.List; | ||||
|   | ||||
| @@ -6,8 +6,11 @@ package cc.winboll.studio.contacts.utils; | ||||
|  * @Describe 联系人工具集 | ||||
|  */ | ||||
| import android.content.ContentResolver; | ||||
| import android.content.ContentUris; | ||||
| import android.content.Context; | ||||
| import android.content.Intent; | ||||
| import android.database.Cursor; | ||||
| import android.net.Uri; | ||||
| import android.provider.ContactsContract; | ||||
| import cc.winboll.studio.libappbase.LogUtils; | ||||
| import java.util.HashMap; | ||||
| @@ -120,4 +123,97 @@ public class ContactUtils { | ||||
|         } | ||||
|         return sbSpaceNumber.toString(); | ||||
|     } | ||||
|  | ||||
|  | ||||
| 	/** | ||||
| 	 * 跳转至系统添加联系人界面的工具函数 | ||||
| 	 * @param context 上下文(如 PhoneCallService、Activity、Fragment 均可,需传入有效上下文) | ||||
| 	 * @param phoneNumber 可选参数:预填的联系人电话(传 null 则跳转空表单) | ||||
| 	 */ | ||||
| 	public static void jumpToAddContact(Context mContext, String phoneNumber) { | ||||
| 		Intent intent = new Intent(Intent.ACTION_INSERT); | ||||
| 		intent.setType("vnd.android.cursor.dir/person"); | ||||
| 		intent.putExtra(android.provider.ContactsContract.Intents.Insert.PHONE, phoneNumber); | ||||
| 		mContext.startActivity(intent); | ||||
| 	} | ||||
| 	 | ||||
| 	/** | ||||
| 	 * 跳转至系统编辑联系人界面(适配小米等定制机型) | ||||
| 	 * @param context 上下文(Activity/Service/Fragment) | ||||
| 	 * @param phoneNumber 待编辑联系人的电话号码(用于匹配已有联系人,必传) | ||||
| 	 * @param contactId 可选:已有联系人的ID(通过 ContactsContract 获取,传null则自动匹配号码) | ||||
| 	 */ | ||||
| 	public static void jumpToEditContact(Context context, String phoneNumber, Long contactId) { | ||||
| 		Intent intent = new Intent(Intent.ACTION_EDIT); | ||||
| 		// 关键:小米等机型需明确设置数据类型为“单个联系人”,避免参数丢失 | ||||
| 		intent.setType(ContactsContract.Contacts.CONTENT_ITEM_TYPE); | ||||
|  | ||||
| 		// 场景A:已知联系人ID(精准定位,优先用此方式,参数传递最稳定) | ||||
| 		if (contactId != null && contactId > 0) { | ||||
| 			// 构建联系人的Uri(格式:content://contacts/people/[contactId],系统标准格式) | ||||
| 			Uri contactUri = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, contactId); | ||||
| 			intent.setData(contactUri); | ||||
| 		}  | ||||
| 		// 场景B:未知ID,通过电话号码匹配(需系统支持号码匹配,小米机型兼容) | ||||
| 		else if (phoneNumber != null && !phoneNumber.isEmpty()) { | ||||
| 			// 方式1:小米等机型兼容的“通过号码定位联系人”参数(部分系统认此参数) | ||||
| 			//intent.putExtra(ContactsContract.Intents.Insert.PHONE_NUMBER, phoneNumber); | ||||
| 			// 方式2:补充系统标准的“数据Uri”,强化匹配(避免参数被定制系统忽略) | ||||
| 			Uri phoneUri = Uri.withAppendedPath(ContactsContract.CommonDataKinds.Phone.CONTENT_FILTER_URI, Uri.encode(phoneNumber)); | ||||
| 			intent.setData(phoneUri); | ||||
| 		} else { | ||||
| 			LogUtils.d(TAG, "编辑联系人失败:电话号码和联系人ID均为空"); | ||||
| 			return; | ||||
| 		} | ||||
|  | ||||
| 		// 可选:预填最新号码(覆盖原有号码,若用户修改了号码,编辑时自动更新) | ||||
| 		if (phoneNumber != null && !phoneNumber.isEmpty()) { | ||||
| 			intent.putExtra(ContactsContract.CommonDataKinds.Phone.NUMBER, phoneNumber); | ||||
| 			intent.putExtra(ContactsContract.CommonDataKinds.Phone.TYPE, ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE); | ||||
| 		} | ||||
|  | ||||
| 		// 启动活动(加防护,避免无联系人应用崩溃) | ||||
| 		if (intent.resolveActivity(context.getPackageManager()) != null) { | ||||
| 			// 小米机型在Service/非Activity中调用,需加NEW_TASK标志,否则可能无法启动 | ||||
| 			intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); | ||||
| 			context.startActivity(intent); | ||||
| 		} else { | ||||
| 			LogUtils.d(TAG, "编辑联系人失败:设备未安装联系人应用"); | ||||
| 		} | ||||
| 	} | ||||
| 	 | ||||
| 	/** | ||||
| 	 * 通过电话号码查询联系人ID(适配小米机型,解决编辑时匹配不稳定问题) | ||||
| 	 * @param context 上下文 | ||||
| 	 * @param phoneNumber 待查询的电话号码 | ||||
| 	 * @return 联系人ID(无匹配时返回-1) | ||||
| 	 */ | ||||
| 	public static Long getContactIdByPhone(Context context, String phoneNumber) { | ||||
| 		if (phoneNumber == null || phoneNumber.isEmpty()) { | ||||
| 			return -1L; | ||||
| 		} | ||||
|  | ||||
| 		ContentResolver cr = context.getContentResolver(); | ||||
| 		// 1. 构建电话查询Uri(系统标准:通过号码过滤联系人数据) | ||||
| 		Uri queryUri = Uri.withAppendedPath(ContactsContract.CommonDataKinds.Phone.CONTENT_FILTER_URI, Uri.encode(phoneNumber)); | ||||
| 		// 2. 只查询“联系人ID”字段(高效,避免冗余数据) | ||||
| 		String[] projection = {ContactsContract.CommonDataKinds.Phone.CONTACT_ID}; | ||||
| 		Cursor cursor = null; | ||||
|  | ||||
| 		try { | ||||
| 			cursor = cr.query(queryUri, projection, null, null, null); | ||||
| 			if (cursor != null && cursor.moveToFirst()) { | ||||
| 				// 3. 读取联系人ID(返回Long类型,避免int溢出) | ||||
| 				return cursor.getLong(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.CONTACT_ID)); | ||||
| 			} | ||||
| 		} catch (Exception e) { | ||||
| 			LogUtils.d(TAG, "查询联系人ID失败。" + e); | ||||
| 		} finally { | ||||
| 			if (cursor != null) { | ||||
| 				cursor.close(); // 关闭游标,避免内存泄漏 | ||||
| 			} | ||||
| 		} | ||||
| 		return -1L; // 无匹配联系人 | ||||
| 	} | ||||
| 	 | ||||
| } | ||||
|   | ||||
| @@ -5,5 +5,8 @@ | ||||
|     <item | ||||
|         android:id="@+id/item_calllog_phonenumber_copy" | ||||
|         android:title="Copy"/> | ||||
|     <item | ||||
|         android:id="@+id/item_calllog_phonenumber_add_contact" | ||||
|         android:title="Add Contact"/> | ||||
|      | ||||
| </menu> | ||||
|   | ||||
| @@ -5,5 +5,8 @@ | ||||
|     <item | ||||
|         android:id="@+id/item_contact_phonenumber_copy" | ||||
|         android:title="Copy"/> | ||||
|     <item | ||||
|         android:id="@+id/item_calllog_phonenumber_edit_contact" | ||||
|         android:title="Edit Contact"/> | ||||
|      | ||||
| </menu> | ||||
|   | ||||
		Reference in New Issue
	
	Block a user
	 ZhanGSKen
					ZhanGSKen