Android ContentProvider實(shí)現(xiàn)獲取手機(jī)聯(lián)系人功能
在之前項(xiàng)目中有用到關(guān)于獲取手機(jī)聯(lián)系人的部分,閑置就想和大家分享一下,話不多說,上代碼:
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); //獲取內(nèi)容訪問者 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="獲取手機(jī)聯(lián)系人" android:onClick="getContacts" /> </LinearLayout>
在需要獲取系統(tǒng)的東西的時(shí)候一定不要忘記給權(quán)限啊
AndroidManifest.xml部分:
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.content"> <!--獲取手機(jī)的聯(lián)系人--> <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>
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
android使用AsyncTask實(shí)現(xiàn)多線程下載實(shí)例
這篇文章主要介紹了android使用AsyncTask實(shí)現(xiàn)多線程下載實(shí)例,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-02-02Android編程實(shí)現(xiàn)修改標(biāo)題欄位置使其居中的方法
這篇文章主要介紹了Android編程實(shí)現(xiàn)修改標(biāo)題欄位置使其居中的方法,涉及Android布局設(shè)置的簡(jiǎn)單實(shí)現(xiàn)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-11-11Android實(shí)戰(zhàn)打飛機(jī)游戲之無限循環(huán)的背景圖(2)
這篇文章主要為大家詳細(xì)介紹了Android實(shí)戰(zhàn)打飛機(jī)游戲之無限循環(huán)的背景圖,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-07-07RxJava 1升級(jí)到RxJava 2過程中踩過的一些“坑”
RxJava2相比RxJava1,它的改動(dòng)還是很大的,那么下面這篇文章主要給大家總結(jié)了在RxJava 1升級(jí)到RxJava 2過程中踩過的一些“坑”,文中介紹的非常詳細(xì),對(duì)大家具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下來要一起看看吧。2017-05-05Android Studio中生成aar文件及本地方式使用aar文件的方法
這篇文章給大家講解Android Studio中生成aar文件以及本地方式使用aar文件的方法,也就是說 *.jar 與 *.aar 的生成與*.aar導(dǎo)入項(xiàng)目方法,本文給大家介紹的非常詳細(xì),需要的朋友參考下吧2017-12-12