ANDROID中vnd.android.cursor的解释

本文深入解析Android ContentProvider中的自定义MIME类型概念,包括其作用、形式、常见用例及如何在实际开发中应用。

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

学习到Content Provider,遇到自定义MIME类型vnd.android.cursor,一直看不懂是什么。其实vnd是vendor-specific的意思,下面是我好不容易找到的详细解释,分析给大家。

引用MIME类型Contentprovider可以返回标准的MIME类型或自定义的MIME类型字符串,或同时返回两者。

MIME类型具有以下形式:type/subtype。

例如,世人皆知的MIME类型text/html具有text类型和html子类型。如果provider从一个URI返回此类型,这表示面向此URI的查询将返回带有HTML标记的文本。

自定义的MIME字符串,也被叫做"vendor-specific"MIME类型,具有更复杂的类型和子类型值,此类型的值总是这样

vnd.android.cursor.dir。用于多行,或这样

vnd.android.cursor.item。用于单行。

而子类型是每个provider都不相同的。Android内置的provider通常具有一个间单的字类型。例如,当联系人应用为一个电话号码创建一个新行时,它为新行设置下面的MIME类型:

vnd.android.cursor.item/phone_v2

可以看到子类型的值只是简单的phone_v2.

其它provider开发者可能基于provider的authority和表名字创建他们自己的子类型式样。例如,假设一个provider含有列车时刻表,provider的authority是com.example.trains,并且它包含三个表:线路1,线路2,线路3。在URI的响应中,

content://com.example.trains/Line1。指向线路1表,provider返回对应的MIME类型为:

vnd.android.cursor.dir/vnd.example.line1

而content URI:content://com.example.trains/Line2/5。指向线路2表的第5行,provider返回的对应的MIME类型为:

vnd.android.cursor.item/vnd.example.line2

大多数contentproviders定义了契约类来包含它们所用到的MIME类型。例如,联系人Provider的契约类ContactsContract.RawContacts,定义了常量CONTENT_ITEM_TYPE,它对应于一行原始的联系人数据


<activity android:name=".opp.BluetoothOppLauncherActivity" android:process="@string/process" android:theme="@android:style/Theme.Material.Light.Dialog" android:label="@string/bt_share_picker_label" android:excludeFromRecents="true" android:configChanges="orientation|keyboardHidden|screenSize" android:enabled="@bool/profile_supported_opp"> <intent-filter> <action android:name="android.intent.action.SEND" /> <category android:name="android.intent.category.DEFAULT" /> <data android:mimeType="image/*" /> <data android:mimeType="video/*" /> <data android:mimeType="audio/*" /> <data android:mimeType="text/x-vcard" /> <data android:mimeType="text/x-vcalendar" /> <data android:mimeType="text/calendar" /> <data android:mimeType="text/plain" /> <data android:mimeType="text/html" /> <data android:mimeType="text/xml" /> <data android:mimeType="application/zip" /> <data android:mimeType="application/vnd.ms-excel" /> <data android:mimeType="application/msword" /> <data android:mimeType="application/vnd.ms-powerpoint" /> <data android:mimeType="application/pdf" /> <data android:mimeType="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" /> <data android:mimeType="application/vnd.openxmlformats-officedocument.wordprocessingml.document" /> <data android:mimeType="application/vnd.openxmlformats-officedocument.presentationml.presentation" /> <data android:mimeType="application/x-hwp" /> <data android:mimeType="application/ogg" /> <data android:mimeType="application/mspowerpoint" /> <data android:mimeType="application/vnd.android.package-archive" /> </intent-filter> <intent-filter> <action android:name="android.intent.action.SEND_MULTIPLE" /> <category android:name="android.intent.category.DEFAULT" /> <data android:mimeType="image/*" /> <data android:mimeType="video/*" /> <data android:mimeType="audio/*" /> <data android:mimeType="x-mixmedia/*" /> <data android:mimeType="text/x-vcard" /> <data android:mimeType="text/x-vcalendar" /> <data android:mimeType="text/plain" /> <data android:mimeType="application/zip" /> <data android:mimeType="application/msword" /> <data android:mimeType="application/vnd.ms-excel" /> <data android:mimeType="application/vnd.ms-powerpoint" /> <data android:mimeType="application/vnd.openxmlformats-officedocument.wordprocessingml.document" /> <data android:mimeType="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" /> <data android:mimeType="application/vnd.openxmlformats-officedocument.presentationml.presentation" /> <data android:mimeType="application/pdf" /> <data android:mimeType="application/ogg" /> <data android:mimeType="application/mspowerpoint" /> </intent-filter> <intent-filter> <action android:name="android.btopp.intent.action.OPEN" /> <category android:name="android.intent.category.DEFAULT" /> <data android:mimeType="vnd.android.cursor.item/vnd.android.btopp" /> </intent-filter> </activity>
07-11
把以下Android代码翻译成Java版本:class DatabaseProvider : ContentProvider() { private val bookDir = 0 private val bookItem = 1 private val categoryDir = 2 private val categoryItem = 3 private val authority = "com.example.databasetest.provider" private var dbHelper: MyDatabaseHelper? = null private val uriMatcher by lazy { val matcher = UriMatcher(UriMatcher.NO_MATCH) matcher.addURI(authority, "book", bookDir) matcher.addURI(authority, "book/#", bookItem) matcher.addURI(authority, "category", categoryDir) matcher.addURI(authority, "category/#", categoryItem) matcher } override fun onCreate() = context?.let { dbHelper = MyDatabaseHelper(it, "BookStore.db", 2) true } ?: false www.blogss.cn override fun query(uri: Uri, projection: Array<String>?, selection: String?, selectionArgs: Array<String>?, sortOrder: String?) = dbHelper?.let { // 查询数据 val db = it.readableDatabase val cursor = when (uriMatcher.match(uri)) { bookDir -> db.query("Book", projection, selection, selectionArgs, null, null, sortOrder) bookItem -> { val bookId = uri.pathSegments[1] db.query("Book", projection, "id = ?", arrayOf(bookId), null, null, sortOrder) } categoryDir -> db.query("Category", projection, selection, selectionArgs, null, null, sortOrder) categoryItem -> { val categoryId = uri.pathSegments[1] db.query("Category", projection, "id = ?", arrayOf(categoryId), null, null, sortOrder) } else -> null } cursor } override fun insert(uri: Uri, values: ContentValues?) = dbHelper?.let { // 添加数据 val db = it.writableDatabase val uriReturn = when (uriMatcher.match(uri)) { bookDir, bookItem -> { val newBookId = db.insert("Book", null, values) Uri.parse("content://$authority/book/$newBookId") } categoryDir, categoryItem -> { val newCategoryId = db.insert("Category", null, values) Uri.parse("content://$authority/category/$newCategoryId") } else -> null } uriReturn } override fun update(uri: Uri, values: ContentValues?, selection: String?, selectionArgs: Array<String>?) = dbHelper?.let { // 更新数据 val db = it.writableDatabase val updatedRows = when (uriMatcher.match(uri)) { bookDir -> db.update("Book", values, selection, selectionArgs) bookItem -> { val bookId = uri.pathSegments[1] db.update("Book", values, "id = ?", arrayOf(bookId)) } categoryDir -> db.update("Category", values, selection, selectionArgs) categoryItem -> { val categoryId = uri.pathSegments[1] db.update("Category", values, "id = ?", arrayOf(categoryId)) } else -> 0 } updatedRows } ?: 0 override fun delete(uri: Uri, selection: String?, selectionArgs: Array<String>?) = dbHelper?.let { // 删除数据 www.blogss.cn val db = it.writableDatabase val deletedRows = when (uriMatcher.match(uri)) { bookDir -> db.delete("Book", selection, selectionArgs) bookItem -> { val bookId = uri.pathSegments[1] db.delete("Book", "id = ?", arrayOf(bookId)) } categoryDir -> db.delete("Category", selection, selectionArgs) categoryItem -> { val categoryId = uri.pathSegments[1] db.delete("Category", "id = ?", arrayOf(categoryId)) } else -> 0 } deletedRows } ?: 0 override fun getType(uri: Uri) = when (uriMatcher.match(uri)) { bookDir -> "vnd.android.cursor.dir/vnd.com.example.databasetest.provider.book" bookItem -> "vnd.android.cursor.item/vnd.com.example.databasetest.provider.book" categoryDir -> "vnd.android.cursor.dir/vnd.com.example.databasetest. provider.category" categoryItem -> "vnd.android.cursor.item/vnd.com.example.databasetest. provider.category" else -> null } }
最新发布
07-22
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值