Manifest
<provider
android:name=".MyContentProvider"
android:authorities="com.zhangyu.testcontentprovider"
android:exported="true"></provider>
MyContentProvider
package com.zhangyu.testcontentprovider;
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;
import android.util.Log;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
public class MyContentProvider extends ContentProvider {
private static final String TAG = "MyContentProvider";
private MySqlite mySqlite;
private String tableName = "userInfo";
private static final int OK = 1;
static UriMatcher uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
static {
uriMatcher.addURI("com.zhangyu.testcontentprovider", "test", OK);
}
@Override
public boolean onCreate() {
mySqlite = new MySqlite(getContext());
return false;
}
@Nullable
@Override
public Cursor query(@NonNull Uri uri, @Nullable String[] projection, @Nullable String selection, @Nullable String[] selectionArgs, @Nullable String sortOrder) {
Cursor cursor = null;
if (uriMatcher.match(uri) == OK) {
SQLiteDatabase db = mySqlite.getReadableDatabase();
cursor = db.query(tableName, projection, selection, selectionArgs, null, null, null);
}
return cursor;
}
@Nullable
@Override
public String getType(@NonNull Uri uri) {
return null;
}
@Nullable
@Override
public Uri insert(@NonNull Uri uri, @Nullable ContentValues values) {
Log.e(TAG, "insert: " + values.getAsString("name") + "," + values.getAsInteger("age"));
if (uriMatcher.match(uri) == OK) {
SQLiteDatabase db = mySqlite.getWritableDatabase();
db.insert(tableName, null, values);
db.close();
}
return uri;
}
@Override
public int delete(@NonNull Uri uri, @Nullable String selection, @Nullable String[] selectionArgs) {
if (uriMatcher.match(uri) == OK) {
SQLiteDatabase db = mySqlite.getWritableDatabase();
db.delete(tableName, selection, selectionArgs);
db.close();
}
return 0;
}
@Override
public int update(@NonNull Uri uri, @Nullable ContentValues values, @Nullable String selection, @Nullable String[] selectionArgs) {
if (uriMatcher.match(uri) == OK) {
SQLiteDatabase db = mySqlite.getWritableDatabase();
db.update(tableName, values, selection, selectionArgs);
db.close();
}
return 0;
}
}
MySqlite
package com.zhangyu.testcontentprovider;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class MySqlite extends SQLiteOpenHelper {
public MySqlite(Context context) {
super(context, "my.db", null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("create table userInfo (_id integer primary key autoincrement,name varchar(20),age integer);");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
另外一个程序调用第一个的ContentProvider
package com.zhangyu.testcontentprovider2;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
private static final String TAG = "MainActivity";
private ContentResolver contentResolver;
private Uri uri;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
contentResolver = getContentResolver();
uri = Uri.parse("content://com.zhangyu.testcontentprovider/test");
}
public void onClick(View view) {
switch (view.getId()) {
case R.id.bt_insert:
insert();
break;
case R.id.bt_query:
query();
break;
case R.id.bt_update:
update();
break;
case R.id.bt_delete:
delete();
break;
}
}
private void delete() {
contentResolver.delete(uri,"name=?",new String[]{"zhangyu"});
}
private void update() {
ContentValues values = new ContentValues();
values.put("name", "liming");
values.put("age", 12);
contentResolver.update(uri,values,"name=?",new String[]{"zhangyu"});
}
private void query() {
Cursor cursor = contentResolver.query(uri, null, null, null, null);
while (cursor.moveToNext()) {
String name = cursor.getString(cursor.getColumnIndex("name"));
int age = cursor.getInt(cursor.getColumnIndex("age"));
Log.e(TAG, "query: " + name + "," + age);
}
}
private void insert() {
ContentValues values = new ContentValues();
values.put("name", "zhangyu");
values.put("age", 20);
contentResolver.insert(uri, values);
}
}
2020-06-19 10:31:51.458 13523-13536/com.zhangyu.testcontentprovider E/MyContentProvider: insert: zhangyu,20
2020-06-19 10:32:02.829 13795-13795/com.zhangyu.testcontentprovider2 E/MainActivity: query: zhangyu,20
2020-06-19 10:32:07.725 13795-13795/com.zhangyu.testcontentprovider2 E/MainActivity: query: liming,12