android - Android 的自定义内容提供程序? (修改记事本示例)

标签 android android-manifest android-intent android-contentprovider

我正在尝试学习内容提供程序的实现及其工作原理。我尝试了 Android SDK 中的示例记事本应用程序,一切正常。我能够创建新笔记并保存和编辑它们。

我尝试将此内容提供者更改为自定义实现。这工作正常。但是我不明白下面的实现

1) 为什么 Notepad.java 在文件夹“com.example.android.notepad”中定义两次,然后在“com.google.provider”中定义两次。

2) Intent 类型“content://com.vinod.provider.NotePad/notes”如何进入列表,而“content://com.vinod.provider.NotePad/notes/2”如何进入编辑 Activity ?这是如何控制的?

3) 在 list 中我看到类似“vnd.android.cursor.dir/vnd.google.note”的 mime 类型。 vnd.android.cursor.dir 和 vnd.android.cursor.item 代表什么。什么是“vnd.google.note”

有人可以解释一下这些问题吗?感谢您的宝贵时间和帮助。

最佳答案

1)两个文件

它们是两个不同的类 -- com.example.android.notepad 是一个实现应用程序的类。 com.google.provider.notepad 是一个实现 ContentProvider 的类。

应用程序提供用户界面,ContentProvider 提供数据存储。

查看This link to the Notepad ContentProvider example ,它的命名更加清晰。


2)URI匹配

引用 NotepadProvider.java :

  sUriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
  sUriMatcher.addURI(NotePad.AUTHORITY, "notes", NOTES);
  sUriMatcher.addURI(NotePad.AUTHORITY, "notes/#", NOTE_ID);

UriMatcher 采用模式列表。 NotePad.AUTHORITY 是内容 URI 的基础。

"notes" 是一种匹配模式,返回枚举 NOTES。 "notes/#" 是一种匹配模式,返回枚举 NOTE_ID。

switch (sUriMatcher.match(uri)) {
case NOTES:
   ...
   break;

case NOTES:
   ...
   break;

default:
   ...
   break;
}

是根据URI决定运行不同代码的模式


3) MIME 类型

MIME 类型是可选的。您可能不需要对它们执行任何操作。

关于android - Android 的自定义内容提供程序? (修改记事本示例),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6852552/

相关文章:

java - 无法实例化 Activity ComponentInfo 错误(已检查 list )

android - 相机 resultCode 返回 0

java - 无法解析方法 'isIntentAvailable(android.content.intent)'

android - 触发警报时启动 Activity

android - Android Studio:尝试查找未在build.gradle中声明的版本的建筑

安卓 : launching diff activities under TabWidget

Android : autocompletetextview, 建议列表显示在 TextView 上方?

java - 如何在android本地数据库中存储intent?

android - 为 Android (Ubuntu14) 编译 FFMpeg - 找不到符号

java - Android - 将位图分配给 ImageView 会更改大小