Android学习笔记2012年(2012-11-23至2012-11-27)

阅读更多

*********************************************2012-11-23****************************************************

十一、Content provider内容提供者控件

1、作用:内容提供者为数据共享提供统一接口,也是一个常用的控件

2、配置:
a、创建内容提供者类继承ContentProvider类的,并覆写抽象方法

b、配置ContentProvider,在提供方的应用的AndroidManifest.xml中添加配置
android:authorities="com.caiz.android.providers.userProvider" />(ContentProvider的唯一标识uri)

3、uri
content://cn.itcast.provider/person/10

代表含义:scheme(固定)// 主机名或authority /路径(数据)

4、编码实现通过内容提供者给外界程序提供操作数据的接口
a、内容提供者实现类:UserProvider

package caiz.android.dboper;

import android.content.ContentProvider;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import caiz.android.dboper.service.DBOpenHelper;

/**
* 内容提供者访问数据库
* @author HuangYucai
*
*/
public class UserProvider extends ContentProvider {
private DBOpenHelper dbOpenHelper;
private static final UriMatcher MATCHER = new UriMatcher(
UriMatcher.NO_MATCH);
private static final int INSERTUSER = 1;
private static final int DELETEUSER = 2;
static {
MATCHER.addURI("com.caiz.android.providers.userProvider", "user",
INSERTUSER);
MATCHER.addURI("com.caiz.android.providers.userProvider", "user/#",
DELETEUSER);
}

@Override
public boolean onCreate() {// 当内容提供者创建时自动调用
dbOpenHelper = new DBOpenHelper(this.getContext());
// TODO Auto-generated method stub
return false;
}

@Override
public Cursor query(Uri uri, String[] projection, String selection,
String[] selectionArgs, String sortOrder) {
SQLiteDatabase db = dbOpenHelper.getWritableDatabase();
Cursor cursor = null;
switch (MATCHER.match(uri)) {
case 1:
cursor = db.query("user", projection, selection, selectionArgs,
null, null, sortOrder);
return cursor;
case 2:
long rowid = ContentUris.parseId(uri);
String where = "uid=" + rowid;
if (selection != null && !"".equals(selection.trim())) {
where = where + " and " + selection;
}
cursor = db.query("user", projection, where, selectionArgs, null,
null, sortOrder);
return cursor;
default:
throw new IllegalArgumentException("The uri'" + uri
+ "' is illegal!");
}
}// 供外部应用查询内容提供者的数据

@Override
public String getType(Uri uri) {// 返回操作数据的类型
// TODO Auto-generated method stub
return null;
}

@Override
public Uri insert(Uri uri, ContentValues values) {// 供外部应用插入内容提供者的数据
// 判断uri专门类
SQLiteDatabase db = dbOpenHelper.getWritableDatabase();

switch (MATCHER.match(uri)) {
case 1:
long rowid = db.insert("user", "uname", values);
Uri insertUri = ContentUris.withAppendedId(uri, rowid);
return insertUri;

default:
throw new IllegalArgumentException("The uri'" + uri
+ "' is illegal!");
}
}

@Override
public int delete(Uri uri, String selection, String[] selectionArgs) {// 供外部应用删除内容提供者的数据
// 判断uri专门类
SQLiteDatabase db = dbOpenHelper.getWritableDatabase();
int rowCounts = 0;
switch (MATCHER.match(uri)) {
case 1:
rowCounts = db.delete("user", selection, selectionArgs);
break;
case 2:
long rowid = ContentUris.parseId(uri);
String where = "uid=" + rowid;
if (selection != null && !"".equals(selection.trim())) {
where = where + " and " + selection;
}
rowCounts = db.delete("user", where, selectionArgs);
break;
default:
throw new IllegalArgumentException("The uri'" + uri
+ "' is illegal!");
}
return rowCounts;
}

@Override
public int update(Uri uri, ContentValues values, String selection,
String[] selectionArgs) {// 供外部应用更新内容提供者的数据
SQLiteDatabase db = dbOpenHelper.getWritableDatabase();
int rowCounts = 0;
switch (MATCHER.match(uri)) {
case 1:
rowCounts = db.update("user", values, selection, selectionArgs);
break;
default:
throw new IllegalArgumentException("The uri'" + uri
+ "' is illegal!");
}
return rowCounts;
}

}

b、内容通过者的测试类:ContentProviderTest

package caiz.android.xml.test;

import android.content.ContentResolver;
import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri;
import android.test.AndroidTestCase;
import android.util.Log;

/**
* 内容提供者测试类
* @author HuangYucai
*
*/
public class ContentProviderTest extends AndroidTestCase {

public void testInsert()throws Exception{//测试添加

Uri uri=Uri.parse("content://com.caiz.android.providers.userProvider/user");
ContentResolver resolver=this.getContext().getContentResolver();
ContentValues values=new ContentValues();
values.put("uname", "tqiongqiong");
values.put("phone", "1523798091");
values.put("amount", "5000001");
resolver.insert(uri, values);
}
public void testDelete()throws Exception{//测试删除
Uri uri=Uri.parse("content://com.caiz.android.providers.userProvider/user/44");
ContentResolver resolver=this.getContext().getContentResolver();
int i=resolver.delete(uri, null, null);
Log.i("ContentProvider", "删除了"+i+"条记录");
}
public void testUpdate()throws Exception{//测试更新
Uri uri=Uri.parse("content://com.caiz.android.providers.userProvider/user/");
ContentResolver resolver=this.getContext().getContentResolver();

ContentValues values=new ContentValues();
values.put("uname", "tangqiongqiong");
values.put("amount", "5000002");
int i=resolver.update(uri, values, "uid=?", new String[]{"45"});
Log.i("ContentProvider", "更新了"+i+"条记录");
}
public void testQueryList()throws Exception{//测试查询列表
Uri uri=Uri.parse("content://com.caiz.android.providers.userProvider/user/");
ContentResolver resolver=this.getContext().getContentResolver();
Cursor cursor= resolver.query(uri, null, "uid>?", new String[]{"20"}, "uid desc");
int num=0;
while(cursor.moveToNext()){
String uname= cursor.getString(cursor.getColumnIndex("UNAME"));
Log.i("ContentProvider", "用户"+(++num)+"的姓名:"+uname);
}

}
public void testQueryById()throws Exception{//测试查询单个
Uri uri=Uri.parse("content://com.caiz.android.providers.userProvider/user/");
ContentResolver resolver=this.getContext().getContentResolver();

Cursor cursor= resolver.query(uri, null, "uid=?", new String[]{"45"}, "uid desc");
if(cursor.moveToNext()){
String uname= cursor.getString(cursor.getColumnIndex("UNAME"));
Log.i("ContentProvider", "id为45的用户姓名是:"+uname);
}

}
}

***********************************************************************************************************

*************************************************2012-11-24************************************************
十一、监听Content provider内容提供者的数据变化

1、作用:两个应用操作使用同一个内容提供者的数据,A应用改变了内容提供者数据,必须通知B应用内容提供者数据
已经改变
A应用---添加数据---》内容提供者---》发出数据变化通知---》B应用(做出响应)

原理:观察者设计模式的应用

2、代码实践:
a、A应用在界面中添加一个按钮,并设置单击事件:

android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

你可能感兴趣的:(android)