SQLite数据库介绍

1.添加权限(写入和读取联系人);

<<span se-mark="1">uses-permission android:name="android.permission.WRITE_CONTACTS"/>

<<span se-mark="1">uses-permission android:name="android.permission.READ_CONTACTS"/>​

2.程序;

public class ContactsActivity extends Activity{

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        //设置主布局;

        setContentView(R.layout.activity_contacts);

    }



    public void addContactsClick(View view){

        ContentResolver cr = getContentResolver();

        //执行一个空值插入,目的是获取系统返回的rawContactId

        ContentValues values = new ContentValues();

        Uri uri = cr.insert(ContactsContract.RawContacts.CONTENT_URI,values);

        long  _id = ContentUris.parseId(uri);

        //插入姓名数据;

        values.clear();

        values.put(ContactsContract.Data.RAW_CONTACT_ID, _id);

        values.put(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE);

        values.put(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME, "Jack");

        cr.insert(ContactsContract.Data.CONTENT_URI, values);

        //插入电话数据;

        values.clear();

        values.put(ContactsContract.Data.RAW_CONTACT_ID, _id);

        values.put(ContactsContract.Data.MIMETYPE,ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE);

        values.put(ContactsContract.CommonDataKinds.Phone.NUMBER,"0099");

        values.put(ContactsContract.CommonDataKinds.Phone.TYPE,ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE);

        cr.insert(ContactsContract.Data.CONTENT_URI, values);

        Toast.makeText(this,"记录已添加",Toast.LENGTH_SHORT).show();

    }



    public void deleteContactsClick(View view){

        ContentResolver contentResolver = this.getContentResolver();

        Uri uri = Uri.parse("content://com.android.contacts/contacts/3");

        contentResolver.delete(uri,null,null);

        Toast.makeText(this,"记录已删除",Toast.LENGTH_SHORT).show();

    }



    public void updateContactsClick(View view){

        ContentResolver cr = getContentResolver();

        ContentValues values = new ContentValues();

        values.put(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE);

        values.put(ContactsContract.CommonDataKinds.Phone.NUMBER, "4455");

        values.put(ContactsContract.CommonDataKinds.Phone.TYPE, ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE);

        cr.update(ContactsContract.Data.CONTENT_URI, values, "_id=1", null);

        Toast.makeText(this,"_id=1的记录已更新",Toast.LENGTH_SHORT).show();

    }



    public void queryContactsClick(View view){

        //获得接口;

        ContentResolver contentResolver = this.getContentResolver();

        String id = null;

        String mimetype = null;

        //只需要从contacts中获取Id,其他的都可以不要,然后通过查看上面编译后的SQL语句可以看出将第二个参数设置为null,

        // 默认的返回列非常多,是一种资源浪费;

        //查询所有联系人的id;

        Cursor cursor =contentResolver.query(ContactsContract.Contacts.CONTENT_URI,

                new String[] {ContactsContract.Contacts._ID},null,null,null);

        //用循环取出所有id;

        while (cursor.moveToNext()){

            id = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));

            //根据id从一个Cursor中获取所有的信息;

            Cursor contactInfoCursor = contentResolver.query(ContactsContract.Data.CONTENT_URI,

                    new String[]{ContactsContract.Data.CONTACT_ID,

                            ContactsContract.Data.MIMETYPE,

                            ContactsContract.Data.DATA1, ContactsContract.Data.DATA15},

                    ContactsContract.Data.CONTACT_ID+"="+id,null,null);

            while (contactInfoCursor.moveToNext()){

                mimetype = contactInfoCursor.getString(contactInfoCursor.getColumnIndex(ContactsContract.Data.MIMETYPE));

                String value = contactInfoCursor.getString(contactInfoCursor.getColumnIndex(ContactsContract.Data.DATA1));

                byte[] photo = contactInfoCursor.getBlob(contactInfoCursor.getColumnIndex(ContactsContract.Data.DATA15));

                if (mimetype.contains("/name")){

                    System.out.println("姓名="+value);

                }else if (mimetype.contains("/im")){

                    System.out.println("QQ="+value);

                }else if (mimetype.contains("/email")){

                    System.out.println("邮箱="+value);

                }else if (mimetype.contains("/phone")){

                    System.out.println("电话="+value);

                }else if (mimetype.contains("/postal-address_v2")){

                    System.out.println("地址="+value);

                }else if (mimetype.contains("/photo")){

                    System.out.println("照片="+value);

                }else if (mimetype.contains("/group")){

                    System.out.println("组="+value);

                }

            }System.out.println("----------------------------------------------");

            contactInfoCursor.close();

        }cursor.close();

    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值