android 使用contentobserver监听数据库内容变化

在android中经常会用到改变数据库内容后再去使用数据库更新的内容,很多人会重新去query一遍,但是这样的问题就是程序会特别占内存,而且有可能会搂关cursor而导致程序内存未释放等等。其实android内部提供了一种ContentObserver的东西来监听数据库内容的变化。


ContentObserver的构造函数需要一个参数Hanlder,因为ContentObserver内部使用了一个实现Runnable接口的内部类NotificationRunnable,来实现数据库内容的变化。需要使用hanlder去post消息。注册ContentObserver的方法是:getContentResolver().registerContentObserver(uri, notifyForDescendents, observer).
上面3个参数为:

uri----Uri类型,是需要监听的数据库的uri.
notifyForDescendents---boolean  true的话就会监听所有与此uri相关的uri。false的话则是直接特殊的uri才会监听。一般都设置为true.
observer-----ContentObserver  就是需要的contentobserver.


初始化一个ContentObserver对象,重载onChange(boolean ),在这个方法里去操作数据库的使用,针对变化后的使用。
写了一个小demo,可以参考下。提示这种监听方式必须是contentprovider才能使用,因为contentprovider有uri.简单的那种sqlite数据库没有uri是使用不了的。
下面demo操作的是在一个activityA里点击button跳转至另外一个activityB,在B中点击button往数据库中加数据,加完后关闭B回到A。A的button的文字自动变化设置到数据库中的字符串。

[java]  view plain copy print ? 在CODE上查看代码片 派生到我的代码片
  1. package ty.com.lto;  
  2.   
  3.   
  4.   
  5. import android.app.Activity;  
  6.   
  7. import android.content.Intent;  
  8.   
  9. import android.database.ContentObserver;  
  10.   
  11. import android.os.Bundle;  
  12.   
  13. import android.os.Handler;  
  14.   
  15. import android.view.View;  
  16.   
  17. import android.widget.Button;  
  18.   
  19.   
  20.   
  21. public class ListenDataTest extends Activity{  
  22.   
  23.     private Button testBtn;  
  24.   
  25.   
  26.   
  27.         @Override  
  28.   
  29.         protected void onCreate(Bundle savedInstanceState) {  
  30.   
  31.                 super.onCreate(savedInstanceState);  
  32.   
  33.                 setContentView(R.layout.listen_data_test);  
  34.   
  35.                 getContentResolver().registerContentObserver(DataChangeProvider.CONTENT_URI,  
  36.   
  37.                                 true, cob);  
  38.   
  39.                   
  40.   
  41.                 testBtn = (Button)findViewById(R.id.test_btn);  
  42.   
  43.                 testBtn.setOnClickListener(new View.OnClickListener() {  
  44.   
  45.                           
  46.   
  47.                         public void onClick(View v) {  
  48.   
  49.                                 Intent in = new Intent(ListenDataTest.this,DataChangeTest.class);  
  50.   
  51.                                 startActivity(in);  
  52.   
  53.                                   
  54.   
  55.                         }  
  56.   
  57.                 });  
  58.   
  59.                   
  60.   
  61.         }  
  62.   
  63.           
  64.   
  65.         private ContentObserver cob = new ContentObserver(new Handler()) {  
  66.   
  67.   
  68.   
  69.                 @Override  
  70.   
  71.                 public boolean deliverSelfNotifications() {  
  72.   
  73.                         return super.deliverSelfNotifications();  
  74.   
  75.                 }  
  76.   
  77.   
  78.   
  79.                 @Override  
  80.   
  81.                 public void onChange(boolean selfChange) {  
  82.   
  83.                         super.onChange(selfChange);  
  84.   
  85.                         testBtn.setText(DataUtils.getChangeName(getApplicationContext()));  
  86.   
  87.                 }  
  88.   
  89.                    
  90.   
  91.          };  
  92.   
  93.   
  94.   
  95.         @Override  
  96.   
  97.         protected void onDestroy() {  
  98.   
  99.                 super.onDestroy();  
  100.   
  101.                 getContentResolver().unregisterContentObserver(cob);  
  102.   
  103.         }  
  104.   
  105.        
  106.   
  107.            
  108.   
  109. }  

 

