ContentProvider安卓的四大组件之一。因为安卓系统下,每个app的私有数据是相互独立,无法相互直接访问。所以就产生了ContentProvider内容提供者类,它可以暴露出一个接口,通过接口可以实现对其私有数据的操作(增、删、改、查)。ContentProvider需要到清单文件中注册。
下面是一个小例子,代码里面有注释:
MyProvider.java
package com.example.provider;
import android.content.ContentProvider;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
/**
* 内容提供者对象
* @author Lenovo
*
*/
public class MyProvider extends ContentProvider {
//得到匹配URI对象
private static UriMatcher matcher = new UriMatcher(UriMatcher.NO_MATCH);
private static final int INSERT = 1;
private static final int UPDATE = 2;
private static final int DELETE = 3;
private static final int QUERY = 4;
MySQLiteOpenHelper mHelper;
static{//初始化,增加匹配规则
matcher.addURI("com.example.provider.myPro", "insert", INSERT);
matcher.addURI("com.example.provider.myPro", "update", UPDATE);
matcher.addURI("com.example.provider.myPro", "delete", DELETE);
matcher.addURI("com.example.provider.myPro", "query", QUERY);
}
@Override
public boolean onCreate() {
// TODO Auto-generated method stub
//初始化创建数据库操作对象
mHelper = new MySQLiteOpenHelper(getContext());
System.err.println("------------ContentProvider创建 了");
return false;
}
@Override
public Cursor query(Uri uri, String[] projection, String selection,
String[] selectionArgs, String sortOrder) {
// TODO Auto-generated method stub
int code = matcher.match(uri);
switch (code) {
case QUERY:
SQLiteDatabase database = mHelper.getReadableDatabase();
return database.query("user", projection, selection, selectionArgs, null, null, sortOrder);
//这里不需要关闭数据库,因为游标返回后还要使用,在外面关闭
case UriMatcher.NO_MATCH:
System.out.println("------------query路径不正确!");
break;
}
return null;
}
@Override
public String getType(Uri url) {
return null;
}
@Override
public Uri insert(Uri uri, ContentValues values) {
// TODO Auto-generated method stub
int code = matcher.match(uri);
switch (code) {
case INSERT:
SQLiteDatabase database = mHelper.getWritableDatabase();
database.insert("user", null, values);
database.close();
break;
case UriMatcher.NO_MATCH:
System.out.println("------------inser路径不正确!");
break;
}
return null;
}
@Override
public int delete(Uri uri, String selection, String[] selectionArgs) {
int code = matcher.match(uri);
switch (code) {
case DELETE:
SQLiteDatabase database = mHelper.getWritableDatabase();
int i = database.delete("user", selection, selectionArgs);
database.close();
return i;
case UriMatcher.NO_MATCH:
System.out.println("------------delete路径不正确!");
break;
}
return 0;
}
@Override
public int update(Uri uri, ContentValues values, String selection,
String[] selectionArgs) {
int code = matcher.match(uri);
switch (code) {
case UPDATE:
SQLiteDatabase database = mHelper.getWritableDatabase();
int i = database.update("user", values, selection, selectionArgs);
database.close();
return i;
case UriMatcher.NO_MATCH:
System.out.println("------------update路径不正确!");
break;
}
return 0;
}
}
DBHelper.java
package com.example.provider;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
/**
* 定义一个数据库操作类
* @author GJ
*
*/
public class MySQLiteOpenHelper extends SQLiteOpenHelper{
public MySQLiteOpenHelper(Context context) {
/**参数1: context 上下文
* 参数2:数据库名称
* 参数3:游标工厂(null为默认)
* 参数4:数据库版本(最小值为1,0会抛异常)
* */
super(context, "test.db", null, 1);
// TODO Auto-generated constructor stub
}
/**
* 数据库创建方法
*/
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
/** 创建表,sqlite数据库不区分字段类型,底层都是varchar类型实现 */
db.execSQL("create table user(id integer primary key autoincrement,name varchar(20),age integer)");
//新增几条初始化数据
db.execSQL("insert into user(name,age) values('123',12)");
db.execSQL("insert into user(name,age) values('dsd',324)");
db.execSQL("insert into user(name,age) values('dsd',52)");
db.execSQL("insert into user(name,age) values('张三',32)");
db.execSQL("insert into user(name,age) values('李四',22)");
}
/**
* 数据库更新方法
*/
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
mainActivty.java
package com.example.provider;
import android.app.Activity;
import android.app.ActionBar;
import android.app.Fragment;
import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.os.Build;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void query(View view){
Uri uri = Uri.parse("content://com.example.provider.myPro/query");
Cursor c = getContentResolver().query(uri, null, null, null, null);
while (c.moveToNext()) {
String name = c.getString(c.getColumnIndex("name"));
int age = c.getInt(c.getColumnIndex("age"));
System.out.println("name:"+name+"------age:"+age);
}
c.close();
}
public void update(View view) {
Uri uri = Uri.parse("content://com.example.provider.myPro/update");
ContentValues values = new ContentValues();
values.put("name", "王五");
int i = getContentResolver().update(uri, values, "name = ?", new String[]{"张三"});
System.out.println("--------更新数据,更新了"+i);
}
public void delete(View view) {
Uri uri = Uri.parse("content://com.example.provider.myPro/delete");
int i = getContentResolver().delete(uri, "name = ?", new String[]{"李四"});
System.out.println("--------删除数据,删除了"+i);
}
public void insert(View view) {
Uri uri = Uri.parse("content://com.example.provider.myPro/insert");
ContentValues values = new ContentValues();
values.put("name", "美女");
values.put("age", 18);
getContentResolver().insert(uri, values);
System.out.println("--------插入数据--");
}
}
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.provider.MainActivity"
tools:ignore="MergeRootFrame" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="查询数据"
android:onClick="query" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="50dp"
android:text="插入数据"
android:onClick="insert" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="100dp"
android:text="更新数据"
android:onClick="update" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="150dp"
android:text="删除数据"
android:onClick="delete" />
</FrameLayout>
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.provider"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="19" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.provider.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!-- android:authorities="com.example.provider.myPro" 访问的主机名,相当于请求跟路径 -->
<provider
android:name="com.example.provider.MyProvider"
android:authorities="com.example.provider.myPro" />
</application>
</manifest>