Android——ContentProvider

概述

  • ContentProvider为App存取内部数据提供统一的外部接口,让不同的应用之间得以共享数据。
  • ContentProvider只是服务端App存取数据的抽象类,我们需要在其基础上实现一个完整 的内容提供器,并重写下列方法:

onCreate :创建数据库并获得数据库连接。
insert :插入数据。
delete :删除数据。
update :更新数据。
query :查询数据,并返回结果集的游标。
getType:获取内容提供器支持的数据类型。

  • Uri(通用资源标识符),代表数据操作的地址,每一个Cont entProvider 都会有唯一的地址。ContentProvider 使用的Uri 语法结构如下:

content://authority/data_path/id

**content://** \]:是通用前缀,表示该Uri用于ContentProvider定位资源。 \[**authority** \]:是授权者名称,用来确定具体由哪一个ContentProvider提供资源。因此 一般authority都由 类的小写全称组成,以保证唯一性。 \[**data_path** \]:是数据路径,用来确定请求的是哪个数据集。 \[**id**\]:是数据编号,用来请求单条数据。如果是多条这个字段忽略。

  • 利用ContentProvider只实现服务端App的数据封装,如果客户端App想访问对方的内部数据,就要通过内容解析器ContentResolver访问。

Serve 内容提供者

案例代码

java 复制代码
package com.example.cpserve.provider;

import android.content.ContentProvider;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.util.Log;

import com.example.cpserve.database.UserDBHelper;

public class MyContentProvider extends ContentProvider {
    private UserDBHelper dbHelper;

    @Override
    public boolean onCreate() {
        dbHelper = UserDBHelper.getInstance(getContext());
        Log.d("MyContentProvider", "onCREATE");
        return false;
    }

    // content://com.example.cpserve.provider.MyContentProvider/user
    @Override
    public Uri insert(Uri uri, ContentValues values) {
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        db.insert(UserDBHelper.TABLE_NAME, null, values);
        return uri;
    }

    @Override
    public Cursor query(Uri uri, String[] projection, String selection,
                        String[] selectionArgs, String sortOrder) {
        SQLiteDatabase db = dbHelper.getReadableDatabase();
        return db.query(UserDBHelper.TABLE_NAME, projection, selection, selectionArgs, sortOrder, null, null, null);

    }

    @Override
    public int delete(Uri uri, String selection, String[] selectionArgs) {
        // Implement this to handle requests to delete one or more rows.
        throw new UnsupportedOperationException("Not yet implemented");
    }

    @Override
    public String getType(Uri uri) {
        // TODO: Implement this to handle requests for the MIME type of the data
        // at the given URI.
        throw new UnsupportedOperationException("Not yet implemented");
    }

    @Override
    public int update(Uri uri, ContentValues values, String selection,
                      String[] selectionArgs) {
        // TODO: Implement this to handle requests to update one or more rows.
        throw new UnsupportedOperationException("Not yet implemented");
    }
}

Client 访问者

案例代码

java 复制代码
package com.example.cpclient;

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.content.ContentProvider;
import android.content.ContentValues;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

public class ContentWriteActivity extends AppCompatActivity {
    private TextView tv_content;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_content_write);
        tv_content = findViewById(R.id.tv_content);

        findViewById(R.id.btn_save).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ContentValues values = new ContentValues();
                // cpserve里的MyContentProvider在创建表时只写了一个name字段
                values.put("name", "张三");
                getContentResolver().insert(UserInfoContent.CONTENT_URI, values);
            }
        });

        findViewById(R.id.btn_read).setOnClickListener(new View.OnClickListener() {
            @SuppressLint("Range")
            @Override
            public void onClick(View view) {
                Cursor cursor = getContentResolver().query(UserInfoContent.CONTENT_URI, null, null, null, null, null);
                if (cursor != null) {
                    StringBuilder sb = new StringBuilder();

                    while (cursor.moveToNext()) {
                        sb.append(cursor.getString(cursor.getColumnIndex("name")));
                    }
                    tv_content.setText(sb);
                }
            }
        });
    }
}

在访问者(Client)的AndroidManifest.xml里需要加入如下代码

java 复制代码
    <!-- 出于安全考虑,Android11要求应用事先说明需要访问的其他软件包 -->
    <queries>
        <!-- 第一种写法 -->
        <!-- <package android:name="com.example.cpserve"/>-->
        <!-- 第二种写法 -->
        <provider android:authorities="com.example.cpserve.provider.MyContentProvider"/>
    </queries>
相关推荐
百***920224 分钟前
【MySQL】MySQL库的操作
android·数据库·mysql
2501_916008892 小时前
没有源码如何加密 IPA 实战流程与多工具组合落地指南
android·ios·小程序·https·uni-app·iphone·webview
2501_940094023 小时前
PS1模拟器 DuckStation更新最新版整合 下载即玩 附PS1Bios/游戏/金手指 安卓版+电脑版
android·游戏·电脑
橙武低代码5 小时前
业务流低代码平台:从理念到实战
android·低代码·ai编程
空白格975 小时前
三方框架必学系列#Retrofit
android
安卓程序猿6 小时前
kotlin build.gradle.kts下修改APK的输出名称
android·kotlin·gradle
wuwu_q6 小时前
通俗易懂 + Android 开发实战的方式,详细讲讲 Kotlin 中的 StateFlow
android·开发语言·kotlin
峰哥的Android进阶之路6 小时前
Kotlin面试题总结
android·开发语言·kotlin
美摄科技6 小时前
android短视频sdk,灵活集成,快速上线!
android·音视频
佳哥的技术分享6 小时前
图形化android可视化开机观测工具bootchart
android