效果:
[img]http://dl.iteye.com/upload/attachment/392131/1c690d35-2d1e-31f9-a674-0ff9a15a147a.jpg[/img]
[img]http://dl.iteye.com/upload/attachment/392133/f6c4b21f-711c-32fa-af1e-9faaa1c775e9.jpg[/img]
[img]http://dl.iteye.com/upload/attachment/392135/93114494-03fb-3459-b6c6-0ce037025ee0.jpg[/img]
main.xml
AndroidManifest.xml
使用Content Provider
查询所有通讯录数据:
content://contacts/people
查询通讯录的特定联系人ID:10
content://contacts/people/10
修改Content provider里的数据
ContentResolver.update();
添加一项数据进入 Content Provider
ContentResolvert.insert();
将数据存储至Content Provider:
ContentResolver().openOutputStream();
自Provider以删除一笔数据:
ContentResolver.delete();
[img]http://dl.iteye.com/upload/attachment/392131/1c690d35-2d1e-31f9-a674-0ff9a15a147a.jpg[/img]
[img]http://dl.iteye.com/upload/attachment/392133/f6c4b21f-711c-32fa-af1e-9faaa1c775e9.jpg[/img]
[img]http://dl.iteye.com/upload/attachment/392135/93114494-03fb-3459-b6c6-0ce037025ee0.jpg[/img]
main.xml
<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout
android:id="@+id/widget0"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
>
<EditText
android:id="@+id/name"
android:layout_width="92px"
android:layout_height="wrap_content"
android:textSize="18sp"
android:layout_x="24px"
android:layout_y="35px"
>
</EditText>
<EditText
android:id="@+id/number"
android:layout_width="268px"
android:layout_height="wrap_content"
android:textSize="18sp"
android:layout_x="23px"
android:layout_y="87px"
>
</EditText>
<Button
android:id="@+id/sreach"
android:layout_width="57px"
android:layout_height="wrap_content"
android:text="搜索"
android:layout_x="26px"
android:layout_y="142px"
>
</Button>
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="21px"
android:layout_y="202px"
>
</TextView>
</AbsoluteLayout>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="provider.test"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".ProviderTest"
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>
package provider.test;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.provider.ContactsContract.PhoneLookup;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class ProviderTest extends Activity {
private EditText name;
private EditText number;
private Button search;
private TextView text;
private static final int PICK_CONTACT_SUNACTIVITY=2;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/** 载入Main.xml*/
setContentView(R.layout.main);
/** 通过id找到组件*/
name=(EditText)findViewById(R.id.name);
number=(EditText)findViewById(R.id.number);
text=(TextView)findViewById(R.id.text);
search=(Button)findViewById(R.id.sreach);
/** 设置button按钮点击事件*/
search.setOnClickListener(new Button.OnClickListener(){
public void onClick(View v){
/** 构建uri来取的联系人数据位置*/
Uri uri=Uri.parse("content://contacts/people");
/** 通过intent来取的联系人数据返回的所选的值*/
Intent intent=new Intent(Intent.ACTION_PICK,uri);
/** 打开新的Activity并期望Activity返回值*/
startActivityForResult(intent, PICK_CONTACT_SUNACTIVITY);
}
});
}
protected void onActivityResult(int requestCode,int resultCode,Intent data){
switch(requestCode){
case PICK_CONTACT_SUNACTIVITY:
Cursor c=null;
Cursor phone =null;
/** 获得uri对象*/
final Uri uriRet=data.getData();
if(uriRet!=null){
try {
/** 必须要有android.permission.READ_CONTACTS权限*/
c=managedQuery(uriRet, null, null, null, null);
/**将Currsor一道数据最前端*/
c.moveToFirst();
/**获得联系人名字*/
String strname=c.getString(c.getColumnIndexOrThrow(PhoneLookup.DISPLAY_NAME));
/**(如果你使用的是2.0或2.0以上的API那么获得联系人号码)
*
* 获取联系人的ID号,在SQLite中的数据库ID
* */
String contactId = c.getString(c.getColumnIndex(ContactsContract.Contacts._ID));
phone = managedQuery(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = "
+ contactId, null, null);
String strPhoneNumber=null;
phone.moveToFirst();
/**获得联系人号码*/
strPhoneNumber = phone.getString(phone.getColumnIndex (ContactsContract.CommonDataKinds.Phone.NUMBER));
/**设置两个EditText参数*/
name.setText(strname);
number.setText(strPhoneNumber);
} catch (Exception e) {
/**将错误信息显示出来*/
text.setText(e.toString());
e.printStackTrace();
}finally{
/**关闭对象*/
phone.close();
c.close();
}
}
break;
}
super.onActivityResult(requestCode, resultCode, data);
}
}
使用Content Provider
查询所有通讯录数据:
content://contacts/people
查询通讯录的特定联系人ID:10
content://contacts/people/10
修改Content provider里的数据
ContentResolver.update();
添加一项数据进入 Content Provider
ContentResolvert.insert();
将数据存储至Content Provider:
ContentResolver().openOutputStream();
自Provider以删除一笔数据:
ContentResolver.delete();