1,创建ContentProvider对象,该方法需要继承ContentProvider
FirstContentProvider.java
package com.example.firstcontentprovider;
import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.content.ContentProvider;
import android.content.ContentValues;
import android.database.Cursor;
import android.view.Menu;
public class FirstContentProvider extends ContentProvider {
@Override
public int delete(Uri uri, String selection, String[] selectionArgs) {
System.out.println("delete方法被调用");
System.out.println("where参数为"+selection);
return 0;
}
@Override
public String getType(Uri uri) {
// TODO Auto-generated method stub
return null;
}
@Override
public Uri insert(Uri uri, ContentValues values) {
System.out.println("insert方法被调用");
System.out.println("参数为"+values);
return null;
}
@Override
public boolean onCreate() {
System.out.println("OnCreate方法被调用");
return true;
}
@Override
public Cursor query(Uri uri, String[] projection, String selection,
String[] selectionArgs, String sortOrder) {
System.out.println("query方法被调用");
System.out.println("where参数为"+selection);
return null;
}
@Override
public int update(Uri uri, ContentValues values, String selection,
String[] selectionArgs) {
System.out.println("OnCreate方法被调用");
System.out.println("where参数为"+selection);
return 0;
}
}
配置AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.firstcontentprovider"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.firstcontentprovider.FirstResolver"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<provider android:name="com.example.firstcontentprovider.FirstContentProvider"
android:authorities="com.example.firstcontentprovider.firstprovider"
android:exported="true"/>
</application>
</manifest>
2,创建ContentResolver应用程序
FirstResolver.java
package com.example.firstcontentprovider;
import android.app.Activity;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class FirstResolver extends Activity{
ContentResolver resolver;
Uri uri=Uri.parse("content://com.example.firstcontentprovider.firstprovider/");
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_first_content_provider);
//获取系统ContentResolver对象
resolver=getContentResolver();
Button add=(Button) super.findViewById(R.id.add);
Button delete=(Button) super.findViewById(R.id.delete);
Button update=(Button) super.findViewById(R.id.update);
Button query=(Button) super.findViewById(R.id.query);
add.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
insert(arg0);
}
});
delete.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
delete(arg0);
}
});
update.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
update(arg0);
}
});
query.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
query(arg0);
}
});
}
public void delete(View source) {
//返回的是Uri对应的ContentProvider的delete()的返回值
int count=resolver.delete(uri, "delete_where", null);
}
public void insert(View source) {
ContentValues values=new ContentValues();
values.put("name", "hanqing");
Uri returnUri=resolver.insert(uri, values);
}
public void query(View source) {
Cursor c=resolver.query(uri, null, "query_where", null, null);
}
public void update(View source) {
ContentValues values=new ContentValues();
values.put("name", "hanqing");
int count=resolver.update(uri, values, "update_where", null);
}
}
activity_main.xml
<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"
tools:context=".FirstContentProvider"
android:orientation="vertical">
<Button
android:id="@+id/add"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="增加" />
<Button
android:id="@+id/delete"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="删除" />
<Button
android:id="@+id/update"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="修改" />
<Button
android:id="@+id/query"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="查找" />
</LinearLayout>