本文实例讲述了Android使用ContentResolver搜索手机通讯录的方法。分享给大家供大家参考,具体如下:
在这个程序中使用ContentResolver来访问通讯录里联系人的关键字,并将所有找到的联系人存入CursorAdapter里。输入搜索人员名字a ,会将所有以a开头的名字都显示出来,输入*,则所有通讯录中的人名显示于AutoCompleteView的AdapterView里,若发生了User选择事件后,会将勾选的联系人电话号码显示于TextView里。
此程序中用到了ContentResolver .query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder)来取出通讯录里的所有联系人;其中将selection及selectionArgs传入null代表将所有联系人找出来。用Cursor 的getString(column index)的方式来取得存储内容,其中column index从0开始,这一点与java.sql.ResultSet不同,因为它是从1开始的。用AutoCompleteTextView.OnItemClickListener事件,这也是当用户单击联系人之后所拦截的事件处理,在其中便以ContactsAdapter.getCursor()方法取得联系人的电话号码。
程序如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
|
import android.app.Activity;
import android.content.ContentResolver;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.Contacts;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AutoCompleteTextView;
import android.widget.TextView;
@SuppressWarnings ( "deprecation" )
public class A07Activity extends Activity {
private AutoCompleteTextView actv;
private TextView tv;
private Cursor c01;
private ContactsAdapter ca;
//找出通讯录中的字段
public static String[] people={
Contacts.People._ID,
Contacts.People.PRIMARY_PHONE_ID,
Contacts.People.TYPE,
Contacts.People.NUMBER,
Contacts.People.LABEL,
Contacts.People.NAME
};
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super .onCreate(savedInstanceState);
setContentView(R.layout.main);
actv=(AutoCompleteTextView)findViewById(R.id.actv);
tv=(TextView)findViewById(R.id.tv);
ContentResolver cr=getContentResolver();
c01=cr.query(Contacts.People.CONTENT_URI, people, null , null , Contacts.People.DEFAULT_SORT_ORDER);
ca= new ContactsAdapter( this ,c01);
actv.setAdapter(ca);
actv.setOnItemClickListener( new OnItemClickListener(){
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
Cursor c02=ca.getCursor();
c02.moveToPosition(arg2);
String number=c02.getString(c02.getColumnIndexOrThrow(Contacts.People.NUMBER));
number=number== null ? "无电话输入" : number;
tv.setText(c02.getString(c02.getColumnIndexOrThrow(Contacts.People.NAME))+ "的电话是:" +number);
}
});
}
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
|
import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.provider.Contacts;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CursorAdapter;
import android.widget.TextView;
@SuppressWarnings ( "deprecation" )
public class ContactsAdapter extends CursorAdapter{
ContentResolver cr;
public ContactsAdapter(Context context, Cursor c) {
super (context, c);
// TODO Auto-generated constructor stub
cr=context.getContentResolver();
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
// TODO Auto-generated method stub
((TextView)view).setText(cursor.getString(cursor.getColumnIndexOrThrow(Contacts.People.NAME)));
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
// TODO Auto-generated method stub
final LayoutInflater li=LayoutInflater.from(context);
final TextView tv=(TextView)li.inflate(android.R.layout.simple_dropdown_item_1line, parent, false );
tv.setText(cursor.getString(cursor.getColumnIndexOrThrow(Contacts.People.NAME)));
return tv;
}
public String converToString(Cursor c){
return c.getString(c.getColumnIndexOrThrow(Contacts.People.NAME));
}
@SuppressWarnings ( "null" )
public Cursor runQueryOnBackgroundThread(CharSequence cs){
if (getFilterQueryProvider()!= null ){
getFilterQueryProvider().runQuery(cs);
}
StringBuilder sb= null ;
String[] s= null ;
if (cs== null ){
sb= new StringBuilder();
sb.append( "UPPER(" );
sb.append(Contacts.ContactMethods.NAME);
sb.append( ")GLOB?" );
s= new String[]{cs.toString().toUpperCase()+ "*" };
}
return cr.query(
Contacts.People.CONTENT_URI,
A07Activity.people,
sb== null ? null :sb.toString(),
s,
Contacts.People.DEFAULT_SORT_ORDER
);
}
}
|
AndroidManifest.xml如下所示:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
<? xml version = "1.0" encoding = "utf-8" ?>
< manifest xmlns:android = "http://schemas.android.com/apk/res/android"
package = "com.my.a07"
android:versionCode = "1"
android:versionName = "1.0" >
< uses-sdk android:minSdkVersion = "10" />
< application
android:icon = "@drawable/ic_launcher"
android:label = "@string/app_name" >
< activity
android:name = ".A07Activity"
android:label = "@string/app_name" >
< intent-filter >
< action android:name = "android.intent.action.MAIN" />
< category android:name = "android.intent.category.LAUNCHER" />
</ intent-filter >
</ activity >
</ application >
< uses-permission android:name = "android.permission.READ_CONTACTS" ></ uses-permission >
</ manifest >
|
通过ContentResolver还可以添加、修改、删除通讯录中的信息;并且通过它还可以访问audio、video、images等数据。相应方法展示如下:
添加:public final Uri insert(Uri uri,ContentValues values),ContentValue.put(key,value) ,其中key为字段名称,value为添加的数据。
修改:public final int update(Uri uri,ContentValues values,String where ,String[] selectionArgs),其中where为sql where 后面的条件字符串。selectionArgs为where里的数据。
删除:public final int delete(Uri uri,String where,String[] selectionArgs).
下面再使用ContentValue.put()方法。通过程序添加通讯录里的联系人的资料。
希望本文所述对大家Android程序设计有所帮助。