ContentProvider实现流程
public class DataBaseContentProvider extends ContentProvider { private SQLiteOpenHelper mSQLiteOpenHelper; private SQLiteDatabase db;
/**
* URI
*/
private static final UriMatcher URIMATCHER = new UriMatcher(
UriMatcher.NO_MATCH); private static final int URI_MATCH_USER = 0; private static final int URI_MATCH_FRIEND = 1; private static final int URI_MATCH_TALK = 2; private static final int URI_MATCH_COMMENT = 3; private static final int URI_MATCH_ALARM = 4; static {
URIMATCHER.addURI(Constants.AUTHORITY, FriendTable.TABLE_NAME,
URI_MATCH_FRIEND); URIMATCHER.addURI(Constants.AUTHORITY, UserTable.TABLE_NAME,
URI_MATCH_USER); URIMATCHER.addURI(Constants.AUTHORITY, TalkTable.TABLE_NAME,
URI_MATCH_TALK); URIMATCHER.addURI(Constants.AUTHORITY, CommentTable.TABLE_NAME,
URI_MATCH_COMMENT); URIMATCHER.addURI(Constants.AUTHORITY,AlarmTable.TABLE_NAME,
URI_MATCH_ALARM); } @Override
public boolean onCreate() {
// TODO Auto-generated method stub
mSQLiteOpenHelper = DataBaseHelper.getInstance(getContext());
db = mSQLiteOpenHelper.getWritableDatabase();
return true;
} /**
* 返回要查询的表名
*/
private String getTableNameByUri(Uri uri) {
if (uri == null) {
return null;
}
int match = URIMATCHER.match(uri);
switch (match) {
case URI_MATCH_USER:
return UserTable.TABLE_NAME; case URI_MATCH_FRIEND:
return FriendTable.TABLE_NAME; case URI_MATCH_TALK:
return TalkTable.TABLE_NAME; case URI_MATCH_COMMENT:
return CommentTable.TABLE_NAME; case URI_MATCH_ALARM:
return AlarmTable.TABLE_NAME; default:
throw new IllegalArgumentException(
"DataBaseContentProvider getTableNameByUri");
}
} @Override
public int delete(Uri url, String where, String[] whereArgs) {
// TODO Auto-generated method stub
int match = URIMATCHER.match(url);
if (match == UriMatcher.NO_MATCH) {
throw new IllegalArgumentException("Unknown URL");
} int count = db.delete(getTableNameByUri(url), where, whereArgs);
ContentResolver cr = getContext().getContentResolver();
cr.notifyChange(url, null);
return count;
} @Override
public String getType(Uri arg0) {
// TODO Auto-generated method stub switch (URIMATCHER.match(arg0)) {
case URI_MATCH_USER:
return UserTable.CONTENT_TYPE;
case URI_MATCH_FRIEND:
return FriendTable.CONTENT_TYPE;
case URI_MATCH_TALK:
return TalkTable.CONTENT_TYPE;
case URI_MATCH_COMMENT:
return CommentTable.CONTENT_TYPE;
case URI_MATCH_ALARM:
return AlarmTable.CONTENT_TYPE;
default:
throw new IllegalArgumentException(
"DataBaseContentProvider getType");
}
} @Override
public Uri insert(Uri uri, ContentValues values) {
// TODO Auto-generated method stub int match = URIMATCHER.match(uri);
Uri rtUri = null;
long rowId = db.replace(getTableNameByUri(uri), null, values);
if (rowId > 0) {
rtUri = ContentUris.withAppendedId(uri, rowId);
}
ContentResolver cr = getContext().getContentResolver();
cr.notifyChange(uri, null);
return rtUri;
} @Override
public Cursor query(Uri uri, String[] projection, String selection,
String[] selectionArgs, String sortOrder) {
// TODO Auto-generated method stub
String tableName = this.getTableNameByUri(uri);
Cursor c = db.query(tableName, projection, selection, selectionArgs,
null, null, sortOrder, null);
c.setNotificationUri(getContext().getContentResolver(), uri);
return c;
} @Override
public int update(Uri uri, ContentValues values, String selection,
String[] selectionArgs) {
// TODO Auto-generated method stub
String tableName = this.getTableNameByUri(uri);
int rt = db.update(tableName, values, selection, selectionArgs);
ContentResolver cr = getContext().getContentResolver();
cr.notifyChange(uri, null);
return rt;
} }
<provider android:name="com.winning.cqs.pregnantbabycare_cqs_v1.db.DataBaseContentProvider"
android:authorities="com.winning.cqs.pregnantbabycare_cqs_v1"
android:multiprocess="true" >
</provider>
public class User implements Serializable { private static final long serialVersionUID = 1L; public int _id;
public String hospitalHost;
public String hospitalName;
public String lastModify;
public String idNo;
public String mobile;
public String name;
public String hospitalCode;
public String dueDate;
public Integer activity;
public Integer yxtUserID;
public String nick;
public String cardTypeDis;
public Integer hospitalID;
public int id;
public Integer yxtMemberID;
public Integer defaultPatientID;
public String address;
public Integer lastPregnancyID;
public String createDate;
public String cardNo;
public String medicalNo;
public Integer patientID; public static String saveUserToDB(ContentResolver cr, User user) {
if (null == cr || null == user) {
return null;
}
String result = null;
//获取到还没有被修改过的用户信息
User user2 = getUsers(cr, String.valueOf(user.id));
ContentValues values = new ContentValues(); values.put(UserTable.Activity, user.activity);
values.put(UserTable.Address, user.address);
values.put(UserTable.CardNo, user.cardNo);
values.put(UserTable.CardTypeDis, user.cardTypeDis);
values.put(UserTable.CreateDate, user.createDate);
values.put(UserTable.DefaultPatientID, user.defaultPatientID);
values.put(UserTable.DueDate, user.dueDate);
values.put(UserTable.HospitalCode, user.hospitalCode);
values.put(UserTable.HospitalHost, user.hospitalHost);
values.put(UserTable.HospitalID, user.hospitalID);
values.put(UserTable.HospitalName, user.hospitalName);
values.put(UserTable.Id, user.id);
values.put(UserTable.IdNo, user.idNo);
values.put(UserTable.LastModify, user.lastModify);
values.put(UserTable.LastPregnancyID, user.lastPregnancyID);
values.put(UserTable.MedicalNo, user.medicalNo);
values.put(UserTable.Mobile, user.mobile);
values.put(UserTable.Name, user.name);
values.put(UserTable.Nick, user.nick);
values.put(UserTable.PatientID, user.patientID);
values.put(UserTable.YxtMemberID, user.yxtMemberID);
values.put(UserTable.YxtUserID, user.yxtUserID); if (values.size() > 0) {
String whereStr = UserTable.Id + " = " + user.id;
// 查询是否有相同数据
Cursor cursor = cr.query(UserTable.CONTENT_URI, null, whereStr,
null, null);
if ((null != cursor) && (0 < cursor.getCount())) {
cr.update(UserTable.CONTENT_URI, values, whereStr, null);
} else {
cr.insert(UserTable.CONTENT_URI, values);
}
if (cursor != null) {
cursor.close();
}
} if (isUpdate(user, user2))
result = "信息保存成功!"; return result;
}
/**
* 判断用户信息是否被修改
* @param user1
* @param user2
* @return
*/
public static boolean isUpdate(User user1, User user2) {
boolean b = true;
if (user1 == null || user2 == null) {
return b;
}
if (user1.id == user2.id && user1.nick.equals(user2.nick)
&& user1.dueDate.equals(user2.dueDate)) {
b = false;
}
return b;
} public static User getUsers(ContentResolver cr, String id) {
User friend = null;
if (null != cr) {
Cursor cursor = cr.query(UserTable.CONTENT_URI, null, UserTable.Id
+ "=?", new String[] { id }, null);
if (cursor.moveToFirst()) {
friend = new User(); friend._id = cursor
.getInt(cursor.getColumnIndex(UserTable._ID));
friend.activity = cursor.getInt(cursor
.getColumnIndex(UserTable.Activity));
friend.address = cursor.getString(cursor
.getColumnIndex(UserTable.Address));
friend.cardNo = cursor.getString(cursor
.getColumnIndex(UserTable.CardNo));
friend.cardTypeDis = cursor.getString(cursor
.getColumnIndex(UserTable.CardTypeDis));
friend.createDate = cursor.getString(cursor
.getColumnIndex(UserTable.CreateDate));
friend.defaultPatientID = cursor.getInt(cursor
.getColumnIndex(UserTable.DefaultPatientID));
friend.dueDate = cursor.getString(cursor
.getColumnIndex(UserTable.DueDate));
friend.hospitalCode = cursor.getString(cursor
.getColumnIndex(UserTable.HospitalCode));
friend.hospitalHost = cursor.getString(cursor
.getColumnIndex(UserTable.HospitalHost));
friend.hospitalID = cursor.getInt(cursor
.getColumnIndex(UserTable.HospitalID));
friend.hospitalName = cursor.getString(cursor
.getColumnIndex(UserTable.HospitalName));
friend.id = cursor.getInt(cursor.getColumnIndex(UserTable.Id));
friend.idNo = cursor.getString(cursor
.getColumnIndex(UserTable.IdNo));
friend.lastModify = cursor.getString(cursor
.getColumnIndex(UserTable.LastModify));
friend.lastPregnancyID = cursor.getInt(cursor
.getColumnIndex(UserTable.LastPregnancyID));
friend.medicalNo = cursor.getString(cursor
.getColumnIndex(UserTable.MedicalNo));
friend.mobile = cursor.getString(cursor
.getColumnIndex(UserTable.Mobile));
friend.name = cursor.getString(cursor
.getColumnIndex(UserTable.Name));
friend.nick = cursor.getString(cursor
.getColumnIndex(UserTable.Nick));
friend.patientID = cursor.getInt(cursor
.getColumnIndex(UserTable.PatientID));
friend.yxtMemberID = cursor.getInt(cursor
.getColumnIndex(UserTable.YxtMemberID));
friend.yxtUserID = cursor.getInt(cursor
.getColumnIndex(UserTable.YxtUserID)); cursor.moveToNext();
cursor.close();
} }
return friend;
} public static ArrayList<User> getAllUsers(ContentResolver cr) {
ArrayList<User> users = new ArrayList<User>();
if (null != cr) {
Cursor cursor = cr.query(UserTable.CONTENT_URI, null, null, null,
null);
if (null != cursor) {
int friendsCount = cursor.getCount(); cursor.moveToFirst();
for (int k = 0; k < friendsCount; k++) {
User friend = new User(); friend._id = cursor.getInt(cursor
.getColumnIndex(UserTable._ID));
friend.activity = cursor.getInt(cursor
.getColumnIndex(UserTable.Activity));
friend.address = cursor.getString(cursor
.getColumnIndex(UserTable.Address));
friend.cardNo = cursor.getString(cursor
.getColumnIndex(UserTable.CardNo));
friend.cardTypeDis = cursor.getString(cursor
.getColumnIndex(UserTable.CardTypeDis));
friend.createDate = cursor.getString(cursor
.getColumnIndex(UserTable.CreateDate));
friend.defaultPatientID = cursor.getInt(cursor
.getColumnIndex(UserTable.DefaultPatientID));
friend.dueDate = cursor.getString(cursor
.getColumnIndex(UserTable.DueDate));
friend.hospitalCode = cursor.getString(cursor
.getColumnIndex(UserTable.HospitalCode));
friend.hospitalHost = cursor.getString(cursor
.getColumnIndex(UserTable.HospitalHost));
friend.hospitalID = cursor.getInt(cursor
.getColumnIndex(UserTable.HospitalID));
friend.hospitalName = cursor.getString(cursor
.getColumnIndex(UserTable.HospitalName));
friend.id = cursor.getInt(cursor
.getColumnIndex(UserTable.Id));
friend.idNo = cursor.getString(cursor
.getColumnIndex(UserTable.IdNo));
friend.lastModify = cursor.getString(cursor
.getColumnIndex(UserTable.LastModify));
friend.lastPregnancyID = cursor.getInt(cursor
.getColumnIndex(UserTable.LastPregnancyID));
friend.medicalNo = cursor.getString(cursor
.getColumnIndex(UserTable.MedicalNo));
friend.mobile = cursor.getString(cursor
.getColumnIndex(UserTable.Mobile));
friend.name = cursor.getString(cursor
.getColumnIndex(UserTable.Name));
friend.nick = cursor.getString(cursor
.getColumnIndex(UserTable.Nick));
friend.patientID = cursor.getInt(cursor
.getColumnIndex(UserTable.PatientID));
friend.yxtMemberID = cursor.getInt(cursor
.getColumnIndex(UserTable.YxtMemberID));
friend.yxtUserID = cursor.getInt(cursor
.getColumnIndex(UserTable.YxtUserID)); users.add(friend); cursor.moveToNext();
}
cursor.close();
} }
return users;
} public static class UserTable implements BaseColumns {
public static final String TABLE_NAME = "user"; public static final Uri CONTENT_URI = Uri.parse("content://"
+ Constants.AUTHORITY + "/" + TABLE_NAME); public static final String CONTENT_TYPE = "vnd.android.cursor.dir/"
+ Constants.AUTHORITY + "/" + TABLE_NAME; public static final String _ID = "_id";
public static final String HospitalHost = "hospitalHost";
public static final String HospitalName = "hospitalName";
public static final String LastModify = "lastModify";
public static final String IdNo = "idNo";
public static final String Mobile = "mobile";
public static final String Name = "name";
public static final String HospitalCode = "hospitalCode";
public static final String DueDate = "dueDate";
public static final String Activity = "activity";
public static final String YxtUserID = "yxtUserID";
public static final String Nick = "nick";
public static final String CardTypeDis = "cardTypeDis";
public static final String HospitalID = "hospitalID";
public static final String Id = "id";
public static final String YxtMemberID = "yxtMemberID";
public static final String DefaultPatientID = "defaultPatientID";
public static final String Address = "address";
public static final String LastPregnancyID = "lastPregnancyID";
public static final String CreateDate = "createDate";
public static final String CardNo = "cardNo";
public static final String MedicalNo = "medicalNo";
public static final String PatientID = "patientID"; public static final String TABLE_CREATESQL = "CREATE TABLE "
+ TABLE_NAME + " (" + "_id"
+ " INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT," + HospitalHost
+ " TEXT," + HospitalName + " TEXT," + LastModify + " TEXT,"
+ IdNo + " TEXT," + Mobile + " TEXT," + Name + " TEXT,"
+ HospitalCode + " TEXT," + DueDate + " TEXT," + Activity
+ " TEXT," + YxtUserID + " TEXT," + Nick + " TEXT,"
+ CardTypeDis + " TEXT," + HospitalID + " TEXT," + Id
+ " TEXT," + YxtMemberID + " TEXT," + DefaultPatientID
+ " TEXT," + Address + " TEXT," + LastPregnancyID + " TEXT,"
+ CreateDate + " TEXT," + CardNo + " TEXT," + MedicalNo
+ " TEXT," + PatientID + " INTEGER" + ");"; } }
ContentProvider实现流程的更多相关文章
- Android ContentProvider 启动分析
对于 ContentProvider 还不是很熟悉的同学,可以阅读上一篇 Android ContentProvider 基本原理和使用详解.本文主要是对 contentProvider 的源码进行分 ...
- Android艺术开发探索——第二章:IPC机制(下)
Android艺术开发探索--第二章:IPC机制(下) 我们继续来讲IPC机制,在本篇中你将会学习到 ContentProvider Socket Binder连接池 一.使用ContentProvi ...
- Android全面解析之Context机制
前言 很高兴遇见你~ 欢迎阅读我的文章. 在文章Android全面解析之由浅及深Handler消息机制中讨论到,Handler可以: 避免我们自己去手动写 死循环和输入阻塞 来不断获取用户的输入以及避 ...
- Contentprovider 注册 启动简单流程
安装app时packagemanager 读取manixfest获取provider信息 存在数据库里流程:1.加载ActivityThread main方法,创建消息队列.ActivityThrea ...
- Contentprovider的创建和使用流程概述
首先在provider类中继承并实现provider的几个必要方法 -- boolean onCreate(),用来执行一些初始化的工作. -- cursor query(Uri, String[], ...
- FrameWork启动流程
Android启动过程包含从Linux内核加载到Home应用程序启动的整个过程.整体流程如下: Android是基于Linux内核的系统平台.启动时,首先通过bootloader(系统加载器),加载L ...
- android Camera 数据流程分析
这篇文章主要针对其数据流程进行分析.Camera一般用于图像浏览.拍照和视频录制.这里先对图像浏览和拍照的数据流进行分析,后面再对视频电话部分进行分析. 1.针对HAL层对摄像头数据处理补充一下 Li ...
- ContentProvider初阶Cookbook
在Android世界里,ContentProvider将数据存储抽象成了类似SQL的形式,通过insert, delete, update, query等接口实现对数据的增删改查.通过ContentP ...
- Android 7.0 中 ContentProvider 实现原理
欢迎大家前往腾讯云社区,获取更多腾讯海量技术实践干货哦~ 作者:汪毅雄 导语: 本文描述了ContentProvider发布者和调用者这两在Framework层是如何实现的. 作为Android的四大 ...
随机推荐
- Web 前端开发精华文章集锦(jQuery、HTML5、CSS3)【系列十八】
<Web 前端开发精华文章推荐>2013年第六期(总第十八期)和大家见面了.梦想天空博客关注 前端开发 技术,分享各种增强网站用户体验的 jQuery 插件,展示前沿的 HTML5 和 C ...
- go环境import cycle not allowed问题处理
1.前言 今天在搭建Go语言环境,从https://golang.org/dl/上下载一个Go语言环境.环境变量配置刚开始如下配置: GOROOT=/home/go/bin go执行文件目录 ...
- Cordova webapp实战开发:(4)Android环境搭建
在<Cordova webapp实战开发:(3)后面可能会学到的东西>中我们说了一下后续大致包括的内容,今天我们继续.上周我在掌中广材集成了友盟的社交分享,今天想集成iOS应该很顺利的,但 ...
- JAVA 设计模式 享元模式
用途 享元模式 (Flyweight) 运用共享技术有效地支持大量细粒度的对象. 享元模式是一种结构型模式. 结构
- Redis Lua脚本原理
2.6版本之后支持嵌入Lua脚本,客户端使用Lua脚本,直接在服务器端原子的执行多条命令 Lua脚本执行过程 创建并修改Lua环境 1 创建基础Lua环境 2 载入函数库 3 创建全局表格Lua 4 ...
- golang中的race检测
golang中的race检测 由于golang中的go是非常方便的,加上函数又非常容易隐藏go. 所以很多时候,当我们写出一个程序的时候,我们并不知道这个程序在并发情况下会不会出现什么问题. 所以在本 ...
- Shiro —— 从一个简单的例子开始
一.Shiro是用来做权限的. 二.权限 1.基本概念: (1)安全实体:要保护的数据. (2)权限:是否有能力去操作(查看.修改.删除 )保护的数据. 2.权限的两个特性 (1)权限的继承性:A 包 ...
- Node.js系列基础学习----安装,实现Hello World, REPL
Node.js基础学习 简介 简单的说 Node.js 就是运行在服务端的 JavaScript.Node.js 是一个基于Chrome JavaScript 运行时建立的一个平台.Node.js是一 ...
- Net重温之路一
简述: 最简单的 Hello World 准备: 工具:VS2013 + SqlServer 2008 R2 我们将以.NET Framework 4.5 为基准 开始: 一:新建解决方案 > ...
- Spring发展历程总结
目前很多公司的架构,从Struts2迁移到了SpringMVC.你有想过为什么不使用Servlet+JSP来构建Java web项目,而是采用SpringMVC呢? 既然这样,我们从源头说起.Stru ...