• 自定义ContentProvider


     在内容提供者中发通知的代码
     
    ContentResolver cr = getContext().getContentResolver();
    //发出通知,所有注册在这个uri上的内容观察者都可以收到通知
    cr.notifyChange(uri, null);
     
    1.java
     
    package com.itheima.mycontentprovider;
     
    import com.itheima.mycontentprovider.db.MyOpenHelper;
     
    import android.content.ContentProvider;
    import android.content.ContentUris;
    import android.content.ContentValues;
    import android.content.UriMatcher;
    import android.database.Cursor;
    import android.database.sqlite.SQLiteDatabase;
    import android.net.Uri;
     
    public class PersonProvider extends ContentProvider {
     
    private SQLiteDatabase db;
    //创建uri匹配器
    UriMatcher um = new UriMatcher(UriMatcher.NO_MATCH);
    {
    //添加匹配规则
    //arg0:主机名
    //arg1:路径
    //arg2:匹配码
    um.addURI("com.itheima.people", "person", 1);//content://com.itheima.people/person
    um.addURI("com.itheima.people", "handsome", 2);//content://com.itheima.people/handsome
    um.addURI("com.itheima.people", "person/#", 3);//content://com.itheima.people/person/10
    }
     
    //内容提供者创建时调用
    @Override
    public boolean onCreate() {
    MyOpenHelper oh = new MyOpenHelper(getContext());
    db = oh.getWritableDatabase();
    return false;
    }
     
    //values:其他应用要插的数据
    @Override
    public Uri insert(Uri uri, ContentValues values) {
    if(um.match(uri) == 1){
    db.insert("person", null, values);
     
    //数据库改变了,内容提供者发出通知
    //arg0:通知发到哪个uri上,注册在这个uri上的内容观察者都可以收到通知
    getContext().getContentResolver().notifyChange(uri, null);
    }
    else if(um.match(uri) == 2){
    db.insert("handsome", null, values);
     
    getContext().getContentResolver().notifyChange(uri, null);
    }
    else{
    throw new IllegalArgumentException("uri传错啦傻逼");
    }
    return uri;
    }
     
    @Override
    public int delete(Uri uri, String selection, String[] selectionArgs) {
    int i = 0;
    if(um.match(uri) == 1){
    i = db.delete("person", selection, selectionArgs);
    }
    else if(um.match(uri) == 2){
    i = db.delete("handsome", selection, selectionArgs);
    }
    else{
    throw new IllegalArgumentException("uri又传错啦傻逼");
    }
     
    return i;
    }
     
    @Override
    public int update(Uri uri, ContentValues values, String selection,
    String[] selectionArgs) {
    int i = db.update("person", values, selection, selectionArgs);
    return i;
    }
     
    @Override
    public Cursor query(Uri uri, String[] projection, String selection,
    String[] selectionArgs, String sortOrder) {
    Cursor cursor = null;
    if(um.match(uri) == 1){
    cursor = db.query("person", projection, selection, selectionArgs, null, null, sortOrder, null);
    }
    else if(um.match(uri) == 2){
    cursor = db.query("handsome", projection, selection, selectionArgs, null, null, sortOrder, null);
    }
    else if(um.match(uri) == 3){
    //取出uri末尾携带的数字
    long id = ContentUris.parseId(uri);
    cursor = db.query("person", projection, "_id = ?", new String[]{"" + id}, null, null, sortOrder, null);
    }
    return cursor;
    }
     
    //返回通过指定uri获取的数据的mimetype
    @Override
    public String getType(Uri uri) {
    if(um.match(uri) == 1){
    return "vnd.android.cursor.dir/person";
    }
    else if(um.match(uri) == 2){
    return "vnd.android.cursor.dir/handsome";
    }
    else if(um.match(uri) == 3){
    return "vnd.android.cursor.item/person";
    }
    return null;
    }
    }
     
     
    2.java
     
    package com.itheima.receivenotify;
    import android.net.Uri;
    import android.os.Bundle;
    import android.os.Handler;
    import android.app.Activity;
    import android.database.ContentObserver;
    import android.view.Menu;
    public class MainActivity extends Activity {
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            
            //注册内容观察者接收01发送的通知
            getContentResolver().registerContentObserver(Uri.parse("content://com.itheima.people"), 
                    true, new ContentObserver(new Handler()) {
                        @Override
                        public void onChange(boolean selfChange) {
                            // TODO Auto-generated method stub
                            super.onChange(selfChange);
                            System.out.println("01数据库改变");
                        }
                    });
        }
        
    }
     
    3.java
     
    package com.itheima.other;
     
    import android.net.Uri;
    import android.os.Bundle;
    import android.app.Activity;
    import android.content.ContentResolver;
    import android.content.ContentValues;
    import android.database.Cursor;
    import android.view.Menu;
    import android.view.View;
     
    public class MainActivity extends Activity {
     
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
        }
     
        public void insert(View v){
         //通过内容提供者把数据插入01数据库
         //1.获取contentResolver
         ContentResolver resolver = getContentResolver();
         //2.访问内容提供者,插入数据
         ContentValues values = new ContentValues();
         values.put("name", "流氓润");
         values.put("phone", 138992);
         values.put("money", 14000);
         //arg0:指定内容提供者的主机名
         resolver.insert(Uri.parse("content://com.itheima.people/person"), values);
        
         values.clear();
         values.put("name", "侃哥");
         values.put("phone", 15999);
         //arg0:指定内容提供者的主机名
         resolver.insert(Uri.parse("content://com.itheima.people/handsome"), values);
        }
        
        public void delete(View v){
         ContentResolver resolver = getContentResolver();
         int i = resolver.delete(Uri.parse("content://com.itheima.people"), "name = ?", new String[]{"凤姐"});
         System.out.println(i);
        }
        
        public void update(View v){
         ContentResolver resolver = getContentResolver();
         ContentValues values = new ContentValues();
         values.put("money", 16001);
         int i = resolver.update(Uri.parse("content://com.itheima.people"), values, "name = ?", new String[]{"春晓"});
         System.out.println(i);
        }
        
        public void query(View v){
         ContentResolver resolver = getContentResolver();
         Cursor cursor = resolver.query(Uri.parse("content://com.itheima.people/person"), null, null, null, null);
         while(cursor.moveToNext()){
         String name = cursor.getString(1);
         String phone = cursor.getString(2);
         String money = cursor.getString(3);
         System.out.println(name + ";" + phone + ";" + money);
         }
        }
        public void queryOne(View v){
         ContentResolver resolver = getContentResolver();
         Cursor cursor = resolver.query(Uri.parse("content://com.itheima.people/person/4"), null, null, null, null);
         if(cursor.moveToNext()){
         String name = cursor.getString(1);
         String phone = cursor.getString(2);
         String money = cursor.getString(3);
         System.out.println(name + ";" + phone + ";" + money);
         }
        }
    }
     
    注意:
    每当 3.java对数据库进行增删修改操作时,1.java就会发出数据库数据变化的通知给2.java,2.java接收通知打印数据库数据发生来变化的信息
  • 相关阅读:
    Ubuntu16安装GPU版本TensorFlow(个人笔记本电脑)
    python读取shp
    python汉字转拼音
    通过Places API Web Service获取兴趣点数据
    通过修改然后commit的方式创建自己的镜像
    centos安装postgis
    centos下mongodb备份(dump)与还原(restore)
    mysql-5.7.12安装
    Buuctf-misc-snake
    Buuctf-misc-刷新过的图片 (F5刷新)
  • 原文地址:https://www.cnblogs.com/SoulCode/p/6393328.html
Copyright © 2020-2023  润新知