contentprovider是安卓四大组件之一,没有Activity复杂的生命周期,只有简单的onCreate过程。ContentProvider是一个抽象类,当实现自己的ContentProvider类,需要继承ContentProvider,并且实现下面6个抽象即可:
insert(Uri, ContentValues):插入新数据;
delete(Uri, String, String[]):删除已有数据;
update(Uri, ContentValues, String, String[]):更新数据;
query(Uri, String[], String, String[], String):查询数据;
onCreate():执行初始化工作;
getType(Uri):获取数据MIME类型。
delete(Uri, String, String[]):删除已有数据;
update(Uri, ContentValues, String, String[]):更新数据;
query(Uri, String[], String, String[], String):查询数据;
onCreate():执行初始化工作;
getType(Uri):获取数据MIME类型。
Uri数据格式如下:
content://com.skytoby.articles/android/1
字段 | 含义 | 对应项 |
---|---|---|
前缀 | 默认的固定开头格式 | content:// |
授权 | 唯一表示provider | com.skytoby.articles |
路径 | 数据类别以及数据项 | /android/1 |
•
一个程序可以通过实现一个
ContentProvider
的抽象接口将自己的数据完全暴露出去,而且
ContentProviders
是以类似数据库中表的方式将数据暴露,也就是说
ContentProvider
就像一个“数据库”。那么外界获取其提供的数据,也就应该与从数据库中获取数据的操作基本一样,只不过是采用
URI
来表示外界需要访问的“数据库”。
•
Android
提供了一些已经在系统中实现的标准
Content Provider
,比如联系人信息,图片库等等,你可以用这些
Content Provider
来访问设备上存储的联系人信息,图片等等。
下面通过实例自建一个provider,然后在另一个app中使用resolver调用这个provider。点击button对student表插入数据。
MyDAO
package com.example.provider;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
public class MyDAO {
private Context context;
private SQLiteDatabase database;
public MyDAO(Context context) {
this.context=context;
MyDBhelper dBhelper=new MyDBhelper(context,"dmyDB",null,1);
database=dBhelper.getWritableDatabase();
}
public Uri DAOinsert(ContentValues contentValues){
long rowid=database.insert("student",null,contentValues);
Uri uri=Uri.parse("content://dmy.provider2");
Uri inserturi=ContentUris.withAppendedId(uri,rowid);
context.getContentResolver().notifyChange(inserturi,null);
return inserturi;
}
}
MyContentProvider
package com.example.provider;
import android.content.ContentProvider;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
public class MyContentProvider extends ContentProvider {
private MyDAO myDAO;
public MyContentProvider() {
}
@Override
public int delete(Uri uri, String selection, String[] selectionArgs) {
// Implement this to handle requests to delete one or more rows.
throw new UnsupportedOperationException("Not yet implemented");
}
@Override
public String getType(Uri uri) {
// TODO: Implement this to handle requests for the MIME type of the data
// at the given URI.
throw new UnsupportedOperationException("Not yet implemented");
}
@Override
public Uri insert(Uri uri, ContentValues values) {
// TODO: Implement this to handle requests to insert a new row.
//getContext().getContentResolver().insert(uri, values);
return myDAO.DAOinsert(values);
}
@Override
public boolean onCreate() {
// TODO: Implement this to initialize your content provider on startup.
Context context=getContext();
myDAO=new MyDAO(context);
return false;
}
@Override
public Cursor query(Uri uri, String[] projection, String selection,
String[] selectionArgs, String sortOrder) {
// TODO: Implement this to handle query requests from clients.
throw new UnsupportedOperationException("Not yet implemented");
}
@Override
public int update(Uri uri, ContentValues values, String selection,
String[] selectionArgs) {
// TODO: Implement this to handle requests to update one or more rows.
throw new UnsupportedOperationException("Not yet implemented");
}
}
resovler的MianActivity
package com.example.resovler;
import androidx.appcompat.app.AppCompatActivity;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ContentResolver resolver=getContentResolver();
Button button=findViewById(R.id.button);
ContentValues values=new ContentValues();
values.put("name","dmy");
values.put("age",20);
Uri uri=Uri.parse("content://dmy.provider2/student");
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {resolver.insert(uri,values);}
});
}
}
provider代码:provider: my-provider
resovler代码:resovler: my-resovler