package com.test.contentprovider;
import android.content.ContentProvider;
import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri;
import android.util.Log;
/**
* MyContentProvider
*/
public class MyContentProvider extends ContentProvider {
public MyContentProvider() {
}
//第一次创建该 ContentProvider 时调用该方法
@Override
public boolean onCreate() {
// TODO: Implement this to initialize your content provider on startup.
Log.d("MyContentProvider", "onCreate方法被调用");
return true;
}
//该方法的返回值代表了该 ContentProvider所提供数据的 MIME类型
@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");
return null;
}
//实现查询方法该方法返回查询得到的cursor
@Override
public Cursor query(Uri uri, String[] projection, String selection,
String[] selectionArgs, String sortOrder) {
Log.d("MyContentProvider", uri + " query方法被调用");
Log.d("MyContentProvider", "selection: " + selection);
return null;
}
//实现插入法 该方法返回新插入记录的 uri
@Override
public Uri insert(Uri uri, ContentValues values) {
Log.d("MyContentProvider", uri + " insert方法被调用");
Log.d("MyContentProvider", "values: " + values);
return null;
}
//实现删除方法 该方法返回被删除的记录条数
@Override
public int delete(Uri uri, String selection, String[] selectionArgs) {
Log.d("MyContentProvider", uri + " delete 方法被调用");
Log.d("MyContentProvider", "selection: " + selection);
return 0;
}
//实现更新方法 该方法返回被更新的记录条数
@Override
public int update(Uri uri, ContentValues values, String selection,
String[] selectionArgs) {
Log.d("MyContentProvider", uri + " update 方法被调用");
Log.d("MyContentProvider", "values: " + values);
Log.d("MyContentProvider", "selection: " + selection);
return 0;
}
}
ContentResolver
package com.test.contentprovider;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
/**
* ContentResolver
*/
public class MyResolverActivity extends AppCompatActivity {
ContentResolver mContentResolver;
Uri uri = Uri.parse("content://cn.itcast.provider/");
Button insert, update, query, delete;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my_resolver);
mContentResolver = getContentResolver();
initView();
initListener();
}
private void initView() {
insert = (Button) findViewById(R.id.insert);
delete = (Button) findViewById(R.id.delete);
update = (Button) findViewById(R.id.update);
query = (Button) findViewById(R.id.query);
}
private void initListener() {
query.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//调用 ContentResolver的 query()方法
//实际是返回的是该 uri 对应的 ContentProvider的 query()方法
Cursor cursor = mContentResolver.query(uri, null, "query_where", null, null);
Toast.makeText(MyResolverActivity.this, "远程ContentResolver的返回的cursor为 : " + cursor, Toast.LENGTH_SHORT).show();
}
});
insert.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ContentValues values = new ContentValues();
values.put("name", "android");
// 调用ContentResolver的insert()方法。
// 实际返回的是该Uri对应的ContentProvider的insert()的返回值
Uri newUri = mContentResolver.insert(uri, values);
Toast.makeText(MyResolverActivity.this, "远程ContentProvide新插入记录的Uri为:" + newUri, Toast.LENGTH_LONG).show();
}
});
update.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ContentValues values = new ContentValues();
values.put("name", "android");
// 调用ContentResolver的update()方法。
// 实际返回的是该Uri对应的ContentProvider的update()的返回值
int count = mContentResolver.update(uri, values
, "update_where", null);
Toast.makeText(MyResolverActivity.this, "远程ContentProvide更新记录数为:"
+ count, Toast.LENGTH_LONG).show();
}
});
delete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// 调用ContentResolver的delete()方法。
// 实际返回的是该Uri对应的ContentProvider的delete()的返回值
int count = mContentResolver.delete(uri
, "delete_where", null);
Toast.makeText(MyResolverActivity.this, "远程ContentProvide删除记录数为:"
+ count, Toast.LENGTH_LONG).show();
}
});
}
}
布局界面
<?xml version="1.0" encoding="utf-8"?>
<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"
tools:context="com.test.contentprovider.MyResolverActivity">
<Button
android:id="@+id/insert"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/insert"
/>
<Button
android:id="@+id/query"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/query"
/>
<Button
android:id="@+id/delete"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/delete"
/>
<Button
android:id="@+id/update"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/update"
/>
</LinearLayout>