Android ContentProvider实现获取手机联系人功能

 更新时间:2017年07月20日 08:56:11   作者:谦之君子  
这篇文章主要为大家详细介绍了Android ContentProvider实现获取手机联系人功能,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

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

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); 
    } 
   } 
  } 
 } 
} 

xml部分:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
 xmlns:android="http://schemas.android.com/apk/res/android" 
 xmlns:app="http://schemas.android.com/apk/res-auto" 
 xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" 
 android:layout_height="match_parent" tools:context="com.example.content.MainActivity"> 
 
 <Button 
  android:layout_width="wrap_content" 
  android:layout_height="wrap_content" 
  android:text="获取手机联系人" 
  android:onClick="getContacts" 
  /> 
 
</LinearLayout> 

在需要获取系统的东西的时候一定不要忘记给权限啊

AndroidManifest.xml部分:

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.content"> 
 
 <!--获取手机的联系人--> 
 <uses-permission android:name="android.permission.READ_CONTACTS"></uses-permission> 
 
 <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" 
  android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" 
  android:supportsRtl="true" android:theme="@style/AppTheme"> 
  <activity android:name=".MainActivity"> 
   <intent-filter> 
    <action android:name="android.intent.action.MAIN" /> 
 
    <category android:name="android.intent.category.LAUNCHER" /> 
   </intent-filter> 
  </activity> 
 </application> 
 
</manifest> 

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • Android自定义WaveProgressView实现水波纹加载需求

    Android自定义WaveProgressView实现水波纹加载需求

    这篇文章主要为大家详细介绍了Android自定义WaveProgressView实现水波纹加载需求,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-09-09
  • Android录音播放管理工具

    Android录音播放管理工具

    这篇文章主要为大家详细介绍了Android录音播放管理工具,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-01-01
  • 详解Android 视频滚动列表(偷懒型)

    详解Android 视频滚动列表(偷懒型)

    小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧本篇文章主要介绍了Android 视频滚动列表(偷懒型),
    2017-11-11
  • Android webveiw 出现栈错误解决办法

    Android webveiw 出现栈错误解决办法

    这篇文章主要介绍了Android webveiw 出现栈错误解决办法的相关资料,出现java.lang.UnsupportedOperationException: For security reasons, WebView is not allowed in privileged processes,这里提供解决办法,需要的朋友可以参考下
    2017-08-08
  • Android手势左右滑动效果

    Android手势左右滑动效果

    这篇文章主要为大家详细介绍了Android手势左右滑动效果,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-11-11
  • Kotlin中空判断处理操作实例

    Kotlin中空判断处理操作实例

    最近使用kotlin重构项目,遇到了一个小问题,在Java中,可能会遇到判断某个对象是否为空,为空执行一段逻辑,不为空执行另外一段逻辑,下面这篇文章主要给大家介绍了关于Kotlin中空判断处理操作的相关资料,需要的朋友可以参考下
    2022-07-07
  • Android串口开发之使用JNI实现ANDROID和串口通信详解

    Android串口开发之使用JNI实现ANDROID和串口通信详解

    这篇文章主要给大家介绍了关于Android串口开发之使用JNI实现ANDROID和串口通信的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧。
    2018-01-01
  • Android整理好的图片压缩工具类

    Android整理好的图片压缩工具类

    今天小编就为大家分享一篇关于Android整理好的图片压缩工具类,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
    2018-12-12
  • 详解Glide4.0集成及使用注意事项

    详解Glide4.0集成及使用注意事项

    这篇文章主要介绍了详解Glide4.0集成及使用注意事项,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-08-08
  • Android自定义实现一个省份简称键盘

    Android自定义实现一个省份简称键盘

    这篇文章主要为大家详细介绍了Android如何自定义实现一个省份简称键盘,可以用在车牌输入等地方,感兴趣的小伙伴可以跟随小编一起学习一下
    2023-06-06

最新评论