Android ContentObserver简单实现

ContentObserver:内容观察者,如果发现所关心的数据变化了,就进行相关操作。比如监听系统短信的收发,联系人信息变更等;

它的实现原理很简单,就是在目标内容发生变化的时候,它主动发出一条消息,告诉ContentObserver,这样便实现了该功能;具体调用getContext().getContentResolver().notifyChange(uri, null); uri表示发送消息的地址;

这里的代码,是在上一篇contentprovider代码中改进的;

效果图:


package com.example.provider;


import android.app.Activity;
import android.app.ActionBar;
import android.app.Fragment;
import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;
import android.os.Build;

public class MainActivity extends Activity {
	Handler handler = new Handler(){
		@Override
		public void handleMessage(Message msg) {
			// TODO Auto-generated method stub
			switch (msg.what) {  
            case 0:  
            	Bundle bundle = msg.getData();
                Toast.makeText(MainActivity.this, bundle.getString("key"), 0).show();    
                break;  
            case 1:  
                break;    
            default:  
                break;  
        }
		}
	};
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		Uri uri = Uri.parse("content://com.example.provider.myPro");
		getContentResolver().registerContentObserver(uri, true, new MyObserver(handler));
	}
	
	public void query(View view){
		Uri uri = Uri.parse("content://com.example.provider.myPro/query");
		Cursor c = getContentResolver().query(uri, null, null, null, null);
		while (c.moveToNext()) {
			String name = c.getString(c.getColumnIndex("name"));
			int age = c.getInt(c.getColumnIndex("age"));
			System.out.println("name:"+name+"------age:"+age);
		}
		c.close();
	}
      
    public void update(View view) {  
    	Uri uri = Uri.parse("content://com.example.provider.myPro/update");
        ContentValues values = new ContentValues();  
        values.put("name", "王五");  
        int i = getContentResolver().update(uri, values, "name = ?", new String[]{"张三"});
        System.out.println("--------更新数据,更新了"+i);  
    }  
    public void delete(View view) {  
    	Uri uri = Uri.parse("content://com.example.provider.myPro/delete");
        int i = getContentResolver().delete(uri, "name = ?", new String[]{"李四"});
        System.out.println("--------删除数据,删除了"+i);  
    }  
    
    public void insert(View view) {  
    	Uri uri = Uri.parse("content://com.example.provider.myPro/insert");
        ContentValues values = new ContentValues();  
        values.put("name", "美女");  
        values.put("age", 18);  
        getContentResolver().insert(uri, values);
        System.out.println("--------插入数据--");  
    }
}

package com.example.provider;

import android.database.ContentObserver;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;

public class MyObserver extends ContentObserver{
	Handler handler;
	public MyObserver(Handler handler) {
		super(handler);
		this.handler = handler;
	}

	@Override
	public void onChange(boolean selfChange) {
		// TODO Auto-generated method stub
		super.onChange(selfChange);
		Message msg = new Message();
		msg.what = 0;
		Bundle bundle = new Bundle();
		bundle.putString("key", "--我收到消息了,有数据变化了--");
		msg.setData(bundle);
		handler.sendMessage(msg);
		//如果是观察短信内容变化情况,就在这里查询短信的数据经常相关操作
	}

	
}

package com.example.provider;

import android.content.ContentProvider;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
/**
 * 内容提供者对象
 * @author Lenovo
 *
 */
public class MyProvider extends ContentProvider {
	//得到匹配URI对象
	private static UriMatcher matcher = new UriMatcher(UriMatcher.NO_MATCH);
	private static final int INSERT = 1;
	private static final int UPDATE = 2;
	private static final int DELETE = 3;
	private static final int QUERY = 4;
	MySQLiteOpenHelper mHelper;
	static{//初始化,增加匹配规则
		matcher.addURI("com.example.provider.myPro", "insert", INSERT);
		matcher.addURI("com.example.provider.myPro", "update", UPDATE);
		matcher.addURI("com.example.provider.myPro", "delete", DELETE);
		matcher.addURI("com.example.provider.myPro", "query", QUERY);
	}

	@Override
	public boolean onCreate() {
		// TODO Auto-generated method stub
		//初始化创建数据库操作对象
		mHelper = new MySQLiteOpenHelper(getContext());
		System.err.println("------------ContentProvider创建 了");
		return false;
	}