<div class="dp-highlighter bg_java" style="font-family: Consolas, 'Courier New', Courier, mono, serif; font-size: 12px; width: 693px; overflow: auto; padding-top: 1px; margin: 18px 0px !important; background-color: rgb(231, 229, 220);"><div class="bar" style="padding-left: 45px;"><div class="tools" style="padding: 3px 8px 10px 10px; font-stretch: normal; font-size: 9px; line-height: normal; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; color: silver; border-left-width: 3px; border-left-style: solid; border-left-color: rgb(153, 153, 153); background-color: rgb(248, 248, 248);"><strong>[java]</strong> <a target=_blank href="http://blog.csdn.net/stoppig/article/details/21740731#" class="ViewSource" title="view plain" style="color: rgb(160, 160, 160); text-decoration: none; border: none; padding: 0px; margin: 0px 10px 0px 0px; font-size: 9px; background-image: none; background-attachment: initial; background-color: inherit; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;">view plain</a><a target=_blank href="http://blog.csdn.net/stoppig/article/details/21740731#" class="CopyToClipboard" title="copy" style="color: rgb(160, 160, 160); text-decoration: none; border: none; padding: 0px; margin: 0px 10px 0px 0px; font-size: 9px; background-image: none; background-attachment: initial; background-color: inherit; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;">copy</a><a target=_blank href="http://blog.csdn.net/stoppig/article/details/21740731#" class="PrintSource" title="print" style="color: rgb(160, 160, 160); text-decoration: none; border: none; padding: 0px; margin: 0px 10px 0px 0px; font-size: 9px; background-image: none; background-attachment: initial; background-color: inherit; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;">print</a><a target=_blank href="http://blog.csdn.net/stoppig/article/details/21740731#" class="About" title="?" style="color: rgb(160, 160, 160); text-decoration: none; border: none; padding: 0px; margin: 0px 10px 0px 0px; font-size: 9px; background-image: none; background-attachment: initial; background-color: inherit; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;">?</a><a target=_blank href="https://code.csdn.net/snippets/249534" target="_blank" title="在CODE上查看代码片" style="color: rgb(160, 160, 160); text-decoration: none; border: none; padding: 0px; margin: 0px 10px 0px 0px; font-size: 9px; background-image: none; background-attachment: initial; background-color: inherit; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;"><img src="https://code.csdn.net/assets/CODE_ico.png" width="12" height="12" alt="在CODE上查看代码片" style="border: none; max-width: 100%; position: relative; top: 1px; left: 2px;" /></a><a target=_blank href="https://code.csdn.net/snippets/249534/fork" target="_blank" title="派生到我的代码片" style="color: rgb(160, 160, 160); text-decoration: none; border: none; padding: 0px; margin: 0px 10px 0px 0px; font-size: 9px; background-image: none; background-attachment: initial; background-color: inherit; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;"><img src="https://code.csdn.net/assets/ico_fork.svg" width="12" height="12" alt="派生到我的代码片" style="border: none; max-width: 100%; position: relative; top: 2px; left: 2px;" /></a><div style="position: absolute; left: 607px; top: 3166px; width: 27px; height: 15px; z-index: 99;"></div></div></div><ol start="1" class="dp-j" style="padding: 0px; border: none; color: rgb(92, 92, 92); margin: 0px 0px 1px 45px !important; background-color: rgb(255, 255, 255);"><li class="alt" style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; color: inherit; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"><span class="keyword" style="margin: 0px; padding: 0px; border: none; color: blue; font-weight: bold; background-color: inherit;">package</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> ty.com.lto;  </span></span></li><li style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; color: rgb(85, 85, 85); line-height: 18px; background-color: rgb(248, 248, 248);"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">  </span></li><li class="alt" style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; color: inherit; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">  </span></li><li style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; color: rgb(85, 85, 85); line-height: 18px; background-color: rgb(248, 248, 248);"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">  </span></li><li class="alt" style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; color: inherit; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"><span class="keyword" style="margin: 0px; padding: 0px; border: none; color: blue; font-weight: bold; background-color: inherit;">import</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> android.app.Activity;  </span></span></li><li style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; color: rgb(85, 85, 85); line-height: 18px; background-color: rgb(248, 248, 248);"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">  </span></li><li class="alt" style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; color: inherit; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"><span class="keyword" style="margin: 0px; padding: 0px; border: none; color: blue; font-weight: bold; background-color: inherit;">import</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> android.content.ContentValues;  </span></span></li><li style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; color: rgb(85, 85, 85); line-height: 18px; background-color: rgb(248, 248, 248);"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">  </span></li><li class="alt" style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; color: inherit; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"><span class="keyword" style="margin: 0px; padding: 0px; border: none; color: blue; font-weight: bold; background-color: inherit;">import</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> android.content.Intent;  </span></span></li><li style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; color: rgb(85, 85, 85); line-height: 18px; background-color: rgb(248, 248, 248);"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">  </span></li><li class="alt" style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; color: inherit; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"><span class="keyword" style="margin: 0px; padding: 0px; border: none; color: blue; font-weight: bold; background-color: inherit;">import</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> android.database.ContentObservable;  </span></span></li><li style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; color: rgb(85, 85, 85); line-height: 18px; background-color: rgb(248, 248, 248);"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">  </span></li><li class="alt" style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; color: inherit; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"><span class="keyword" style="margin: 0px; padding: 0px; border: none; color: blue; font-weight: bold; background-color: inherit;">import</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> android.database.ContentObserver;  </span></span></li><li style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; color: rgb(85, 85, 85); line-height: 18px; background-color: rgb(248, 248, 248);"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">  </span></li><li class="alt" style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; color: inherit; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"><span class="keyword" style="margin: 0px; padding: 0px; border: none; color: blue; font-weight: bold; background-color: inherit;">import</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> android.os.Bundle;  </span></span></li><li style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; color: rgb(85, 85, 85); line-height: 18px; background-color: rgb(248, 248, 248);"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">  </span></li><li class="alt" style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; color: inherit; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"><span class="keyword" style="margin: 0px; padding: 0px; border: none; color: blue; font-weight: bold; background-color: inherit;">import</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> android.os.Handler;  </span></span></li><li style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; color: rgb(85, 85, 85); line-height: 18px; background-color: rgb(248, 248, 248);"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">  </span></li><li class="alt" style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; color: inherit; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"><span class="keyword" style="margin: 0px; padding: 0px; border: none; color: blue; font-weight: bold; background-color: inherit;">import</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> android.view.View;  </span></span></li><li style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; color: rgb(85, 85, 85); line-height: 18px; background-color: rgb(248, 248, 248);"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">  </span></li><li class="alt" style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; color: inherit; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"><span class="keyword" style="margin: 0px; padding: 0px; border: none; color: blue; font-weight: bold; background-color: inherit;">import</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> android.widget.Button;  </span></span></li><li style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; color: rgb(85, 85, 85); line-height: 18px; background-color: rgb(248, 248, 248);"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">  </span></li><li class="alt" style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; color: inherit; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">  </span></li><li style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; color: rgb(85, 85, 85); line-height: 18px; background-color: rgb(248, 248, 248);"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">  </span></li><li class="alt" style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; color: inherit; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;"><span class="keyword" style="margin: 0px; padding: 0px; border: none; color: blue; font-weight: bold; background-color: inherit;">public</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> </span><span class="keyword" style="margin: 0px; padding: 0px; border: none; color: blue; font-weight: bold; background-color: inherit;">class</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> DataChangeTest </span><span class="keyword" style="margin: 0px; padding: 0px; border: none; color: blue; font-weight: bold; background-color: inherit;">extends</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> Activity{  </span></span></li><li style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; color: rgb(85, 85, 85); line-height: 18px; background-color: rgb(248, 248, 248);"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">  </span></li><li class="alt" style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; color: inherit; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">     <span class="keyword" style="margin: 0px; padding: 0px; border: none; color: blue; font-weight: bold; background-color: inherit;">private</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> Button dataBtn;  </span></span></li><li style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; color: rgb(85, 85, 85); line-height: 18px; background-color: rgb(248, 248, 248);"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">  </span></li><li class="alt" style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; color: inherit; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">     DataSqlite mDataSqlite;  </span></li><li style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; color: rgb(85, 85, 85); line-height: 18px; background-color: rgb(248, 248, 248);"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">  </span></li><li class="alt" style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; color: inherit; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">     <span class="annotation" style="margin: 0px; padding: 0px; border: none; color: rgb(100, 100, 100); background-color: inherit;">@Override</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">  </span></span></li><li style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; color: rgb(85, 85, 85); line-height: 18px; background-color: rgb(248, 248, 248);"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">  </span></li><li class="alt" style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; color: inherit; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">     <span class="keyword" style="margin: 0px; padding: 0px; border: none; color: blue; font-weight: bold; background-color: inherit;">protected</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> </span><span class="keyword" style="margin: 0px; padding: 0px; border: none; color: blue; font-weight: bold; background-color: inherit;">void</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> onCreate(Bundle savedInstanceState) {  </span></span></li><li style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; color: rgb(85, 85, 85); line-height: 18px; background-color: rgb(248, 248, 248);"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">  </span></li><li class="alt" style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; color: inherit; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">        <span class="keyword" style="margin: 0px; padding: 0px; border: none; color: blue; font-weight: bold; background-color: inherit;">super</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">.onCreate(savedInstanceState);  </span></span></li><li style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; color: rgb(85, 85, 85); line-height: 18px; background-color: rgb(248, 248, 248);"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">  </span></li><li class="alt" style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; color: inherit; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">        setContentView(R.layout.data_change_test);  </span></li><li style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; color: rgb(85, 85, 85); line-height: 18px; background-color: rgb(248, 248, 248);"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">  </span></li><li class="alt" style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; color: inherit; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">        dataBtn = (Button)findViewById(R.id.data_test_btn);  </span></li><li style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; color: rgb(85, 85, 85); line-height: 18px; background-color: rgb(248, 248, 248);"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">  </span></li><li class="alt" style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; color: inherit; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">        mDataSqlite = <span class="keyword" style="margin: 0px; padding: 0px; border: none; color: blue; font-weight: bold; background-color: inherit;">new</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> DataSqlite(</span><span class="keyword" style="margin: 0px; padding: 0px; border: none; color: blue; font-weight: bold; background-color: inherit;">this</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">);  </span></span></li><li style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; color: rgb(85, 85, 85); line-height: 18px; background-color: rgb(248, 248, 248);"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">  </span></li><li class="alt" style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; color: inherit; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">        dataBtn.setOnClickListener(<span class="keyword" style="margin: 0px; padding: 0px; border: none; color: blue; font-weight: bold; background-color: inherit;">new</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> View.OnClickListener() {  </span></span></li><li style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; color: rgb(85, 85, 85); line-height: 18px; background-color: rgb(248, 248, 248);"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">  </span></li><li class="alt" style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; color: inherit; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">              </span></li><li style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; color: rgb(85, 85, 85); line-height: 18px; background-color: rgb(248, 248, 248);"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">  </span></li><li class="alt" style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; color: inherit; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">            <span class="keyword" style="margin: 0px; padding: 0px; border: none; color: blue; font-weight: bold; background-color: inherit;">public</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> </span><span class="keyword" style="margin: 0px; padding: 0px; border: none; color: blue; font-weight: bold; background-color: inherit;">void</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> onClick(View v) {  </span></span></li><li style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; color: rgb(85, 85, 85); line-height: 18px; background-color: rgb(248, 248, 248);"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">  </span></li><li class="alt" style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; color: inherit; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">                ContentValues con = <span class="keyword" style="margin: 0px; padding: 0px; border: none; color: blue; font-weight: bold; background-color: inherit;">new</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;"> ContentValues();  </span></span></li><li style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; color: rgb(85, 85, 85); line-height: 18px; background-color: rgb(248, 248, 248);"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">  </span></li><li class="alt" style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; color: inherit; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">                con.put(<span class="string" style="margin: 0px; padding: 0px; border: none; color: red; background-color: inherit;">"name"</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">, </span><span class="string" style="margin: 0px; padding: 0px; border: none; color: red; background-color: inherit;">"数据变化了"</span><span style="margin: 0px; padding: 0px; border: none; background-color: inherit;">);  </span></span></li><li style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; color: rgb(85, 85, 85); line-height: 18px; background-color: rgb(248, 248, 248);"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">  </span></li><li class="alt" style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; color: inherit; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">                getContentResolver().insert(DataChangeProvider.CONTENT_URI, con);  </span></li><li style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; color: rgb(85, 85, 85); line-height: 18px; background-color: rgb(248, 248, 248);"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">  </span></li><li class="alt" style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; color: inherit; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">                finish();  </span></li><li style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; color: rgb(85, 85, 85); line-height: 18px; background-color: rgb(248, 248, 248);"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">  </span></li><li class="alt" style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; color: inherit; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">            }  </span></li><li style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; color: rgb(85, 85, 85); line-height: 18px; background-color: rgb(248, 248, 248);"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">  </span></li><li class="alt" style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; color: inherit; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">        });  </span></li><li style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; color: rgb(85, 85, 85); line-height: 18px; background-color: rgb(248, 248, 248);"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">  </span></li><li class="alt" style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; color: inherit; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">     }  </span></li><li style="margin: 0px !important; padding: 0px 3px 0px 10px !important; border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; color: rgb(85, 85, 85); line-height: 18px; background-color: rgb(248, 248, 248);"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">  </span></li><li class="alt" style="border-style: none none none solid; border-left-width: 3px; border-left-color: rgb(153, 153, 153); list-style: decimal-leading-zero outside; color: inherit; line-height: 18px; margin: 0px !important; padding: 0px 3px 0px 10px !important;"><span style="margin: 0px; padding: 0px; border: none; color: black; background-color: inherit;">}  </span></li></ol></div>

 

