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的四大 ...
随机推荐
- Javascript: 从prototype漫谈到继承(2)
本文同时也发表在我另一篇独立博客 <Javascript: 从prototype漫谈到继承(2)>(管理员请注意!这两个都是我自己的原创博客!不要踢出首页!不是转载!已经误会三次了!) 上 ...
- equals和==的区别 你真的掌握了吗?
PS:最近读Java编程思想的时候发现了一些小问题.就是equals方法和==,感觉自己是真正掌握了,其实并没有.简单的记录一下. 学习内容: 1.equals 和 == 的区别 equals和== ...
- 七、L2CAP
1. L2CAP 在BR/EDR模式下,在connection procedure成功执行后,两台设备通过一条物理信道(physical channel)连接在一起,同时两者之间建立起了一条 ...
- Insert Plain Text and Images into RichTextBox at Runtime
Insert Plain Text and Images into RichTextBox at Runtime' https://www.codeproject.com/Articles/4544/ ...
- Servlet生命周期中的service方法分析
问题ServletLifeCycle中的service方法内,有super.service(request, response); 会执行this.doGet(HttpServletRequest r ...
- 他答对一半(打一字)asp.net开源简答题项目
先出个字谜: 他答对一半(打一字) 你猜出来了没? 可以到这个网址答题:http://m.hovertree.com/miyu/bjae/4fpmm2td.htm 看你的答案是否正确. 这是ASP.N ...
- javascript事件分类解析
最近在学习javascript,就顺便把常用事件给大家整理整理,也让自己加深印象.不足之处欢迎各位补充. 一般事件 onclick 鼠标点击时触发此事件 ondblclick 鼠标双击时触发此事件 ...
- Nancy 学习-视图引擎 继续跨平台
前面一篇,讲解Nancy的基础,以及Nancy自宿主,现在开始学习视图引擎. Nancy 目前支持两种 一个是SSVE 一个是Razor.下面我们一起学习. The Super Simple View ...
- Jquery请求Ajax的json数据
获得单个json对象 $.getJSON("test.js", { name: "John", time: "2pm" }, funct ...
- winform自定义日期控件,要求可以手动输入日期DatePicker
要求:文本框中能手动输入数字,向上箭头根据鼠标位置给年月日递增,向下箭头递减 一:页面加载时: private void FlatDatePicker_Load(object sender, Even ...