当前位置:   article > 正文

【Android】ContentProvider_android contentprovider

android contentprovider

ContentProvider做为跨进程通信的手段之一,一般用于暴露本应用数据给到外部应用,提供一套标准的数据访问模型,常常与本地数据库联合使用

  1. xml中声明provider
<provider
            android:name="包名.ProductProvider"
            android:authorities="包名.ProductProvider"
            android:enabled="true"
            android:exported="true"
            android:multiprocess="true" />
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  1. 继承ContentProvider实现增删改查的接口,定义UriMatcher,matcher名为包名+类名,访问端通过match设置好的uri进行访问
public class ProductProvider extends ContentProvider {

    private static final String MAUTHORITIESNAME="包名.ProductProvider";
    private static UriMatcher matcher=new UriMatcher(UriMatcher.NO_MATCH);
    private static final int PRODUCT=1;
    //构建URI
    static {
        matcher.addURI(MAUTHORITIESNAME,"product",PRODUCT);
    }
    @Override
    public boolean onCreate() {
       // Log.i("testProductProvider","onCreate");
        ProductDatabase.getInstance(getContext());

        return true;
    }

    @Nullable
    @Override
    public Cursor query(@NonNull Uri uri, @Nullable String[] projection, @Nullable String selection, @Nullable String[] selectionArgs, @Nullable String sortOrder) {
        Log.i("testProductProvider","query");
        int match=matcher.match(uri);
        Log.i("testProductProvider","query match:"+match);
        switch (match){
            case PRODUCT:
                return ProductDatabase.getInstance(getContext()).getProductDao().getProduct();
        }
        return  null;
    }

    @Nullable
    @Override
    public String getType(@NonNull Uri uri) {
        int match=matcher.match(uri);
        switch (match){
            case PRODUCT:
                return "vnd.android.cursor.dir/product";
        }
       return null;
    }

    @Nullable
    @Override
    public Uri insert(@NonNull Uri uri, @Nullable ContentValues values) {
        return null;
    }

    @Override
    public int delete(@NonNull Uri uri, @Nullable String selection, @Nullable String[] selectionArgs) {
        return 0;
    }

    @Override
    public int update(@NonNull Uri uri, @Nullable ContentValues values, @Nullable String selection, @Nullable String[] selectionArgs) {
        return 0;
    }
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  1. 访问数据端先设置包名可见性
<queries>
        			<package android:name="要访问的包名"/>
    		</queries>
  • 1
  • 2
  • 3

4.使用URI查询

Uri uri=Uri.parse("content://包名.类名/路径")Cursor cursor=getContentResolver().query(uri,null,null,null,null);
		//打印结果	     
		while (cursor.moveToNext()){
                            	Log.i("testCourse","id:"+cursor.getLong(0)
			+" content:"+cursor.getString(1)
                                    	+" cover:"+cursor.getString(2)
			+" type:"+cursor.getInt(3));
                       	 }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/黑客灵魂/article/detail/993229
推荐阅读
相关标签
  

闽ICP备14008679号