	@Override
	public Cursor query(Uri uri, String[] projection, String selection,
			String[] selectionArgs, String sortOrder) {
		// TODO Auto-generated method stub
		int code = matcher.match(uri);
		switch (code) {
			case QUERY:
				SQLiteDatabase database = mHelper.getReadableDatabase();
				return database.query("user", projection, selection, selectionArgs, null, null, sortOrder);
				//这里不需要关闭数据库,因为游标返回后还要使用,在外面关闭
			case UriMatcher.NO_MATCH:
				System.out.println("------------query路径不正确!");
				break;
		}
		return null;
	}

	@Override
	public String getType(Uri url) {
		return null;
	}

	@Override
	public Uri insert(Uri uri, ContentValues values) {
		// TODO Auto-generated method stub
		int code = matcher.match(uri);
		switch (code) {
			case INSERT:
				SQLiteDatabase database = mHelper.getWritableDatabase();
				database.insert("user", null, values);
				database.close();
				//发送消息到到消息邮箱,表示自己的内容变化了(配合内容监听者使用 ContentObserver)
				getContext().getContentResolver().notifyChange(uri, null);
				break;
			case UriMatcher.NO_MATCH:
				System.out.println("------------inser路径不正确!");
				break;
		}
		return null;
	}

	@Override
	public int delete(Uri uri, String selection, String[] selectionArgs) {
		int code = matcher.match(uri);
		switch (code) {
			case DELETE:
				SQLiteDatabase database = mHelper.getWritableDatabase();
				int i = database.delete("user", selection, selectionArgs);
				database.close();
				//发送消息到到消息邮箱,表示自己的内容变化了(配合内容监听者使用 ContentObserver)
				if(i != 0)//有数据改变才发送
					getContext().getContentResolver().notifyChange(uri, null);
				return i;
			case UriMatcher.NO_MATCH:
				System.out.println("------------delete路径不正确!");
				break;
		}
		return 0;
	}

	@Override
	public int update(Uri uri, ContentValues values, String selection,
			String[] selectionArgs) {
		int code = matcher.match(uri);
		switch (code) {
			case UPDATE:
				SQLiteDatabase database = mHelper.getWritableDatabase();
				int i = database.update("user", values, selection, selectionArgs);
				database.close();
				//发送消息到到消息邮箱,表示自己的内容变化了(配合内容监听者使用 ContentObserver)
				if(i != 0)//有数据改变才发送
					getContext().getContentResolver().notifyChange(uri, null);
				return i;
			case UriMatcher.NO_MATCH:
				System.out.println("------------update路径不正确!");
				break;
		}
		return 0;
	}
	
	

}

package com.example.provider;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

/**
 * 定义一个数据库操作类
 * @author GJ
 *
 */
public class MySQLiteOpenHelper extends SQLiteOpenHelper{

	public MySQLiteOpenHelper(Context context) {
		/**参数1: context 上下文
		 * 参数2:数据库名称
		 * 参数3:游标工厂(null为默认)
		 * 参数4:数据库版本(最小值为1,0会抛异常)
		 * */
		super(context, "test.db", null, 1);
		// TODO Auto-generated constructor stub
	}
	/**
	 * 数据库创建方法
	 */
	@Override
	public void onCreate(SQLiteDatabase db) {
		// TODO Auto-generated method stub
		/** 创建表,sqlite数据库不区分字段类型,底层都是varchar类型实现  */
		db.execSQL("create table user(id integer primary key autoincrement,name varchar(20),age integer)");
		//新增几条初始化数据
		db.execSQL("insert into user(name,age) values('123',12)");
		db.execSQL("insert into user(name,age) values('dsd',324)");
		db.execSQL("insert into user(name,age) values('dsd',52)");
		db.execSQL("insert into user(name,age) values('张三',32)");
		db.execSQL("insert into user(name,age) values('李四',22)");
	}
	/**
	 * 数据库更新方法
	 */
	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		
	}
	
}

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.provider.MainActivity"
    tools:ignore="MergeRootFrame" >
	<Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="查询数据"
        android:onClick="query" />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="50dp"
        android:text="插入数据"
        android:onClick="insert" />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="100dp"
        android:text="更新数据"
        android:onClick="update" />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="150dp"
        android:text="删除数据"
        android:onClick="delete" />
    

</FrameLayout>

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.provider"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="14"
        android:targetSdkVersion="19" />

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.provider.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>
		<!-- android:authorities="com.example.provider.myPro" 访问的主机名,相当于请求跟路径 -->
        <provider
            android:name="com.example.provider.MyProvider"
            android:authorities="com.example.provider.myPro" />
    </application>

</manifest>

源码下载: http://download.csdn.net/detail/qq247300948/7426211

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值