android從通訊錄中選擇聯絡人並讀取號碼
1.Manifest檔案中新增許可權配置:
<uses-permission android:name="android.permission.WRITE_CONTACTS"/>
<uses-permission android:name="android.permission.READ_CONTACTS"/>
2.宣告開啟聯絡人應用的Intent:
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setClassName("com.android.contacts","com.android.contacts.activities.ContactSelectionActivity" );
startActivityForResult(intent, 1);
3.onActivityResult方法中獲取號碼:
protected void onActivityResult(int requestCode,int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
String number = getPhoneNumber(data);
Log.d(TAG,number);
}
private String getPhoneNumber(Intent intent){
Cursor cursor = null ;
Cursor phone = null;
try{
String[] projections = {ContactsContract.Contacts._ID,ContactsContract.Contacts.HAS_PHONE_NUMBER};
cursor = getContentResolver().query(intent.getData(),projections, null, null, null);
if ((cursor == null) || (!cursor.moveToFirst())){
return null;
}
int _id = cursor.getColumnIndexOrThrow(ContactsContract.Contacts._ID);
String id = cursor.getString(_id);
int has_phone_number = cursor.getColumnIndexOrThrow(ContactsContract.Contacts.HAS_PHONE_NUMBER);
int hasPhoneNumber = cursor.getInt(has_phone_number);
String phoneNumber = null;
if(hasPhoneNumber>0){
phone = getContentResolver().query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + "="
+ id, null, null);
while(phone.moveToNext()){
int index = phone.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
String number = phone.getString(index);
phoneNumber = number;
}
}
return phoneNumber;
}catch(Exception e){
}finally{
if (cursor != null) cursor.close();
if(phone != null) phone.close();
}
return null;
}
相關推薦
android從通訊錄中選擇聯絡人並讀取號碼
1.Manifest檔案中新增許可權配置: <uses-permission android:name="android.permission.WRITE_CONTACTS"/> <uses-permission android:name
Android從相簿中選擇圖片並作為Activity的背景(主題)
實現邏輯: 首先獲取本地相簿圖片的絕對路徑URL,儲存; 通過圖片的絕對路徑轉成bitmap; 設背景,結束。 許可權: <uses-permission android:
Android從通訊錄中讀取系統聯絡人
第一步、先建立一個Contacts.java實體類,用於表示聯絡人。package com.t20.contacts.entity; import java.io.Serializable; public class Contacts implements Seriali
android 獲取通訊錄中同一聯絡人的多個號碼
public static void fetchContact(Context context) { List<ContactsBean> list = new ArrayList<>(); ContentResolver cr = context.getC
Android-從相簿中選擇圖片顯示出來
一、選擇圖片 定義Intent跳轉到特定相簿的Uri下挑選,然後將挑選結果返回給Activity 用到startActivityForResult Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Imag
如何向android手機通訊錄中新增聯絡人
直接在手機的通訊錄的資料庫中新增列表 相關程式碼如下 package com.example.test; import android.support.v7.app.ActionBarActivity; import android.os.Bundle; import
android---從相簿中選擇圖片
private ImageView picture; public static final int CHOOSE_PHOTO=2; picture=(ImageView)findViewById(R.id.imageView); run_Permissions();
Android從相簿中選擇圖片顯示出來
下面的兩篇部落格我是選擇其中的一部分使用的。大家可以自己試試。 第一篇:http://blog.csdn.net/jackyguo1992/article/details/26729107 一、選擇圖片 定義Intent跳轉到特定相簿的Uri下挑選,然後將挑選結果返
Android 從網頁中跳轉到APP,從微信開啟自己的app並開啟指定頁面
先說實現步驟再說原理: 使用步驟 一,首先要給你要開啟的應用中的activity設定過濾器(在清單檔案裡設定)以JumpActivity為例如下面的: <intent-filter> 中就是所需過濾器 <activity android:name=".
Android 從資料庫中快速搜尋匹配資料並新增監聽事件
如何從資料庫中搜索與我們目標相符的資料呢? 我使用的是List view+cursoradapter。現在應該很少有人使用list view了吧,原來打算換換recyclerview來寫的,但是recyclerview不支援cursor view啊。暫且先記著
Android呼叫系統相機拍照,從相簿中選擇照片,呼叫系統攝像機錄影
最近在Android Studio上折騰程式碼,昨天編譯器又很不給面子的報錯了: Error type 3 Error: Activity class {com.example.myapplication/com.example.myapplication
從resource中的raw資料夾中獲取檔案並讀取資料(資原始檔只能讀不能寫)
轉載:http://blog.sina.com.cn/s/blog_4d25c9870100qpax.html 一、 從resource中的raw資料夾中獲取檔案並讀取資料(資原始檔只能讀不能寫) String res = ""; try{ InputStre
Android傳送簡訊(簡訊傳送以及群發和從電話本選擇聯絡人)---簡訊管家2
分析下怎麼寫 首先,我們需要一個輸入框,可以手動的輸入手機號碼, 其次,很少有人願意手動輸入,那麼我們需要提供一個按鈕來給我們的使用者選擇自己電話本中的聯絡人(一次可以選擇多個即群發) 然後,我們需要一個簡訊編輯介面,可以編輯簡訊 最後兩個按鈕,一個傳送,點選後傳送訊息,一個
Android 從imageview中獲得bitmap的方法
blog bit win matrix image logs led bitmap raw 第一種: 使用setDrawingCacheEnabled()和getDrawingCache()這兩種方法,第一個是為了設置是否開啟緩存,第二個就可以直接獲得imageview中
ionic拍照上傳圖片與從檔案中選擇圖片
本文主要使用cordova實現拍照上傳,拍照上傳或從資料夾中選擇上傳圖片 流程:拍照或選擇圖片===>獲取本機路徑==>向伺服器上傳圖片,獲取伺服器上圖片路徑 一、環境準備 安裝 cordova-plugin-camera 外掛 &n
Pig指令碼從Hive中load資料並存入到Hbase中
1、我們先建一個Hive表test01: create table test01(name String, age int, phone String,province String, city String) ROW FORMAT DELIMITED FIELDS TERMINATED B
ionic呼叫照相機或者從相簿中選擇圖片進行上傳
開發App的時候會經常用到拍照上傳留作憑證,或者從相簿中選擇。 相關外掛: 相機:cordova-plugin-camera 2.4.1 相簿:cordova-plugin-image-picker 1.1.1 檔案:cordova-plugin-file 許可權:cordova-plugi
thymeleaf和freemarker從物件中獲取時間並格式化,同時非空判斷
thymeleaf,freemarker從物件中獲取時間並格式化,同時非空判斷 freemarker物件非空判斷:${(creditInformationDO.delFlag)!} freemarker從物件中獲取時間並格式化:${(creditInformationD
從網路中下載圖片並進行顯示
通過get請求方式獲取網路圖片----主要是方便以後查閱以及做一個筆記 任務要求: 1.新建一個佈局檔案,在佈局檔案中新增Button、ImageView兩個控制元件 2.新建一個GetPictThread實現Runnable介面 (1)定義一個handler用於
android從Context中得到Activity的名字
之前如果兩個Activity公用了一個Adapter時,習慣性的在Adapter的建構函式中加一個type的引數。但是前幾天寫應用的時候突然想到既然每個Adapter都需要Context,它又是個上下文,裡邊應該包含Activity的名字。於是斷了一下點還真的有