Andrid 系统本身提供了大量的ContentProvider,如联系人信息,系统的多媒体信息等
系统内容提供者的使用
1. 通过getContext()获取Context对象,通过Content对象getContentResolver获取ContentResolver对象
2. 调用ContentResolver的insert(),updata(),delete(),query();
例子管理联系人:
权限
<!-- 读联系人权限 -->
<uses-permissionandroid:name="android.permission.READ_CONTACTS" />
<!-- 写联系人权限 -->
<uses-permissionandroid:name="android.permission.WRITE_CONTACTS" />
<!-- 拨号权限 -->
<uses-permissionandroid:name="android.permission.CALL_PHONE" />
<!-- 读短信权限 -->
<uses-permissionandroid:name="android.permission.READ_SMS" />
单项的布局
<?xml version="1.0"encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="80dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:orientation="vertical" >
<ExpandableListView
android:id="@+id/showmessage"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</ExpandableListView>
</LinearLayout>
Activity布局
<?xml version="1.0"encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ListView
android:id="@+id/showData"
android:layout_width="match_parent"
android:layout_height="match_parent"></ListView>
</LinearLayout>
Activity:
public classShowPhonePeople extends Activity{
private ListView showData;
privateArrayAdapter<String> adapter;
private List<String> listData;
privateList<List<String>> data;
private ExpandableListView listview;
@Override
protected void onCreate(BundlesavedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_showphonepeople);
showData=(ListView)findViewById(R.id.showData);
listData=newArrayList<String>();
data=newArrayList<List<String>>();
getdata();
adapter=newArrayAdapter<String>(this, android.R.layout.simple_expandable_list_item_1, listData);
showData.setAdapter(adapter);
final Viewview=getLayoutInflater().inflate(R.layout.layout_result, null, false);
listview=(ExpandableListView)view.findViewById(R.id.showmessage);
showData.setOnItemClickListener(newAdapterView.OnItemClickListener() {
@Override
public voidonItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
showItem(view);
}
});
}
public void getdata(){
ContentResolverresolver=getContentResolver();
Cursorcursor=resolver.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);//管理联系人的uri:ContactsContract.Contacts.CONTENT_URI
while(cursor.moveToNext()){
StringcontactId=cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));//获取联系人的id//根据索引获取数据
Stringname=cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));//获取联系人姓名
CursornumberCursor=resolver.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,ContactsContract.CommonDataKinds.Phone.CONTACT_ID+"=?", new String[]{contactId}, null);//根据id获取联系人电话号码
listData.add(name);
List<String>listNumbet=newArrayList<String>();
while(numberCursor.moveToNext()){
Stringnumber=numberCursor.getString(numberCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));//获取联系人电话
listNumbet.add("电话号码:"+number);
}
numberCursor.close();
data.add(listNumbet);
}
cursor.close();
}
public void showItem(View view){
ExpandableListAdapterlistAdapter=newBaseExpandableListAdapter() {
@Override
public boolean isChildSelectable(int arg0, int arg1) {
return true;
}
@Override
public boolean hasStableIds() {
return true;
}
@Override
public View getGroupView(int groupPosition, boolean arg1, View arg2,ViewGroup arg3) {
TextViewtextview=getTextView();
textview.setText(getGroup(groupPosition).toString());
return textview;
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
@Override
public int getGroupCount() {
return listData.size();
}
@Override
public Object getGroup(int arg0) {
return listData.get(arg0);
}
@Override
public int getChildrenCount(int arg0) {
return data.get(arg0).size();
}
//设置文本框的参数
private TextView getTextView(){
AbsListView.LayoutParamslp=newAbsListView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 64);
TextViewtextview=newTextView(ShowPhonePeople.this);
textview.setLayoutParams(lp);
textview.setGravity(Gravity.CENTER_VERTICAL|Gravity.LEFT);
textview.setPadding(36,0, 0, 0);
textview.setTextSize(20);
return textview;
}
//获取每个子View的布局
@Override
public View getChildView(int groupPosition, int childPosition, boolean arg2, View arg3,
ViewGrouparg4) {
TextViewtextview=getTextView();
textview.setText(data.get(groupPosition).get(childPosition));
return textview;
}
//获取子项的id,原适用于集合中嵌套集合,当前只有一个集合
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
//获取子项的子项的数据:该方法原是适用于连个集合,list<List>集合,当前只有一个集合
@Override
public Object getChild(int groupPosition, int childPosition) {
return data.get(groupPosition).get(childPosition);
}
};
listview.setAdapter(listAdapter);
new AlertDialog.Builder(this).setView(view).setPositiveButton("确定", null).show();
}
}