[java]  view plain copy print ? 在CODE上查看代码片 派生到我的代码片
  1. package ty.com.lto;  
  2.   
  3.    
  4.   
  5.    
  6.   
  7. import android.content.ContentProvider;  
  8.   
  9. import android.content.ContentUris;  
  10.   
  11. import android.content.ContentValues;  
  12.   
  13. import android.content.Context;  
  14.   
  15. import android.content.UriMatcher;  
  16.   
  17. import android.database.Cursor;  
  18.   
  19. import android.database.SQLException;  
  20.   
  21. import android.database.sqlite.SQLiteDatabase;  
  22.   
  23. import android.database.sqlite.SQLiteOpenHelper;  
  24.   
  25. import android.database.sqlite.SQLiteQueryBuilder;  
  26.   
  27. import android.database.sqlite.SQLiteDatabase.CursorFactory;  
  28.   
  29. import android.net.Uri;  
  30.   
  31. import android.text.TextUtils;  
  32.   
  33.    
  34.   
  35. public class DataChangeProvider extends ContentProvider{  
  36.   
  37.     private SQLiteOpenHelper mOpenHelper;  
  38.   
  39.     private static final int ALARMS = 1;  
  40.   
  41.     private static final int ALARMS_ID = 2;  
  42.   
  43.     private static final UriMatcher sURLMatcher = new UriMatcher(UriMatcher.NO_MATCH);  
  44.   
  45.     public static final Uri CONTENT_URI = Uri.parse("content://ty.com.lto/test");   
  46.   
  47.    
  48.   
  49.     static {  
  50.   
  51.         sURLMatcher.addURI("ty.com.lto""test", ALARMS);  
  52.   
  53.         sURLMatcher.addURI("ty.com.lto""test/#", ALARMS_ID);  
  54.   
  55.     }  
  56.   
  57.       
  58.   
  59.     private static class DatabaseHelper extends SQLiteOpenHelper{  
  60.   
  61.          private static final String TEST_DATABASE = "test.db";  
  62.   
  63.          private static final int VERSION = 1;  
  64.   
  65.            
  66.   
  67.          public DatabaseHelper(Context context) {  
  68.   
  69.              super(context, TEST_DATABASE, null, VERSION);  
  70.   
  71.              // TODO Auto-generated constructor stub  
  72.   
  73.          }  
  74.   
  75.             
  76.   
  77.    
  78.   
  79.          @Override  
  80.   
  81.          public void onCreate(SQLiteDatabase db) {  
  82.   
  83.              String sql = "CREATE TABLE "+"test"+" (" +  
  84.   
  85.                      "_id INTEGER PRIMARY KEY," +  
  86.   
  87.                      "name TEXT "+   
  88.   
  89.                       ");";  
  90.   
  91.              db.execSQL(sql);  
  92.   
  93.          }  
  94.   
  95.    
  96.   
  97.          @Override  
  98.   
  99.          public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {  
  100.   
  101.              String sql = "DROP TABLE IF EXIST "+TEST_DATABASE;  
  102.   
  103.              db.execSQL(sql);  
  104.   
  105.              onCreate(db);  
  106.   
  107.          }  
  108.   
  109.            
  110.   
  111.     }  
  112.   
  113.       
  114.   
  115.     public DataChangeProvider() {  
  116.   
  117.     }  
  118.   
  119.       
  120.   
  121.     @Override  
  122.   
  123.     public int delete(Uri url, String where, String[] whereArgs) {  
  124.   
  125.         SQLiteDatabase db = mOpenHelper.getWritableDatabase();  
  126.   
  127.         int count;  
  128.   
  129.         long rowId = 0;  
  130.   
  131.         switch (sURLMatcher.match(url)) {  
  132.   
  133.             case ALARMS:  
  134.   
  135.                 count = db.delete("test", where, whereArgs);  
  136.   
  137.                 break;  
  138.   
  139.             case ALARMS_ID:  
  140.   
  141.                 String segment = url.getPathSegments().get(1);  
  142.   
  143.                 rowId = Long.parseLong(segment);  
  144.   
  145.                 if (TextUtils.isEmpty(where)) {  
  146.   
  147.                     where = "_id=" + segment;  
  148.   
  149.                 } else {  
  150.   
  151.                     where = "_id=" + segment + " AND (" + where + ")";  
  152.   
  153.                 }  
  154.   
  155.                 count = db.delete("test", where, whereArgs);  
  156.   
  157.                 break;  
  158.   
  159.             default:  
  160.   
  161.                 throw new IllegalArgumentException("Cannot delete from URL: " + url);  
  162.   
  163.         }  
  164.   
  165.    
  166.   
  167.         getContext().getContentResolver().notifyChange(url, null);  
  168.   
  169.         return count;  
  170.   
  171.     }  
  172.   
  173.    
  174.   
  175.     @Override  
  176.   
  177.     public String getType(Uri url) {  
  178.   
  179.         int match = sURLMatcher.match(url);  
  180.   
  181.         switch (match) {  
  182.   
  183.             case ALARMS:  
  184.   
  185.                 return "vnd.android.cursor.dir/alarms";  
  186.   
  187.             case ALARMS_ID:  
  188.   
  189.                 return "vnd.android.cursor.item/alarms";  
  190.   
  191.             default:  
  192.   
  193.                 throw new IllegalArgumentException("Unknown URL");  
  194.   
  195.         }  
  196.   
  197.     }  
  198.   
  199.    
  200.   
  201.     @Override  
  202.   
  203.     public Uri insert(Uri url, ContentValues initialValues) {  
  204.   
  205.         if (sURLMatcher.match(url) != ALARMS) {  
  206.   
  207.             throw new IllegalArgumentException("Cannot insert into URL: " + url);  
  208.   
  209.         }  
  210.   
  211.    
  212.   
  213.         ContentValues values;  
  214.   
  215.         if (initialValues != null) {  
  216.   
  217.             values = new ContentValues(initialValues);  
  218.   
  219.         } else {  
  220.   
  221.             values = new ContentValues();  
  222.   
  223.         }  
  224.   
  225.               
  226.   
  227.         if (!values.containsKey("name"))  
  228.   
  229.             values.put("name""");  
  230.   
  231.    
  232.   
  233.         SQLiteDatabase db = mOpenHelper.getWritableDatabase();  
  234.   
  235.         long rowId = db.insert("test"null, values);  
  236.   
  237.         if (rowId < 0) {  
  238.   
  239.             throw new SQLException("Failed to insert row into " + url);  
  240.   
  241.         }  
  242.   
  243.         Uri newUrl = ContentUris.withAppendedId(CONTENT_URI, rowId);  
  244.   
  245.         getContext().getContentResolver().notifyChange(newUrl, null);  
  246.   
  247.         return newUrl;  
  248.   
  249.     }  
  250.   
  251.    
  252.   
  253.     @Override  
  254.   
  255.     public boolean onCreate() {  
  256.   
  257.         mOpenHelper = new DatabaseHelper(getContext());  
  258.   
  259.         return true;  
  260.   
  261.     }  
  262.   
  263.    
  264.   
  265.     @Override  
  266.   
  267.     public Cursor query(Uri url, String[] projection, String where,  
  268.   
  269.             String[] whereArgs, String sortOrder) {  
  270.   
  271.         SQLiteQueryBuilder qb = new SQLiteQueryBuilder();  
  272.   
  273.         int match = sURLMatcher.match(url);  
  274.   
  275.         switch (match) {  
  276.   
  277.             case ALARMS:  
  278.   
  279.                 qb.setTables("test");  
  280.   
  281.                 break;  
  282.   
  283.             case ALARMS_ID:  
  284.   
  285.                 qb.setTables("test");  
  286.   
  287.                 qb.appendWhere("_id=");  
  288.   
  289.                 qb.appendWhere(url.getPathSegments().get(1));  
  290.   
  291.                 break;  
  292.   
  293.             default:  
  294.   
  295.                 throw new IllegalArgumentException("Unknown URL " + url);  
  296.   
  297.         }  
  298.   
  299.    
  300.   
  301.         SQLiteDatabase db = mOpenHelper.getReadableDatabase();  
  302.   
  303.         Cursor cur = qb.query(db, projection, where, whereArgs,nullnull, sortOrder);  
  304.   
  305.         if (cur != null) {  
  306.   
  307.             cur.setNotificationUri(getContext().getContentResolver(), url);  
  308.   
  309.                
  310.   
  311.         }   
  312.   
  313.         return cur;  
  314.   
  315.     }  
  316.   
  317.    
  318.   
  319.     @Override  
  320.   
  321.     public int update(Uri url, ContentValues values, String where,String[] whereArgs) {  
  322.   
  323.         int count;  
  324.   
  325.         long rowId = 0;  
  326.   
  327.         int match = sURLMatcher.match(url);  
  328.   
  329.         SQLiteDatabase db = mOpenHelper.getWritableDatabase();  
  330.   
  331.         switch (match) {  
  332.   
  333.             case ALARMS_ID: {  
  334.   
  335.                 String segment = url.getPathSegments().get(1);  
  336.   
  337.                 rowId = Long.parseLong(segment);  
  338.   
  339.                 count = db.update("test", values, "_id=" + rowId, null);  
  340.   
  341.                 break;  
  342.   
  343.             }  
  344.   
  345.             default: {  
  346.   
  347.                 throw new UnsupportedOperationException(  
  348.   
  349.                         "Cannot update URL: " + url);  
  350.   
  351.             }  
  352.   
  353.         }  
  354.   
  355.         getContext().getContentResolver().notifyChange(url, null);  
  356.   
  357.         return count;  
  358.   
  359.     }  
  360.   
  361.    
  362.   
  363. }  

 

