📅  最后修改于: 2023-12-03 14:59:17.267000             🧑  作者: Mango
Android中的内容提供者( Content Provider)是一种能够向外部提供访问自己私有数据的机制。它可以将数据安全地共享给其他应用,也可以和其他应用共享其他应用的数据。通过内容提供者,应用可以在不知道具体 数据存储位置的情况下获取数据。这种机制也能在应用之间共享数据,从而帮助开发者编写更加高效、更加健壮的代码。
public class MyContentProvider extends ContentProvider {
//定义一个静态变量,这个变量用来描述ContentProvider对外提供的数据。
public static final String AUTHORITY = "com.example.my.contentprovider";
public static final Uri CONTENT_URI = Uri.parse("content://"+ AUTHORITY + "/person");
private static final int PERSON_COLLECTION = 1;
private static final int PERSON_SINGLE = 2;
//定义一个UriMatcher,用来匹配Uri,不匹配则返回-1
private static final UriMatcher uriMatcher;
static {
uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
uriMatcher.addURI(AUTHORITY,"person",PERSON_COLLECTION);
uriMatcher.addURI(AUTHORITY,"person/#",PERSON_SINGLE);
}
//声明一些私有变量和方法,用来对Provider进行一些操作,如增、删、改、查等。
private SQLiteDatabase database;
@Override
public boolean onCreate() {
MyDatabaseHelper helper = new MyDatabaseHelper(getContext());
database = helper.getWritableDatabase();
return (database == null) ? false : true;
}
@Override
public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
switch (uriMatcher.match(uri)){
case PERSON_COLLECTION:
return database.query("person",projection,selection,selectionArgs,null,null,sortOrder);
case PERSON_SINGLE:
long id = ContentUris.parseId(uri);
String where = "id=" + id;
if (TextUtils.isEmpty(selection)){
selection = where;
}
else {
selection = where + " and " + selection;
}
return database.query("person",projection,selection,selectionArgs,null,null,sortOrder);
default:
throw new IllegalArgumentException("URI 不支持。");
}
}
@Override
public Uri insert(Uri uri, ContentValues values) {
long rowId = database.insert("person",null,values);
if (rowId > 0){
Uri insertedUri = ContentUris.withAppendedId(CONTENT_URI,rowId);
getContext().getContentResolver().notifyChange(insertedUri,null);
return insertedUri;
}else {
throw new SQLException("无法保存" + uri);
}
}
@Override
public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
int count = 0;
switch (uriMatcher.match(uri)){
case PERSON_COLLECTION:
count = database.update("person",values,selection,selectionArgs);
break;
case PERSON_SINGLE:
long id = ContentUris.parseId(uri);
String where = "id =" + id;
if (TextUtils.isEmpty(selection)){
selection = where;
}
else {
selection = where + " and " + selection;
}
count = database.update("person",values,selection,selectionArgs);
break;
default:
throw new IllegalArgumentException("URI不支持。");
}
getContext().getContentResolver().notifyChange(uri,null);
return count;
}
@Override
public int delete(Uri uri, String selection, String[] selectionArgs) {
int count = 0;
switch (uriMatcher.match(uri)){
case PERSON_COLLECTION:
count = database.delete("person",selection,selectionArgs);
break;
case PERSON_SINGLE:
long id = ContentUris.parseId(uri);
String where = "id =" + id;
if (TextUtils.isEmpty(selection)){
selection = where;
}
else {
selection = where + " and " + selection;
}
count = database.delete("person",selection,selectionArgs);
break;
default:
throw new IllegalArgumentException("URI 不支持。");
}
getContext().getContentResolver().notifyChange(uri,null);
return count;
}
@Override
public String getType(Uri uri) {
switch (uriMatcher.match(uri)){
case PERSON_COLLECTION:
return "vnd.android.cursor.dir/person";
case PERSON_SINGLE:
return "vnd.android.cursor.item/person";
default:
throw new IllegalArgumentException("URI 不支持。");
}
}
}
<!--在AndroidManifest.xml中注册ContentProvider-->
<provider
android:name=".MyContentProvider"
android:authorities="com.example.my.contentprovider"
android:enabled="true"
android:exported="true"
android:grantUriPermissions="true"
android:multiprocess="true"
android:readPermission="android.permission.READ_CONTACTS"
android:writePermission="android.permission.WRITE_CONTACTS">
</provider>
public class MainActivity extends AppCompatActivity {
private ListView mListView;
private SimpleCursorAdapter mAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mListView = findViewById(R.id.listview);
mAdapter = new SimpleCursorAdapter(this,
android.R.layout.simple_list_item_2,
null,
new String[] {"name","age"},
new int[] {android.R.id.text1,android.R.id.text2},
CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);
mListView.setAdapter(mAdapter);
//访问内容提供者
Uri uri = Uri.parse("content://com.example.my.contentprovider/person");
String[] projection = {"name","age"};
String selection = null;
String[] selectionArgs = null;
String sortOrder = null;
Cursor cursor = getContentResolver().query(uri,projection,selection,selectionArgs,sortOrder);
mAdapter.swapCursor(cursor);
}
}
以上就是关于Android中的内容提供者的介绍和示例。使用ContentProvider能够实现应用之间的数据共享,为开发提供了更加多样化的操作方式。