注册/取消注册ContentObserver方法,抽象类ContentResolver类中的方法原型如下:
unregisterContentObserver()去取消注册。
例子:
用来观察系统是否改变了飞行模式状态,
PS: 大家可以去SDK中查看该类:android.provider.Settings.System。该类封装了对设置模块下所有值的存取,比如:
飞行模式状态、蓝牙状态、屏幕亮度值等,并且提供了相应的Uri。
1、 观察飞行模式状态的ContentObserver派生类,AirPlaneContentObserver.java
package com.tcwzy; import android.content.Context; import android.database.ContentObserver; import android.os.Handler; import android.provider.*; import android.provider.Settings.SettingNotFoundException; import android.util.Log; public class AirPlaneContentObserver extends ContentObserver{ private static String TAG = "tcwzy"; private static int MSG_AIRPLANE = 1; private Context mContext; private Handler mHandler; public AirPlaneContentObserver(Handler handler) { super(handler); // TODO Auto-generated constructor stub } public AirPlaneContentObserver(Context context, Handler handler) { super(handler); mContext = context; mHandler = handler; } @Override public void onChange(boolean selfChange) { // TODO Auto-generated method stub super.onChange(selfChange); Log.i(TAG, "-------------the airplane mode has changed-------------"); try { int isAirPlaneOpen = Settings.System.getInt(mContext.getContentResolver(), Settings.System.AIRPLANE_MODE_ON); Log.i(TAG, " isAirplaneOpen -----> " +isAirPlaneOpen) ; mHandler.obtainMessage(MSG_AIRPLANE,isAirPlaneOpen).sendToTarget() ; } catch (SettingNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
package com.tcwzy; import android.app.Activity; import android.net.Uri; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.provider.Settings; import android.util.Log; import android.widget.TextView; public class MyContentObserverActivity extends Activity { /** Called when the activity is first created. */ private TextView airPlane; private final static int MSG_AIRPLANE = 1; private AirPlaneContentObserver airplaneCO; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); airplaneCO = new AirPlaneContentObserver(this, mHandler); registerContentObserver(); airPlane = (TextView)findViewById(R.id.airplane); } private void registerContentObserver() { Log.v("tcwzy","registerContentObserver"); Uri airPlaneUri = Settings.System.getUriFor(Settings.System.AIRPLANE_MODE_ON); Log.v("tcwzy","uri: " + airPlaneUri); this.getContentResolver().registerContentObserver(airPlaneUri, false, airplaneCO); } private Handler mHandler = new Handler() { @Override public void handleMessage(Message msg) { // TODO Auto-generated method stub super.handleMessage(msg); switch (msg.what) { case MSG_AIRPLANE: Log.v("tcwzy","handle MSG_AIRPLANE"); int isAirPlane = (Integer)msg.obj; if(isAirPlane != 0) { airPlane.setText("飞行模式已打开"); } else if (isAirPlane == 0){ airPlane.setText("飞行模式已关闭"); } break; default: break; } } } ; }
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:id="@+id/airplane" android:layout_width="fill_parent" android:layout_height="wrap_content" /> </LinearLayout>