MainActivity如下:
package cn.testbaidu; import android.net.Uri; import android.os.Bundle; import android.os.Handler; 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.ContentObserver; import android.database.Cursor; /** * Demo描述: * 应用A(TestBaidu)调用另外一个应用(TestContentProvider)中的自定义ContentProvider,即: * 1 自定义ContentProvider的使用 * 2 其它应用调用该ContentProvider * 3 ContentObserver的使用 * * 备注说明: * 1 该例子在以前版本的基础上整理了代码 * 2 该例子在以前版本的基础上融合了ContentObserver的使用 * 利用ContentObserver随时监听ContentProvider的数据变化. * 为实现该功能需要在自定义的ContentProvider的insert(),update(),delete() * 方法中调用getContext().getContentResolver().notifyChange(uri, null); * 向外界通知该ContentProvider里的数据发生了变化 ,以便ContentObserver作出相应 * * 测试方法: * 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 long lastTime=0; private ContentResolver mContentResolver; private ContentObserverSubClass mContentObserverSubClass; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); init(); initContentObserver(); } 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()); } // 注册一个针对ContentProvider的ContentObserver用来观察内容提供者的数据变化 private void initContentObserver() { Uri uri = Uri.parse("content://cn.bs.testcontentprovider/person"); mContentObserverSubClass=new ContentObserverSubClass(new Handler()); this.getContentResolver().registerContentObserver(uri, true,mContentObserverSubClass); } @Override protected void onDestroy() { super.onDestroy(); if (mContentObserverSubClass!=null) { this.getContentResolver().unregisterContentObserver(mContentObserverSubClass); } } // 自定义一个内容观察者ContentObserver private class ContentObserverSubClass extends ContentObserver { public ContentObserverSubClass(Handler handler) { super(handler); } //采用时间戳避免多次调用onChange( ) @Override public void onChange(boolean selfChange) { super.onChange(selfChange); System.out.println("ContentObserver onChange() selfChange="+ selfChange); if (System.currentTimeMillis()-lastTime>2000) { ContentResolver resolver = getContentResolver(); Uri uri = Uri.parse("content://cn.bs.testcontentprovider/person"); // 获取最新的一条数据 Cursor cursor = resolver.query(uri, null, null, null,"personid desc limit 1"); while (cursor.moveToNext()) { int personid = cursor.getInt(cursor.getColumnIndex("personid")); System.out.println("内容提供者中的数据发生变化,现数据中第一条数据的personid="+ personid); } cursor.close(); lastTime=System.currentTimeMillis(); }else{ System.out.println("时间间隔过短,忽略此次更新"); } } @Override public boolean deliverSelfNotifications() { return true; } } 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/queryButton" 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/deleteButton" 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" /> <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/typeButton" 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" /> </RelativeLayout>
如有疑问请留言或者到本站社区交流讨论,感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
标签:SQLite
相关阅读 >>
分享php代码将360浏览器导出的favdb的Sqlite数据库文件转换为html
navicat premium永久激活码+激活教程 附激活补丁下载
更多相关阅读请进入《Sqlite》频道 >>

数据库系统概念 第6版
本书主要讲述了数据模型、基于对象的数据库和XML、数据存储和查询、事务管理、体系结构等方面的内容。