ContentProvider
安卓四大组件之一
作用:把私有数据暴露给其他的应用,通常把私有数据库的数据暴露给其他的应用。
安卓代码模拟执行某一个方法:
创建一个class文件Test.java继承来自android.test.AndroidTestCase。然后在清单文件里面添加配置项,androdi:name的配置是固定的,包名就是package,user-libiary的名字也是固定的。
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.ldw.selfContentProvider"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<instrumentation android:name="android.test.InstrumentationTestRunner"
android:targetPackage="com.ldw.selfContentProvider"
></instrumentation>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<uses-library android:name="android.test.runner"/>
<activity
android:name="com.ldw.selfContentProvider.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
模拟创建一个数据库:
package com.ldw.selfContentProvider;
import android.test.AndroidTestCase;
public class Test extends AndroidTestCase {
public void test(){
MyOpenHelper my = new MyOpenHelper(getContext(), "po.db", null, 1);
my.getWritableDatabase();
}
}
APP1:创建内容提供者
内容提供者需要在清单文件总配置,authorities是内容提供者的地址,通过这个地址可以访问内容提供者,精确的知道访问的是哪一个内容提供者,exported:可以导出数据,获取到数据。
可以在Uri后面添加参数来进一步限定访问哪一个数据表,比如下面的Uri是com.ldw.po限定访问的是po表:
Uri.parse("content://com.ldw.po/po")
配置范例如下:
<provider android:name="com.ldw.selfContentProvider.poProvider"
android:authorities="com.ldw.po"
android:exported="true"
></provider>
Manifist.xml文件具体如下
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.ldw.selfContentProvider"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<instrumentation android:name="android.test.InstrumentationTestRunner"
android:targetPackage="com.ldw.selfContentProvider"
></instrumentation>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<uses-library android:name="android.test.runner"/>
<activity
android:name="com.ldw.selfContentProvider.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<provider android:name="com.ldw.selfContentProvider.poProvider"
android:authorities="com.ldw.po"
android:exported="true"
></provider>
</application>
</manifest>
MyOpenHelper.java
package com.ldw.selfContentProvider;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
public class MyOpenHelper extends SQLiteOpenHelper {
public MyOpenHelper(Context context, String name, CursorFactory factory,
int version) {
super(context, name, factory, version);
// TODO Auto-generated constructor stub
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL("create table po(_id integer primary key autoincrement, name char(10), money integer(20))");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
}
}
poProvider.java
package com.ldw.selfContentProvider;
import android.content.ContentProvider;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
public class poProvider extends ContentProvider {
private MyOpenHelper my;
SQLiteDatabase db;
//内容提供者创建的时候调用
@Override
public boolean onCreate() {
// TODO Auto-generated method stub
my = new MyOpenHelper(getContext(), "po.db", null, 1);
db = my.getWritableDatabase();
return false;
}
@Override
public Cursor query(Uri uri, String[] projection, String selection,
String[] selectionArgs, String sortOrder) {
// TODO Auto-generated method stub
Cursor cursor = db.query("po", projection, selection, selectionArgs, null, null, sortOrder, null);
return cursor;
}
@Override
public String getType(Uri uri) {
// TODO Auto-generated method stub
return null;
}
//这个方法提供其他的方法调用,用于往po数据库中插入数据
//values由其他的应用传入,封装需要被传入的数据,
@Override
public Uri insert(Uri uri, ContentValues values) {
// TODO Auto-generated method stub
db.insert("po", null, values);
return uri;
}
@Override
public int delete(Uri uri, String selection, String[] selectionArgs) {
// TODO Auto-generated method stub
//返回值是删除的行数
int i = db.delete("po", selection, selectionArgs);
return i;
}
@Override
public int update(Uri uri, ContentValues values, String selection,
String[] selectionArgs) {
// TODO Auto-generated method stub
//返回值是
int i = db.update("po", values, selection, selectionArgs);
return i;
}
}
APP2:访问自己定义的内容提供者
activity_main.xml
<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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity"
android:orientation="vertical"
>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="插入"
android:onClick="insert"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="删除"
android:onClick="delete"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="修改"
android:onClick="update"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="查询"
android:onClick="query"
/>
</LinearLayout>
MainActivity.java
package com.ldw.visit;
import android.app.Activity;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void insert(View v){
//通过内容提供者把数据插入people数据库
//拿到contentResolver
ContentResolver cr = getContentResolver();
ContentValues values = new ContentValues();
values.put("name", "AAA");
values.put("money", "15000");
//Uri前面必须带前缀,是内容提供者的地址
cr.insert(Uri.parse("content://com.ldw.po"), values);
values.put("name", "BBB");
values.put("money", "15000");
//Uri前面必须带前缀,是内容提供者的地址
cr.insert(Uri.parse("content://com.ldw.po"), values);
values.put("name", "CCC");
values.put("money", "15000");
//Uri前面必须带前缀,是内容提供者的地址
cr.insert(Uri.parse("content://com.ldw.po"), values);
values.put("name", "DDD");
values.put("money", "15000");
//Uri前面必须带前缀,是内容提供者的地址
cr.insert(Uri.parse("content://com.ldw.po"), values);
}
public void delete(View v){
//通过内容提供者把数据插入people数据库
//拿到contentResolver
ContentResolver cr = getContentResolver();
int i = cr.delete(Uri.parse("content://com.ldw.po"), "name = ?", new String[]{"AAA"});
System.out.println(i);
}
public void update(View v){
//通过内容提供者把数据插入people数据库
//拿到contentResolver
ContentResolver cr = getContentResolver();
ContentValues values = new ContentValues();
values.put("name", "aaa");
int i = cr.update(Uri.parse("content://com.ldw.po"), values, "name = ?", new String[]{"AAA"});
System.out.println(i);
}
public void query(View v){
//通过内容提供者把数据插入people数据库
//拿到contentResolver
ContentResolver cr = getContentResolver();
Cursor cursor = cr.query(Uri.parse("content://com.ldw.po"), null, null, null, null);
while(cursor.moveToNext()){
String name = cursor.getString(1);
String money = cursor.getString(2);
System.out.println("name:" + name + ";" + "money" + money);
}
}
}