Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android實現通訊錄效果——獲取手機號碼和姓名

Android實現通訊錄效果——獲取手機號碼和姓名

編輯:關於Android編程

首先給大家展示下運行效果圖:

由於通訊錄在手機裡是以數據庫貯存的 所以我們可以通過一個方法

context.getContentResolver().query(Phone.CONTENT_URI,
null, null, null, null);

來獲得通訊錄 ,這個方法返回一個游標的數據類型,通過moveToNext()方法來獲取所有的手機號碼信息
當然讀取手機通訊錄需要權限 在adnroidManifest文件中聲明即可

由於我也實現了打電話的功能 所以也要聲明權限

<uses-permission android:name="android.permission.READ_CONTACTS">
<uses-permission android:name="android.permission.CALL_PHONE"></uses-permission></uses-permission>

布局文件

activity_main.xml

<relativelayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent">
<listview android:id="@+id/listView1" android:layout_width="match_parent" android:layout_height="wrap_content">
</listview>
</relativelayout>

listview的布局文件:item.xml,在這裡我設置的頭像為默認的 當然也可以在手機數據庫中讀取聯系人的icon

<!--{cke_protected}{C}%3C!%2D%2D%3Fxml%20version%3D%221.0%22%20encoding%3D%22utf-8%22%3F%2D%2D%3E-->
<relativelayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent">
<imageview android:id="@+id/image" android:layout_width="60dp" android:layout_height="60dp" android:padding="10dp" android:src="@drawable/ic_launcher">
<textview android:id="@+id/name" android:paddingtop="10dp" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_torightof="@id/image" android:text="name">
<textview android:id="@+id/number" android:paddingtop="5dp" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_below="@id/name" android:layout_torightof="@id/image" android:text="number">
</textview></textview></imageview></relativelayout>

自己封裝一個聯系人信息的類 有兩個變量

package com.example.getphonenumber;
public class PhoneInfo {
private String name;
private String number;
public PhoneInfo(String name, String number) {
this.name = name;
this.number = number;
}
public String getName() {
return name;
}
public String getNumber() {
return number;
}
}

讀取手機數據庫中的通訊錄

GetPhoneNumberFromMobile.class

package com.example.getphonenumber;
import java.util.ArrayList;
import java.util.List;
import android.content.Context;
import android.database.Cursor;
import android.provider.ContactsContract.CommonDataKinds.Phone;
public class GetPhoneNumberFromMobile {
private List<phoneinfo> list;
public List<phoneinfo> getPhoneNumberFromMobile(Context context) {
// TODO Auto-generated constructor stub
list = new ArrayList<phoneinfo>();
Cursor cursor = context.getContentResolver().query(Phone.CONTENT_URI,
null, null, null, null);
//moveToNext方法返回的是一個boolean類型的數據 
while (cursor.moveToNext()) {
//讀取通訊錄的姓名
String name = cursor.getString(cursor
.getColumnIndex(Phone.DISPLAY_NAME));
//讀取通訊錄的號碼
String number = cursor.getString(cursor
.getColumnIndex(Phone.NUMBER));
PhoneInfo phoneInfo = new PhoneInfo(name, number);
list.add(phoneInfo);
}
return list;
}
}</phoneinfo></phoneinfo></phoneinfo>

自定義adapter

package com.example.getphonenumber;
import java.util.ArrayList;
import java.util.List;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
public class MyAdapter extends BaseAdapter{
private List<phoneinfo> list;
private Context context;
public MyAdapter(List<phoneinfo> list, Context context) {
this.list = list;
this.context = context;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return list.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return list.get(position);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
if(convertView==null){
ViewHolder viewHolder=new ViewHolder();
LayoutInflater inflater=LayoutInflater.from(context);
convertView=inflater.inflate(R.layout.item, null);
viewHolder.name=(TextView) convertView.findViewById(R.id.name);
viewHolder.number=(TextView) convertView.findViewById(R.id.number);
viewHolder.name.setText(list.get(position).getName());
viewHolder.number.setText(list.get(position).getNumber());
}
return convertView;
}
public class ViewHolder{
TextView name;
TextView number;
}
}</phoneinfo></phoneinfo>

MainActivity中listview加載適配器 並為其添加點擊監聽事件

package com.example.getphonenumber;
import java.util.ArrayList;
import java.util.List;
import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.Toast;
public class MainActivity extends Activity implements OnItemClickListener {
private ListView lv;
private MyAdapter adapter;
private GetPhoneNumberFromMobile getPhoneNumberFromMobile;
private List<phoneinfo> list = new ArrayList<phoneinfo>();
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lv = (ListView) findViewById(R.id.listView1);
getPhoneNumberFromMobile = new GetPhoneNumberFromMobile();
list = getPhoneNumberFromMobile.getPhoneNumberFromMobile(this);
adapter = new MyAdapter(list, this);
lv.setAdapter(adapter);
lv.setOnItemClickListener(this);
}
@Override
public void onItemClick(AdapterView<!--?--> parent, View view, int position,
long id) {
// TODO Auto-generated method stub
String number = list.get(position).getNumber();
Intent intent = new Intent();
intent.setAction("android.intent.action.CALL");
intent.addCategory(Intent.CATEGORY_DEFAULT);
intent.setData(Uri.parse("tel:"+number));
startActivity(intent);
}
}</phoneinfo></phoneinfo>

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved