Android——contentProvider内容提供者

本文详细介绍了Android中的ContentProvider组件,包括其定义、创建方法及基本使用流程。通过一个具体的例子展示了如何实现数据的增删查改等功能,并提供了配置文件的设置方式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.什么是内容提供者contentProvider


2.如何创建contentProvider



需要写一个类继承contentProvider,需要重写你自己需要的方法,里面有增删查改等的方法

package com.zking.administrator.g160618_android16_sqlite;

import android.content.ContentProvider;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.Log;

import com.zking.db.DBHelper;

/**
 * Created by Administrator on 2017/7/17 0017.
 */

public class MyProvider extends ContentProvider {
    @Override
    public boolean onCreate() {
        Log.i("test","onCreate");
        return false;
    }

    @Nullable
    @Override
    public Cursor query(@NonNull Uri uri, @Nullable String[] projection, @Nullable String selection, @Nullable String[] selectionArgs, @Nullable String sortOrder) {
        Log.i("test","query");

        //把数据查询出来
        DBHelper dbHelper=new DBHelper(getContext(),"G160618.db",null,2);
        SQLiteDatabase sqLiteDatabase=dbHelper.getReadableDatabase();
        //sqLiteDatabase.rawQuery("select * from login",null);
        return sqLiteDatabase.rawQuery("select * from login",null);
    }

    @Nullable
    @Override
    public String getType(@NonNull Uri uri) {
        Log.i("test","getType");
        return null;
    }

    @Nullable
    @Override
    public Uri insert(@NonNull Uri uri, @Nullable ContentValues values) {
        Log.i("test","insert");
        return null;
    }

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

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

配置文件


<!--配置内容提供者-->
        <provider
            android:authorities="com.zking.administrator.g160618_android16_sqlite.LOGIN"
            android:name=".MyProvider"
            android:exported="true"
            ></provider>
    </application>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值