<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="query"
android:text="查询"
/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="insert"
android:text="插入" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="update"
android:text="更新" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="delete"
android:text="删除" />
</LinearLayout>
----------------------------------------------
MainActivity.java
package com.example.firstresolver;
import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.database.Cursor;
import android.view.View;
import android.widget.Toast;
public class MainActivity extends Activity {
ContentResolver contentResolver;
Uri uri=Uri.parse("content://com.example.firstprovider/");
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//获取系统的ContentResolver对象
contentResolver=getContentResolver();
}
public void query(View source)
{
//调用ContentResolver的query方法,实际返回的是该Uri对应的ContentProvider的query()的返回值
Cursor c=contentResolver.query(uri, null, "query_where", null, null);
Toast.makeText(this, "远程ContentProvider返回的Cursor为:"+c, Toast.LENGTH_SHORT).show();
}
public void insert(View source)
{
ContentValues values=new ContentValues();
values.put("name", "fkjava");
//调用ContentResolver的insert方法
Uri newUri=contentResolver.insert(uri, values);
Toast.makeText(this, "远程ContentProvide新插入记录的Uri为:"+newUri, Toast.LENGTH_SHORT).show();
}
public void update(View source)
{
ContentValues values = new ContentValues();
values.put("name", "fkjava");
// 调用ContentResolver的update()方法。
// 实际返回的是该Uri对应的ContentProvider的update()的返回值
int count = contentResolver.update(uri, values
, "update_where", null);
Toast.makeText(this, "远程ContentProvide更新记录数为:"
+ count, Toast.LENGTH_LONG).show();
}
public void delete(View source)
{
// 调用ContentResolver的delete()方法。
// 实际返回的是该Uri对应的ContentProvider的delete()的返回值
int count = contentResolver.delete(uri
, "delete_where", null);
Toast.makeText(this, "远程ContentProvide删除记录数为:"
+ count, Toast.LENGTH_LONG).show();
}
}
------------------------------
FirstProvider.java
package com.example.firstprovider;
import android.content.ContentProvider;
import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri;
public class FirstProvider extends ContentProvider {
public FirstProvider() {
}
@Override
public boolean onCreate() {
// TODO: Implement this to initialize your content provider on startup.
System.out.println("----conCreate方法被调用----");
return true;
}
@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;
}
@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");
System.out.println(uri+"----delete方法被调用----");
System.out.println("selection参数为: "+selection);
return 0;
}
@Override
public Uri insert(Uri uri, ContentValues values) {
// TODO: Implement this to handle requests to insert a new row.
//throw new UnsupportedOperationException("Not yet implemented");
System.out.println(uri+"----insert方法被调用----");
System.out.println("valuse参数为: "+values);
return null;
}
@Override
public Cursor query(Uri uri, String[] projection, String selection,
String[] selectionArgs, String sortOrder) {
// TODO: Implement this to handle query requests from clients.
System.out.println(uri+"----query方法被调用----");
System.out.println("selection参数为:"+selection);
//throw new UnsupportedOperationException("Not yet implemented");
return null;
}
@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");
System.out.println(uri+"----update方法被调用----");
System.out.println("selection参数为: "+selection+", values参数为:"+values);
return 0;
}
}