首页 > 其他分享 >如何保存联系人

如何保存联系人

时间:2022-12-09 17:00:33浏览次数:26  
标签:String 联系人 保存 cursor 如何 intent new Intent


 这两天项目有个需求,可以对事务操作人进行 【新建联系人】【保存到已有联系人】【编辑联系人(xxx)】.

 总结一下,日后备用。先看一下效果:

       

如何保存联系人_integer

                                                                        

如何保存联系人_null_02

  一、【新建联系人】主要是给系统发一个Intent

            

public void addContact() {
Intent intent = new Intent("android.intent.action.INSERT",
People.CONTENT_URI);
if (emailAdderss != null) { // 将要增加的联系人信息放到 exta data里
intent.putExtra(ContactsContract.Intents.Insert.EMAIL, emailAdderss);
intent.putExtra(ContactsContract.Intents.Insert.NAME, creatorName);
}
startActivity(intent);
setResult(RESULT_FIRST_USER);
finish(); }

二、【保存到已有联系人】.(备注:选联系人中之后,到编辑该联系人,程序手动需要继续调用系统功能)

     1、先调用系统的已有联系人列表

               Intent intent = new Intent("android.intent.action.PICK",
                  People.CONTENT_URI);
     2、设置resultCode             startActivityForResult(intent,Constants.PICK_CONTACT);

     3、编写响应 resultCode 代码,做出编辑响应。

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {switch (requestCode) {
case Constants.PICK_CONTACT:
if (resultCode == RESULT_OK) {
Uri contactData = data.getData();
String urlStr = contactData.getEncodedPath();
String[] idStrs = urlStr.split("/");
int id = Integer.parseInt(idStrs[2]); // 主要是获得 选中人的 id 然后 进行编辑

if (Build.VERSION.SDK_INT == 4 || Build.VERSION.RELEASE.equals("1.6")) { // 发送编辑联系人请求

// for sdk1.6
Intent intent_ = new Intent("android.intent.action.EDIT", Uri.parse("content://contacts/people/" + id));
intent_.putExtra(ContactsContract.Intents.Insert.EMAIL, emailAdderss);
startActivity(intent_); } else {
Intent intent_ = new Intent("android.intent.action.EDIT", Uri.parse("content://com.android.contacts/raw_contacts/" + id));
intent_.putExtra(ContactsContract.Intents.Insert.EMAIL, emailAdderss); startActivity(intent_);
}
finish();
}
break;
default:
super.onActivityResult(requestCode, resultCode, data);
break;
}

三:编辑以后联系人

      1、根据email获得联系人名称

        

public  String lookupContactNameByEmail(String email) {

String where=Email.DATA1+"=?";
Cursor cursor = getContentResolver().query(ContactsContract.CommonDataKinds.Email.CONTENT_URI, new String[]{ Email.CONTACT_ID}, where, new String[]{email}, null);
if(cursor.moveToNext()){
String contactWhere=Contacts._ID+"="+cursor.getInt(0);
cursor.close();
Cursor cursor2 = getContentResolver().query(Contacts.CONTENT_URI, new String[]{Contacts.DISPLAY_NAME}, contactWhere, null,null);
if(cursor2.moveToNext()){
String str = cursor2.getString(0);
return str;
}
}
return "unKnow";
}

      2、发送编辑Action

      

public void editContact() {
Integer id = null;
Cursor cursor = getContactsByEmail();
if(cursor.getCount()>0){
cursor.moveToFirst();
id = cursor.getInt(cursor.getColumnIndex(Data.RAW_CONTACT_ID));
DebugUtils.v(TAG, emailAdderss+"===================================================+++"+id );
cursor.close();
if (Build.VERSION.SDK_INT == 4 || Build.VERSION.RELEASE.equals("1.6")) {

// for sdk1.6
Intent intent_ = new Intent("android.intent.action.EDIT", Uri.parse("content://contacts/people/" + id)); startActivity(intent_);
} else {
Intent intent_ = new Intent("android.intent.action.EDIT", Uri.parse("content://com.android.contacts/raw_contacts/" + id));
startActivity(intent_);
}

finish();
}


}

标签:String,联系人,保存,cursor,如何,intent,new,Intent
From: https://blog.51cto.com/u_15907753/5926298

相关文章

  • 如何用charles修改响应数据
    应用charlse的修改响应数据,可以完成一些前端页面特殊场景展示和接口功能未完善时的测试工作,使用过程如下:1.选中需要打断点的接口右键点击选择断点,点击【代理】,打开断点设......
  • 教你如何用纯css代码实现太极阴阳鱼动画效果
    今天看到一个有意思的效果,闲来无事做一个:把2d静态的太极图改成了3d,阴极和阳极分到了两个平面里实现旋转效果,这个好实现,重点是实现它的透明效果,平面太极图显示出两极是用......
  • marquee如何让文字上下来回滚动
    首先让我们认识这个<marquee>标签,它是成对出现的标签,首标签<marquee>和尾标签</marquee>之间的内容就是滚动内容。<marquee>标签的属性主要有behavior、bgcolor、direction......
  • “云办公”如何用任务协同工具搞定项目和团队管理?
    远程参加会议、团队协同作业、项目负责人进行任务分配、团队成员多人协同编辑文件及同时推进项目、人力部门在线进行审批报销……,随着“云办公”的加速普及,人们只需一台电......
  • Qt:QCamera打开系统摄像头,截图保存实例
    ​​          Qt,C++学习交流群:302558294(欢迎你的加入)效果图:动态图太大,这里我就不弄动态图了。用到三个库:#include<QCamera>#include<QCameraImage......
  • Unity调用摄像头拍照及保存图片
    可能大家在使用Unity开发项目的时候,会碰到这样一个需求,就是去调用摄像头拍照或者保存图片,其中摄像头拍照方式也有多种,这里就给大家简单介绍下。usingSystem.Collections;......
  • vue3组合式api<script lang="ts" setup>中如何接收父组件props传值,以及子组件emit回调
    子组件children.vue首先要引入<scriptlang="ts"setup>import{defineProps,defineEmits}from"vue";constprops=defineProps<{id:string,option:any}>()c......
  • KodExplorer修改增加保存工作空间
    KodExplorer修改增加保存工作空间简述经常在线修改PHP代码,但是关了浏览器就没了历史打开的文件记录,因此,增加一个保存工作空间的功能。可道云是使用sea.js来进行控制JS......
  • Nginx 反向代理如何获取真实的ip
    问题出现的环境:本地(H)postMan 请求nginx(A服务器)--转到两台应用服务器(B,C);预期是应用服务器接口请求的时候,拿到的是本地的H的服务器,但是拿到的是A服务器的IP; 转:nginx......
  • 如何正确使用Airtest报告插件?报告小tips上线
    1.前言在使用Airtest做自动化测试时,默认生成的报告,其实是airtest的专属报告。它对于poco语句(控件测试场景)、airtest-selenium语句(web测试场景)的支持不够完善,因此我们需......