个人记录
 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实现流程的更多相关文章

  1. Android ContentProvider 启动分析

    对于 ContentProvider 还不是很熟悉的同学,可以阅读上一篇 Android ContentProvider 基本原理和使用详解.本文主要是对 contentProvider 的源码进行分 ...

  2. Android艺术开发探索——第二章:IPC机制(下)

    Android艺术开发探索--第二章:IPC机制(下) 我们继续来讲IPC机制,在本篇中你将会学习到 ContentProvider Socket Binder连接池 一.使用ContentProvi ...

  3. Android全面解析之Context机制

    前言 很高兴遇见你~ 欢迎阅读我的文章. 在文章Android全面解析之由浅及深Handler消息机制中讨论到,Handler可以: 避免我们自己去手动写 死循环和输入阻塞 来不断获取用户的输入以及避 ...

  4. Contentprovider 注册 启动简单流程

    安装app时packagemanager 读取manixfest获取provider信息 存在数据库里流程:1.加载ActivityThread main方法,创建消息队列.ActivityThrea ...

  5. Contentprovider的创建和使用流程概述

    首先在provider类中继承并实现provider的几个必要方法 -- boolean onCreate(),用来执行一些初始化的工作. -- cursor query(Uri, String[], ...

  6. FrameWork启动流程

    Android启动过程包含从Linux内核加载到Home应用程序启动的整个过程.整体流程如下: Android是基于Linux内核的系统平台.启动时,首先通过bootloader(系统加载器),加载L ...

  7. android Camera 数据流程分析

    这篇文章主要针对其数据流程进行分析.Camera一般用于图像浏览.拍照和视频录制.这里先对图像浏览和拍照的数据流进行分析,后面再对视频电话部分进行分析. 1.针对HAL层对摄像头数据处理补充一下 Li ...

  8. ContentProvider初阶Cookbook

    在Android世界里,ContentProvider将数据存储抽象成了类似SQL的形式,通过insert, delete, update, query等接口实现对数据的增删改查.通过ContentP ...

  9. Android 7.0 中 ContentProvider 实现原理

    欢迎大家前往腾讯云社区,获取更多腾讯海量技术实践干货哦~ 作者:汪毅雄 导语: 本文描述了ContentProvider发布者和调用者这两在Framework层是如何实现的. 作为Android的四大 ...

随机推荐

  1. 谈谈Android 6.0运行时权限理解

    前言 谷歌在2015年8月份时候,发布了Android 6.0版本,代号叫做“棉花糖”(Marshmallow ),其中的很大的一部分变化,是在用户权限授权上,或许是感觉之前默认授权的不合理,现在6. ...

  2. Windows Azure Virtual Machine (26) 使用高级存储(SSD)和DS系列VM

    <Windows Azure Platform 系列文章目录> Update: 2016-11-3,如果大家在使用Linux VM,使用FIO进行IOPS测试的时候,请使用以下命令: su ...

  3. java设计模式(五)--建造者模式(Builder)

    转载:http://zz563143188.iteye.com/blog/1847029 工厂类模式提供的是创建单个类的模式,而建造者模式则是将各种产品集中起来进行管理,用来创建复合对象,所谓复合对象 ...

  4. 20 个高质量响应式的 HTML/CSS 网站模板

    BisLite: Free HTML Website Templates Professional Responsive HTML5/CSS3 template Respond Wood workin ...

  5. div模拟textarea实现高度自增长

    今天突然有位前端的朋友问我textarea怎么实现高度随内容自增长,我一下子懵了,首先想到的是用js改变textarea的高度,但是百度了很多参考代码效果都不是很理想. 因为之前实际项目中用的text ...

  6. JAVA - IDEA快捷键(精简版)

    快捷键 功能 Ctrl + Alt + V 对应eclipse ctrl + l + 2 自动补全 Ctrl + Alt + L 对应eclipse ctrl + shift + o 代码格式化 Ct ...

  7. 使用NuGet打包并发布至ProGet过程 (打包再次详解)【下篇】

    一.前言 上篇[1]主要介绍了利用csproj文件使用NuGet打包至ProGet的过程,并附上了用于在Jenkins上运行的python脚本.本篇的主要内容分为以下几点: 1. Nuspec与Nup ...

  8. Java集合框架之List接口

    在上一篇Java集合框架之Collection接口中我们知道List接口是Collection接口的子接口,List接口对Collection进行了简单的扩充,List接口中的元素的特点为有序,可重复 ...

  9. Scalaz(23)- 泛函数据结构: Zipper-游标定位

    外面沙尘滚滚一直向北去了,意识到年关到了,码农们都回乡过年去了,而我却留在这里玩弄“拉链”.不要想歪了,我说的不是裤裆拉链而是scalaz Zipper,一种泛函数据结构游标(cursor).在函数式 ...

  10. 《Java4android》视频学习笔记——面向对象的应用(一)

    ---恢复内容开始--- 有一台HP打印机需要一个程序来实现开机,打印,关机这三个功能 class HPprinter { void open(){ System.out.println(" ...