Android学习--ContentProvider之读取系统联系人

       ContentProvider的用法有2种,一种是使用现有的内容提供器来读取和操作相应程序中的数据,另一种是创建自己的内容提供器给我们程序的数据提供外部访问接口。

1、使用ContentResolver来增删改查

查询

Uri uri = Uri.parse("content://com.example.app.provider/table1");
Cursor cursor = getContentResolver().query(
                    uri,
                    projection,
                    selection,
                    selectionArgs,
                    sortOrder);
            if (null != cursor){
                while (cursor.moveToNext()){
                    String column1 = cursor.getString(cursor.getColumnIndex("column1");
                    String column2 = cursor.getString(cursor.getColumnIndex("column2");
                }
                cursor.close();
            }

添加

ContentValues values = new ContentValues();
values.put("column1", "text");
values.put("column2", 1);
getContentResolver().insert(uri,values);

更新

ContentValues values = new ContentValues();
values.put("column1", "");
getContentResolver().update(uri, values, "column1=? and column2=?", new String[]{"text", "1"});

删除

getContentResolver().delete(uri,  "column2 = ?", new String[]{ "1"});

 

2、读取系统联系人

布局文件activity_main.xml




    
    

主MainActivity

package com.example.contactstest;

import android.Manifest;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.provider.ContactsContract;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    List contactList;
    ArrayAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        contactList = new ArrayList<>();
        ListView listView = (ListView) findViewById(R.id.list_view);
        adapter = new ArrayAdapter(MainActivity.this,
                android.R.layout.simple_list_item_1, contactList);
        listView.setAdapter(adapter);

        if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.READ_CALL_LOG) !=
                PackageManager.PERMISSION_GRANTED){
            ActivityCompat.requestPermissions(this,
                    new String[]{Manifest.permission.READ_CONTACTS}, 1);
        }else {
            readContacts();
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        switch (requestCode){
            case 1:
                if (grantResults.length > 0 && grantResults[0]== PackageManager.PERMISSION_GRANTED){
                   readContacts();
                }else {
                    Toast.makeText(this, "Permission Denied", Toast.LENGTH_SHORT).show();
                }
                break;
            default:
        }
    }

    private void readContacts(){
        Cursor cursor = null;
        try {
            cursor = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                    null, null, null, null);
            if (null != cursor){
                while (cursor.moveToNext()){
                    String name = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
                    String number = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                    contactList.add(name+"\n"+number);
                }
            }
            adapter.notifyDataSetChanged();
        }catch (Exception e){
            e.printStackTrace();
        }
        finally {
            if (null != cursor){
                cursor.close();
            }
        }

    }
}

AndroidManifest.xml中添加权限

manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.contactstest">

    

在模拟器中添加两个联系人,然后运行程序即可

3、运行效果图

Android学习--ContentProvider之读取系统联系人_第1张图片

 

你可能感兴趣的:(Android学习)