Android ContentProvider實(shí)現(xiàn)獲取手機(jī)聯(lián)系人功能
在之前項(xiàng)目中有用到關(guān)于獲取手機(jī)聯(lián)系人的部分,閑置就想和大家分享一下,話不多說(shuō),上代碼:
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)容訪問(wèn)者
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自定義WaveProgressView實(shí)現(xiàn)水波紋加載需求
這篇文章主要為大家詳細(xì)介紹了Android自定義WaveProgressView實(shí)現(xiàn)水波紋加載需求,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-09-09
Android webveiw 出現(xiàn)棧錯(cuò)誤解決辦法
這篇文章主要介紹了Android webveiw 出現(xiàn)棧錯(cuò)誤解決辦法的相關(guān)資料,出現(xiàn)java.lang.UnsupportedOperationException: For security reasons, WebView is not allowed in privileged processes,這里提供解決辦法,需要的朋友可以參考下2017-08-08
Android串口開發(fā)之使用JNI實(shí)現(xiàn)ANDROID和串口通信詳解
這篇文章主要給大家介紹了關(guān)于Android串口開發(fā)之使用JNI實(shí)現(xiàn)ANDROID和串口通信的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。2018-01-01
Android自定義實(shí)現(xiàn)一個(gè)省份簡(jiǎn)稱鍵盤
這篇文章主要為大家詳細(xì)介紹了Android如何自定義實(shí)現(xiàn)一個(gè)省份簡(jiǎn)稱鍵盤,可以用在車牌輸入等地方,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2023-06-06

