mainactivity如下:
package cn.testbaidu; import android.net.uri; import android.os.bundle; import android.view.view; import android.view.view.onclicklistener; import android.widget.button; import android.app.activity; import android.content.contentresolver; import android.content.contentvalues; import android.database.cursor; /** * demo描述: * 应用a(testbaidu)调用另外一个应用(testcontentprovider) * 中的自定义contentprovider,即: * 1 自定义contentprovider的使用 * 2 其它应用调用该contentprovider * * 测试方法: * 1 依次测试contentprovider的增查删改(注意该顺序) * 2 其它应用查询该contentprovider的数据 * */ public class mainactivity extends activity { private button maddbutton; private button mdeletebutton; private button mupdatebutton; private button mquerybutton; private button mtypebutton; private contentresolver mcontentresolver; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.main); init(); } private void init() { mcontentresolver=this.getcontentresolver(); maddbutton=(button) findviewbyid(r.id.addbutton); maddbutton.setonclicklistener(new clicklistenerimpl()); mdeletebutton=(button) findviewbyid(r.id.deletebutton); mdeletebutton.setonclicklistener(new clicklistenerimpl()); mupdatebutton=(button) findviewbyid(r.id.updatebutton); mupdatebutton.setonclicklistener(new clicklistenerimpl()); mquerybutton=(button) findviewbyid(r.id.querybutton); mquerybutton.setonclicklistener(new clicklistenerimpl()); mtypebutton=(button) findviewbyid(r.id.typebutton); mtypebutton.setonclicklistener(new clicklistenerimpl()); } private class clicklistenerimpl implements onclicklistener{ @override public void onclick(view v) { switch (v.getid()) { case r.id.addbutton: person person=null; for (int i = 0; i < 5; i++) { person=new person("xiaoming"+i, "9527"+i,(8888+i)); testinsert(person); } break; case r.id.deletebutton: testdelete(1); break; case r.id.updatebutton: testupdate(3); break; case r.id.querybutton: //查询表 //queryfromcontentprovider(-1); //查询personid=2的数据 testquery(2); break; case r.id.typebutton: testtype(); break; default: break; } } } private void testinsert(person person) { contentvalues contentvalues=new contentvalues(); contentvalues.put("name", person.getname()); contentvalues.put("phone", person.getphone()); contentvalues.put("salary",person.getsalary()); uri inserturi=uri.parse("content://cn.bs.testcontentprovider/person"); uri returnuri=mcontentresolver.insert(inserturi, contentvalues); system.out.println("新增数据:returnuri="+returnuri); } private void testdelete(int index){ uri uri=uri.parse("content://cn.bs.testcontentprovider/person/"+string.valueof(index)); mcontentresolver.delete(uri, null, null); } private void testupdate(int index){ uri uri=uri.parse("content://cn.bs.testcontentprovider/person/"+string.valueof(index)); contentvalues values=new contentvalues(); values.put("name", "hanmeimei"); values.put("phone", "1234"); values.put("salary", 333); mcontentresolver.update(uri, values, null, null); } private void testquery(int index) { uri uri=null; if (index<=0) { //查询表 uri=uri.parse("content://cn.bs.testcontentprovider/person"); } else { //按照id查询某条数据 uri=uri.parse("content://cn.bs.testcontentprovider/person/"+string.valueof(index)); } //对应上面的:查询表 //cursor cursor= mcontentresolver.query(uri, null, null, null, null); //对应上面的:查询personid=2的数据 //注意:因为name是varchar字段的,所以应该写作"name='xiaoming1'" // 若写成"name=xiaoming1"查询时会报错 cursor cursor= mcontentresolver.query(uri, null, "name='xiaoming1'", null, null); while(cursor.movetonext()){ int personid=cursor.getint(cursor.getcolumnindex("personid")); string name=cursor.getstring(cursor.getcolumnindex("name")); string phone=cursor.getstring(cursor.getcolumnindex("phone")); int salary=cursor.getint(cursor.getcolumnindex("salary")); system.out.println("查询得到:personid=" + personid+",name="+name+",phone="+phone+",salary="+salary); } cursor.close(); } private void testtype(){ uri diruri=uri.parse("content://cn.bs.testcontentprovider/person"); string dirtype=mcontentresolver.gettype(diruri); system.out.println("dirtype:"+dirtype); uri itemuri=uri.parse("content://cn.bs.testcontentprovider/person/3"); string itemtype=mcontentresolver.gettype(itemuri); system.out.println("itemtype:"+itemtype); } }
person如下:
package cn.testbaidu; public class person { private integer id; private string name; private string phone; private integer salary; public person(string name, string phone,integer salary) { this.name = name; this.phone = phone; this.salary=salary; } public person(integer id, string name, string phone,integer salary) { this.id = id; this.name = name; this.phone = phone; this.salary=salary; } public integer getid() { return id; } public void setid(integer id) { this.id = id; } public string getname() { return name; } public void setname(string name) { this.name = name; } public string getphone() { return phone; } public void setphone(string phone) { this.phone = phone; } public integer getsalary() { return salary; } public void setsalary(integer salary) { this.salary = salary; } @override public string tostring() { return "person [id=" + id + ", name=" + name + ", phone=" + phone+ ", salary=" + salary + "]"; } }
main.xml如下:
<relativelayout 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" > <button android:id="@+id/addbutton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerhorizontal="true" android:layout_margintop="30dip" android:text="增加" android:textsize="20sp" /> <button android:id="@+id/deletebutton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerhorizontal="true" android:layout_margintop="30dip" android:layout_below="@id/addbutton" android:text="删除" android:textsize="20sp" /> <button android:id="@+id/updatebutton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerhorizontal="true" android:layout_margintop="30dip" android:layout_below="@id/deletebutton" android:text="修改" android:textsize="20sp" /> <button android:id="@+id/querybutton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerhorizontal="true" android:layout_margintop="30dip" android:layout_below="@id/updatebutton" android:text="查询" android:textsize="20sp" /> <button android:id="@+id/typebutton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerhorizontal="true" android:layout_margintop="30dip" android:layout_below="@id/querybutton" android:text="类型" android:textsize="20sp" /> </relativelayout>
//以下为testcontentprovider
mainactivity如下:
package cn.testcontentprovider; import android.app.activity; import android.os.bundle; public class mainactivity extends activity { @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.main); } }
contentprovidertest如下:
package cn.testcontentprovider; 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; /** * 注意事项: * 在androidmanifest.xml中注册contentprovider时的属性 * android:exported="true"表示允许其他应用访问. * 这样testbaidu这个应用才可以访问该处的contentprovider */ public class contentprovidertest extends contentprovider { private dbopenhelper dbopenhelper; private urimatcher uri_matcher; private static final int persons = 0; private static final int person = 1; @override public boolean oncreate() { initurimatcher(); dbopenhelper=new dbopenhelper(getcontext()); return true; } //初始化urimatcher private void initurimatcher(){ uri_matcher=new urimatcher(urimatcher.no_match); //表示返回所有的person,其中persons为该特定uri的标识码 uri_matcher.adduri("cn.bs.testcontentprovider","person", persons); //表示返回某一个person,其中person为该特定uri的标识码 uri_matcher.adduri("cn.bs.testcontentprovider","person/#", person); } /** * 插入操作: * 插入操作只有一种可能:向一张表中插入 * 返回结果为新增记录对应的uri * 方法db.insert()返回结果为新增记录对应的主键值 */ @override public uri insert(uri uri, contentvalues values) { sqlitedatabase db=dbopenhelper.getwritabledatabase(); switch (uri_matcher.match(uri)) { case persons: long rowid=db.insert("person", "name,phone,salary", values); return contenturis.withappendedid(uri, rowid); default: throw new illegalargumentexception("unknown uri"+uri.tostring()); } } /** * 更新操作: * 更新操作有两种可能:更新一张表或者更新某条数据 * 在更新某条数据时原理类似于查询某条数据,见下. */ @override public int update(uri uri, contentvalues values, string selection, string[] selectionargs) { sqlitedatabase db=dbopenhelper.getwritabledatabase(); int updatanum=0; switch (uri_matcher.match(uri)) { //更新表 case persons: updatanum=db.update("person", values, selection, selectionargs); break; //按照id更新某条数据 case person: long id=contenturis.parseid(uri); string where="personid="+id; if(selection!=null&&!"".equals(selection.trim())){ where=selection+" and "+where; } updatanum=db.update("person", values, where, selectionargs); break; default: throw new illegalargumentexception("unknown uri"+uri.tostring()); } return updatanum; } /** * 删除操作: * 删除操作有两种可能:删除一张表或者删除某条数据 * 在删除某条数据时原理类似于查询某条数据,见下. */ @override public int delete(uri uri, string selection, string[] selectionargs) { sqlitedatabase db=dbopenhelper.getwritabledatabase(); int deletednum=0; switch (uri_matcher.match(uri)) { //删除表 case persons: deletednum=db.delete("person", selection, selectionargs); break; //按照id删除某条数据 case person: long id=contenturis.parseid(uri); string where="personid="+id; if(selection!=null&&!"".equals(selection.trim())){ where=selection+" and "+where; } deletednum=db.delete("person", where, selectionargs); break; default: throw new illegalargumentexception("unknown uri"+uri.tostring()); } return deletednum; } /** * 查询操作: * 查询操作有两种可能:查询一张表或者查询某条数据 * 注意事项: * 在查询某条数据时要注意--因为此处是按照personid来查询 * 某条数据,但是同时可能还有其他限制.例如: * 要求personid为2且name为xiaoming1 * 所以在查询时分为两步: * 第一步: * 解析出personid放入where查询条件 * 第二部: * 判断是否有其他限制(如name),若有则将其 * 组拼到where查询条件. * 详细代码见下. */ @override public cursor query(uri uri, string[] projection, string selection, string[] selectionargs, string sortorder) { sqlitedatabase db=dbopenhelper.getwritabledatabase(); cursor cursor; switch (uri_matcher.match(uri)) { //查询表 case persons: cursor=db.query("person", projection, selection, selectionargs, null, null, sortorder); break; //按照id查询某条数据 case person: //第一步: long id=contenturis.parseid(uri); string where="personid="+id; //第二步: if(selection!=null&&!"".equals(selection.trim())){ where=selection+" and "+where; } cursor=db.query("person", projection, where, selectionargs, null, null, sortorder); break; default: throw new illegalargumentexception("unknown uri"+uri.tostring()); } return cursor; } /** * 返回当前uri所代表的数据的mime类型. * 如果该uri对应的数据可能包含多条记录,那么返回 * 字符串应该以"vnd.android.cursor.dir/"开头 * 如果该uri对应的数据只包含一条记录,那么返回 * 字符串应该以"vnd.android.cursor.item/"开头 */ @override public string gettype(uri uri) { switch (uri_matcher.match(uri)) { case persons: return "vnd.android.cursor.dir/persons"; case person: return "vnd.android.cursor.item/person"; default: throw new illegalargumentexception("unknown uri"+uri.tostring()); } } }
dbopenhelper如下:
package cn.testcontentprovider; import android.content.context; import android.database.sqlite.sqlitedatabase; import android.database.sqlite.sqliteopenhelper; public class dbopenhelper extends sqliteopenhelper { public dbopenhelper(context context) { super(context, "contentprovidertest.db", null, 1); } @override public void oncreate(sqlitedatabase db) { db.execsql("create table person(personid integer primary key autoincrement,name varchar(20),phone varchar(12),salary integer(12))"); } //当数据库版本号发生变化时调用该方法 @override public void onupgrade(sqlitedatabase db, int arg1, int arg2) { //db.execsql("alter table person add phone varchar(12) null"); //db.execsql("alter table person add salary integer null"); } }
androidmanifest.xml如下:
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="cn.testcontentprovider" android:versioncode="1" android:versionname="1.0" > <uses-sdk android:minsdkversion="8" android:targetsdkversion="8" /> <uses-permission android:name="android.permission.internet" /> <uses-permission android:name="android.permission.access_network_state" /> <uses-permission android:name="android.permission.write_external_storage" /> <uses-permission android:name="android.permission.mount_unmount_filesystems" /> <application android:allowbackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/apptheme" > <activity android:name="cn.testcontentprovider.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="cn.testcontentprovider.contentprovidertest" android:authorities="cn.bs.testcontentprovider" android:exported="true" /> </application> </manifest>
更多android中自定义contentprovider实例。
