内容提供器--学习笔记(1)

来源:互联网 发布:2016网络歌曲打包下载 编辑:程序博客网 时间:2024/06/08 18:22

一、内容提供器简介
主要用于在不同之间实现数据共享的功能,它提供了一套完整的机制,允许一个程序访问另一个程序中的数据,同时还能保证被访问数据的安全性。
用法一般有两种:一是使用现有的内容提供器来读取和操作相应程序中的数据;二是创建自己的内容提供器给我们程序的数据提供外部访问接口。

二、ContentResolver的基本用法
ContentResolver中的增删查改方法接收Uri参数(称内容Uri);它是给内容提供器中的数据建立了唯一的标识符,由两部分组成:权限和路径
权限:用于对不同的应用程序做区分,一般采用程序包名的方式来进行命名。
路径:用于对同一应用程序中不同的表做区分的,通常添加到权限后面。

例如:
content://com.example.app.provider/table1
content://com.example.app.provider/table2

得到内容Uri字符串后用Uri.Parse()解析成Uri对象才作为参数传入。
Uri uri=Uri.Parse(“content://com.example.app.provider/table1”)
(1)、查询:
Cursor cursor=getContentResolver().query(uri,projection,selection,selectionArgs,sortOrder);
(2)、读取:
if(cursor!=null){
while(cursor.moveToNext()){
String colum1=cursor.getString(cursor.getColumnIndex(“column1”));
colum2=cursor.getInt(cursor.getColumnIndex(“column2”));
}
cursor.close();
}
(3)、增加,修改,删除
增加:
ContentValues values=new ContentValues();
values.put(“column1”,”text”);
values.put(“column2”,1);
getContentResolver().insert(uri,values);

修改:
ContentValues values=new ContentValues();
values.put(“column1”,”“);//清空值
getContentResolver().update(uri,values,”column1=?and column2=?”,new Sttring[] {“text”,1} );

删除:
getContentResolver().update(uri,values,”column1=?”,new Sttring[] {“text”} );

最后附上一个读取手机联系人的demo:
1、布局:

<LinearLayout 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" android:paddingLeft="@dimen/activity_horizontal_margin"    android:paddingRight="@dimen/activity_horizontal_margin"    android:paddingTop="@dimen/activity_vertical_margin"    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"><ListView    android:id="@+id/contacts_view"    android:layout_width="match_parent"    android:layout_height="match_parent"></ListView></LinearLayout>

2、java代码:

package demo8.contactstest;import android.database.Cursor;import android.os.Bundle;import android.provider.ContactsContract;import android.support.v7.app.AppCompatActivity;import android.widget.ArrayAdapter;import android.widget.ListView;import java.util.ArrayList;import java.util.List;public class MainActivity extends AppCompatActivity {    ListView contactView;    ArrayAdapter<String> adapter;//适配器    List<String> contactsList=new ArrayList<String>();//数据源    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        contactView= (ListView) findViewById(R.id.contacts_view);        adapter=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,contactsList);        contactView.setAdapter(adapter);        readContacts();    }    private void readContacts() {        Cursor cursor=null;        try {            //查询联系人数据            cursor=getContentResolver().query(                    ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,null,null,null);            while (cursor.moveToNext()){                //获取联系人姓名                String displayName=cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));                //获取联系人手机号                String number=cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));                contactsList.add(displayName+"\n"+number);            }        }catch (Exception e){            e.printStackTrace();        }finally {            if(cursor!=null){                cursor.close();//关闭游标            }        }    }}

3、权限配置:

<uses-permission android:name="android.permission.READ_CONTACTS"/>
0 0
原创粉丝点击