[html]  view plain copy print ? 在CODE上查看代码片 派生到我的代码片
  1. <?xml version="1.0" encoding="utf-8"?>  
  2.   
  3. <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
  4.   
  5.       package="ty.com.lto"  
  6.   
  7.       android:versionCode="1"  
  8.   
  9.       android:versionName="1.0"  
  10.   
  11.       >  
  12.   
  13.     <application android:icon="@drawable/icon" android:label="@string/app_name">  
  14.   
  15.     <provider android:name="DataChangeProvider" android:authorities="ty.com.lto"/>  
  16.   
  17.         <activity android:name=".ListenDataTest"  
  18.   
  19.                   android:label="@string/app_name">  
  20.   
  21.             <intent-filter>  
  22.   
  23.                 <action android:name="android.intent.action.MAIN" />  
  24.   
  25.                 <category android:name="android.intent.category.LAUNCHER" />  
  26.   
  27.             </intent-filter>  
  28.   
  29.         </activity>  
  30.   
  31.     <activity android:name=".DataChangeTest" android:label="@string/app_name"/>  
  32.   
  33.     </application>  
  34.   
  35. <uses-permission android:name="android.permission.INTERNET"/>  
  36.   
  37. <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>  
  38.   
  39.   
  40.   
  41. </manifest>  

转载自:http://dev.10086.cn/cmdn/bbs/viewthread.php?tid=37427&extra=page%3D1%26amp%3Bfilter%3Dtype%26amp%3Btypeid%3D67

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值