注意:要使用ContentProvider内容提供者,必须还要有一个ContentResolver来接收和操作ContentProvider所提供的数据
所以,写了俩个项目,分别为ContentProvider和ContentResolver
在ContentProvider里:
注册了一个Provider
代码如下:
<provider android:name=".FirstProvider" android:authorities="com.first" android:exported="true"></provider>
package com.example.contentproviderapp; import android.content.ContentProvider; import android.content.ContentValues; import android.database.Cursor; import android.net.Uri; public class FirstProvider extends ContentProvider{ @Override public int delete(Uri arg0, String arg1, String[] arg2) { // TODO Auto-generated method stub System.out.println("delete"); return 0; } @Override public String getType(Uri arg0) { // TODO Auto-generated method stub System.out.println("getType"); return null; } @Override public Uri insert(Uri arg0, ContentValues arg1) { // TODO Auto-generated method stub System.out.println("insert"); return null; } @Override public boolean onCreate() { // TODO Auto-generated method stub System.out.println("onCreate"); return false; } @Override public Cursor query(Uri arg0, String[] arg1, String arg2, String[] arg3, String arg4) { // TODO Auto-generated method stub System.out.println("query"); return null; } @Override public int update(Uri arg0, ContentValues arg1, String arg2, String[] arg3) { // TODO Auto-generated method stub System.out.println("update"); return 0; } }在ContentResolver里用MainActivity处理了FirstProvider传来的数据
activity_main布局:
<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:orientation="vertical"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="添加数据" android:onClick="myadd" android:textSize="30sp"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="删除数据" android:onClick="mydelete" android:textSize="30sp"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="更新数据" android:onClick="myupdate" android:textSize="30sp"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="查询数据" android:onClick="myquery" android:textSize="30sp"/> </LinearLayout>
package com.example.contentresolverapp; import android.net.Uri; import android.os.Bundle; import android.app.Activity; import android.content.ContentProvider; import android.content.ContentResolver; import android.content.ContentValues; import android.view.Menu; import android.view.View; public class MainActivity extends Activity { //操作ContentProvider所提供的数据 private ContentResolver cr; private String uri="content://com.first"; public void myadd(View view) { // TODO Auto-generated method stub ContentValues values=new ContentValues(); cr.insert(Uri.parse(uri), values); } public void mydelete(View view) { // TODO Auto-generated method stub cr.delete(Uri.parse(uri), null, null); } public void myupdate(View view) { // TODO Auto-generated method stub ContentValues values=new ContentValues(); cr.update(Uri.parse(uri), values, null, null); } public void myquery(View view) { // TODO Auto-generated method stub cr.query(Uri.parse(uri), null, null, null, null); } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); cr=getContentResolver(); } }
效果图: