AndroidContentProvider实现获取手机联系人功能-创新互联

在之前项目中有用到关于获取手机联系人的部分,闲置就想和大家分享一下,话不多说,上代码:

成都创新互联公司主营北戴河网站建设的网络公司,主营网站建设方案,重庆APP软件开发,北戴河h5成都微信小程序搭建,北戴河网站营销推广欢迎北戴河等地区企业咨询

java部分:


package com.example.content; 
 
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; 
 
public class MainActivity extends AppCompatActivity { 
 
 private ContentResolver cr; 
 
 @Override 
 protected void onCreate(Bundle savedInstanceState) { 
  super.onCreate(savedInstanceState); 
  setContentView(R.layout.activity_main); 
  //获取内容访问者 
  cr = getContentResolver(); 
 } 
 public void getContacts(View view){ 
  Uri uri=Uri.parse("content://com.android.contacts/raw_contacts"); 
  Cursor cursor=cr.query(uri,null,null,null,null); 
  while(cursor.moveToNext()){ 
   int _id=cursor.getInt(cursor.getColumnIndex("_id")); 
   String display_name=cursor.getString(cursor.getColumnIndex("display_name")); 
   Log.i("test",_id+" "+display_name); 
   Uri uriData=Uri.parse("content://com.android.contacts/raw_contacts/"+_id+"/data"); 
   Cursor cursorData=cr.query(uriData,null,null,null,null); 
   while(cursorData.moveToNext()){ 
    String mimetype=cursorData.getString(cursorData.getColumnIndex("mimetype")); 
    String data1=cursorData.getString(cursorData.getColumnIndex("data1")); 
    if("vnd.android.cursor.item/phone_v2".equals(mimetype)){ 
     Log.i("test","  "+mimetype+" "+data1); 
    } 
   } 
  } 
 } 
} 

文章名称:AndroidContentProvider实现获取手机联系人功能-创新互联
转载来于:http://cdiso.cn/article/cdddjs.html

其他资讯