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

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

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部分:

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">

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="获取手机联系人"

android:onClick="getContacts"

/>

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

AndroidManifest.xml部分:

android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round"

android:supportsRtl="true" android:theme="@style/AppTheme">

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值