---
title: "Excessive JNI global references - Android"
author: "JonsenElizee"
date: "2011-06-12T03:11:47Z"
tags: []
categories: ["移动开发"]
slug: "excessive-jni-global-references-android"
draft: false
lastmod: "2011-06-12T03:11:47Z"
id: 1078323
url: "https://zqs.iteye.com/blog/1078323"
---

ContentObserver is used in ContentService

ObserverNode is a capsulation of ObserverEntry to provide addObserverLocked method and so on.

./frameworks/base/core/java/android/content/ContentService.java calls registerContentObserver method to register uri and responding observer.

during this registering, a ObserverNode instance would be created, so, a ObserverEntry would be created as a observer list.

./frameworks/base/services/java/com/android/server/SystemServer.java initializes ContentService via ContentService.main.

 

 

 

 

 

 

 

Here is a example for using ContentObserver.  
http://www.eoeandroid.com/forum.php?mod=viewthread&tid=9162&page=1

最近有个朋友问了我如何接受指定号码的短信，并且不让系统截取到通知用户。真好前端时间看天朝group，也有个朋友问了这个问题，而且通过ContentObserver方式解决了，我这里就把我实现的代码贴出来，以便需要的朋友参考。

   1. public class ScreenTest extends Activity {  
   2.         
   3.         class SmsContent extends ContentObserver{  
   4.                 private Cursor cursor = null;  
   5.                 public SmsContent(Handler handler) {  
   6.                         super(handler);  
   7.                 }  
   8.                 
   9.                 /\**  
  10.                  * @Description 当短信表发送改变时，调用该方法  
  11.                  *                                 需要两种权限  
  12.                  *                                 android.permission.READ\_SMS读取短信  
  13.                  *                                 android.permission.WRITE\_SMS写短信  
  14.                  * @Author Snake  
  15.                  * @Date 2010-1-12  
  16.                  \*/  
  17.                 @Override  
  18.                 public void onChange(boolean selfChange) {  
  19.                         // TODO Auto-generated method stub  
  20.                         super.onChange(selfChange);  
  21.                         //读取收件箱中指定号码的短信  
  22.                         cursor = managedQuery(Uri.parse("content://sms/inbox"),  
                                                    new String\[]{"\_id", "address", "read"},  
                                                    " address=? and read=?",  
                                                    new String\[]{"12345678901", "0"},  
                                                    "date desc");  
  23.                         
  24.                         if (cursor != null){  
  25.                                 ContentValues values = new ContentValues();  
  26.                                 values.put("read", "1");                //修改短信为已读模式  
  27.                                 cursor.moveToFirst();  
  28.                                 while (cursor.isLast()){  
  29.                                         //更新当前未读短信状态为已读  
  30.                                         getContentResolver().update(Uri.parse("content://sms/inbox"),  
                                                                          values,  
                                                                          " \_id=?",  
                                                                          new String\[]{""+cursor.getInt(0)});  
  31.                                         cursor.moveToNext();  
  32.                                 }  
  33.                         }  
  34.                 }  
  35.         }  
  36.         
  37.     /\** Called when the activity is first created. \*/  
  38.     @Override  
  39.     public void onCreate(Bundle savedInstanceState) {  
  40.         super.onCreate(savedInstanceState);  
  41.         setContentView(R.layout.main);  
  42.         SmsContent content = new SmsContent(new Handler());  
  43.         //注册短信变化监听  
  44.         this.getContentResolver().registerContentObserver(Uri.parse("content://sms/"), true, content);  
  45.     }  
  46. }