Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android 利用ContentProvider獲取聯系人信息

Android 利用ContentProvider獲取聯系人信息

編輯:關於Android編程

Android  利用ContentProvider獲取聯系人信息。在寫代碼前我們首先看一下運行的效果
運行效果如下:
這裡寫圖片描述

點了獲取聯系人就展示如下效果

這裡寫圖片描述

讀取聯系人信息的例子(MainActivity)

package com.example.administrator.myapplication;

import android.content.ContentResolver;
import android.database.Cursor;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ListView;
import android.widget.SimpleAdapter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * 獲取聯系人
 */
public class MainActivity extends AppCompatActivity {

    private ContentResolver cr;
    private List> data;
    private ListView lv_main_list;
    private SimpleAdapter simpleAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        lv_main_list = (ListView)findViewById(R.id.lv_main_list);

        //獲取內容訪問者
        cr = getContentResolver();

        data = new ArrayList>();
        //適配器
        simpleAdapter = new SimpleAdapter(this,data,android.R.layout.simple_list_item_2,new String[]{"names","phones"},new int[]{android.R.id.text1,android.R.id.text2});
        lv_main_list.setAdapter(simpleAdapter);
    }

    public void getContacts(View view){
        Cursor cursor=cr.query(Uri.parse("content://com.android.contacts/raw_contacts"),null,null,null,null);
        while(cursor.moveToNext()){
            Map map=new HashMap();
            int id=cursor.getInt(cursor.getColumnIndex("_id"));
            String displayName=cursor.getString(cursor.getColumnIndex("display_name"));
            Log.i("test",id+" "+displayName);
            map.put("names",displayName);

            //根據聯系人獲取聯系人數據
            Cursor cursor2=cr.query(Uri.parse("content://com.android.contacts/raw_contacts/"+id+"/data"),null,null,null,null);
            while(cursor2.moveToNext()){
              //  int type=cursor2.getInt(cursor2.getColumnIndex("mimetype_id"));
                String type=cursor2.getString(cursor2.getColumnIndex("mimetype"));
                String data1=null;
                if ("vnd.android.cursor.item/phone_v2".equals(type)){
                    data1 = cursor2.getString(cursor2.getColumnIndex("data1"));
                    Log.i("test","   "+type+" "+data1);
                    map.put("phones",data1);
                }
            }
            data.add(map);
        }
        //通知適配器發生改變
        simpleAdapter.notifyDataSetChanged();
    }
}

布局文件(activity_main.xml)
 

<!--?xml version="1.0" encoding="utf-8"?-->
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingbottom="@dimen/activity_vertical_margin" android:paddingleft="@dimen/activity_horizontal_margin" android:paddingright="@dimen/activity_horizontal_margin" android:paddingtop="@dimen/activity_vertical_margin" android:orientation="vertical" tools:context="com.example.administrator.myapplication.MainActivity">
 
    <button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="獲取所有的聯系人" android:onclick="getContacts">
    <listview android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/lv_main_list"></listview>
</button></linearlayout>

需要在清單文件上配置聯系人的權限(AndroidManifest.xml)

   
  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved