android添加联系人(直接添加到联系人数据库)
来源:互联网 发布:nginx 配置多端口监听 编辑:程序博客网 时间:2024/04/28 00:13
- 添加联系人姓名、手机、头像。。。
- java核心代码
- package com.kaka.addContact;
- import java.io.ByteArrayOutputStream;
- import android.app.Activity;
- import android.content.ContentUris;
- import android.content.ContentValues;
- import android.graphics.Bitmap;
- import android.graphics.BitmapFactory;
- import android.net.Uri;
- import android.os.Bundle;
- import android.provider.ContactsContract;
- import android.provider.ContactsContract.CommonDataKinds.Email;
- import android.provider.ContactsContract.CommonDataKinds.Im;
- import android.provider.ContactsContract.CommonDataKinds.Phone;
- import android.provider.ContactsContract.CommonDataKinds.Photo;
- import android.provider.ContactsContract.CommonDataKinds.StructuredName;
- import android.provider.ContactsContract.Contacts.Data;
- import android.provider.ContactsContract.RawContacts;
- public class Main extends Activity {
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- boolean result = insert("kaka", "110", "111@qq.com",
- "111");
- System.out.println("insert result " + result);
- }
- public boolean insert(String given_name, String mobile_number,
- String work_email, String im_qq) {
- try {
- ContentValues values = new ContentValues();
- // 下面的操作会根据RawContacts表中已有的rawContactId使用情况自动生成新联系人的rawContactId
- Uri rawContactUri = getContentResolver().insert(
- RawContacts.CONTENT_URI, values);
- long rawContactId = ContentUris.parseId(rawContactUri);
- // 向data表插入姓名数据
- if (given_name != "") {
- values.clear();
- values.put(Data.RAW_CONTACT_ID, rawContactId);
- values.put(Data.MIMETYPE, StructuredName.CONTENT_ITEM_TYPE);
- values.put(StructuredName.GIVEN_NAME, given_name);
- getContentResolver().insert(ContactsContract.Data.CONTENT_URI,
- values);
- }
- // 向data表插入电话数据
- if (mobile_number != "") {
- values.clear();
- values.put(Data.RAW_CONTACT_ID, rawContactId);
- values.put(Data.MIMETYPE, Phone.CONTENT_ITEM_TYPE);
- values.put(Phone.NUMBER, mobile_number);
- values.put(Phone.TYPE, Phone.TYPE_MOBILE);
- getContentResolver().insert(ContactsContract.Data.CONTENT_URI,
- values);
- }
- // 向data表插入Email数据
- if (work_email != "") {
- values.clear();
- values.put(Data.RAW_CONTACT_ID, rawContactId);
- values.put(Data.MIMETYPE, Email.CONTENT_ITEM_TYPE);
- values.put(Email.DATA, work_email);
- values.put(Email.TYPE, Email.TYPE_WORK);
- getContentResolver().insert(ContactsContract.Data.CONTENT_URI,
- values);
- }
- // 向data表插入QQ数据
- if (im_qq != "") {
- values.clear();
- values.put(Data.RAW_CONTACT_ID, rawContactId);
- values.put(Data.MIMETYPE, Im.CONTENT_ITEM_TYPE);
- values.put(Im.DATA, im_qq);
- values.put(Im.PROTOCOL, Im.PROTOCOL_QQ);
- getContentResolver().insert(ContactsContract.Data.CONTENT_URI,
- values);
- }
- // 向data表插入头像数据
- Bitmap sourceBitmap = BitmapFactory.decodeResource(getResources(),
- R.drawable.icon);
- final ByteArrayOutputStream os = new ByteArrayOutputStream();
- // 将Bitmap压缩成PNG编码,质量为100%存储
- sourceBitmap.compress(Bitmap.CompressFormat.PNG, 100, os);
- byte[] avatar = os.toByteArray();
- values.put(Data.RAW_CONTACT_ID, rawContactId);
- values.put(Data.MIMETYPE, Photo.CONTENT_ITEM_TYPE);
- values.put(Photo.PHOTO, avatar);
- getContentResolver().insert(ContactsContract.Data.CONTENT_URI,
- values);
- }
- catch (Exception e) {
- return false;
- }
- return true;
- }
- }
- Manifest中添加读写联系人数据权限
- <?xml version="1.0" encoding="utf-8"?>
- <manifest xmlns:android="http://schemas.android.com/apk/res/android"
- package="com.kaka.addContact"
- android:versionCode="1"
- android:versionName="1.0">
- <uses-sdk android:minSdkVersion="8" />
- <application android:icon="@drawable/icon" android:label="@string/app_name">
- <activity android:name=".Main"
- android:label="@string/app_name">
- <intent-filter>
- <action android:name="android.intent.action.MAIN" />
- <category android:name="android.intent.category.LAUNCHER" />
- </intent-filter>
- </activity>
- </application>
- <uses-permission android:name="android.permission.READ_CONTACTS" />
- <uses-permission android:name="android.permission.WRITE_CONTACTS" />
- </manifest>
0 0
- android添加联系人(直接添加到联系人数据库)
- Android批量添加联系人到通讯录
- Android实现批量添加联系人到通讯录
- android,添加联系人
- android 添加联系人分组
- android通讯录 添加联系人
- Android-通讯录:添加联系人
- android联系人获取,添加
- Android 批量添加联系人
- Android添加@联系人功能
- Android 使用ContentProvider 添加联系人
- Android NFC标签添加联系人
- Android批量添加删除联系人
- android批量添加联系人工具
- Android动态添加联系人列表
- Android 自定义添加,查询联系人
- Android添加数据到系统自带的联系人界面
- android通过Intent跳转到添加联系人界面的小结
- UI复习练习_优酷布局
- web3.0使用注解定义Servlet
- 第十三周项目一数组大折腾(1)
- jdk7 新特性-Java语言的动态性-脚本语言支持api-脚本的编译04
- 第十三周 OJ平台-在有序数组中插入数据
- android添加联系人(直接添加到联系人数据库)
- tools:context="${packageName}.${activityClass}"的作用
- C++之 typedef void *HANDLE
- 贝叶斯网络简介
- 查看Linux启动的各种服务
- TreeMap和TreeSet
- Tyvj P1108(守望者的逃离-贪心)
- 数组大折腾(4)
- Python函数参数到底是按值还是按引用