首页 > 编程知识 正文

脚底板穴位位置详解图片,2022年属猴人运势详解最新完整版

时间:2023-05-04 02:55:11 阅读:157694 作者:4410

今天,我仔细阅读了Content Providers的官方API文档,总结了Content Providers在Android中的使用方法。

各种类型的Content Provider管理结构化数据集的读/写。 提供封装数据和确保数据安全的机制。 Content providers是用于在一个正在运行的进程和另一个进程之间传递数据的标准接口。

如果缓慢的帅哥想使用content provider读取数据,你可以在程序的上下文中将http://www.Sina.com/content resolver对象转换为provider的provider client 内容解决程序是提供程序的一种

如果不需要与其他APP应用程序共享数据,则不需要开发自己的提供程序。 但是,如果希望APP应用程序提供定制的数据检索方法,或者希望在APP应用程序之间复制和粘贴复杂的数据和文件,则可以自行开发其他提供程序。

Android本身包括各种各样的内容提供商,用于管理音频、视频、图像和联系人信息。 您可以在名为android.provider包的参考文档中了解详细的使用方法。

数据被组织成一个表。 如何从Content Provider访问?

Content Provider的基本Content Provider管理对中央数据存储的访问和访问。 内容提供商是安卓APP的一部分,提供用于自己处理数据的UI。 但是,Content Provider主要用于其他APP,并通过provider client类使用provider。 这意味着providers和provider clients提供了数据一致性和标准化的接口,处理进程之间的通信和数据安全性。

本文主要介绍以下基础知识。

1、内容提供商的工作原理

2、内容提供商提供数据检索的API

3、content provider提供数据插入、更新、删除的API

4、其他API功能

content provider为外APP应用程序提供的数据格式类似于关系数据库中的表。 表中的每一行表示一种类型的实例,而行中的每一列表示该实例的数据。

例如,user dictionary是Android平台的嵌入式提供程序,用于存储用户希望存储的非标准拼写单词。 表-1显示了提供程序表的格式。

综述

APP应用程序使用名为ContentResolver的客户端对象访问content provider中的数据。 此对象中的方法名称与提供程序中的方法名称相同,是ContentProvider的具体子类的实例,ContentProvider方法具有基本的“创建、检索、更新和删除”功能和持久性存储

英语水平有限。 这个词不知道该怎么表达,就直接放到原文里吧。

thecontentresolverobjectintheclientapplication ' sprocessandthecontentproviderobjectintheapplicationthatownstheproviderautomatom tion.contentprovideralsoactsasanabstractionlayerbetweenitsrepositoryofdataandtheexternalappearanceopearance

例如,要从User Dictionary Provider检索单词及其本地环境数据,请调用ContentResolver.query ()。 query (方法是由User Dictionary Provider定义的ContentProvider.query )方法。 以下代码显示了ContentProvider.query ()调用。

1//queriestheuserdictionaryandreturnsresults2mcursor=get content resolver (.query (3user dictionary.words.content ) ) //thecontenturiofthewordstable4mprojection,//thecolumnstoreturnforeachrow5ms elec

tionClause // Selection criteria6 mSelectionArgs, // Selection criteria7 mSortOrder); // The sort order for the returned rows

表-2展示了 query(Uri,projection,selection,selectionArgs,sortOrder)中各参数对应的SQL SELECT 语句:

Content URIs

 

 Content URI是Provider中用来识别数据的URI(统一资源标识符)。 Content URIs包括整个Provider的符号名(它的身份认证)和一个指向表的名字(一个路径)。缓慢的帅哥调用一个client method访问Provider中的表时,表的content URI是其中的一个参数。

 在上面的几行代码中,CONTENT_URI包含了user dictionary的单词表的content URI。ContentResolver对象解析URI的身份认证,用这个身份认证和系统中已知的provider列表比较,ContentResolver把正确的查询请求参数传给对应的Provider。

ContentProvider使用Content URI中的部分路径选择要访问的表。一个Provider通常拥有所有可用表的路径。

在前面的代码中,单词表完整的URI路径是:

content://user_dictionary/words user_dictionary是provider的身份认证, words是表的路径,content://表示这是一个Content URI。
许多Provider允许你通过在URI结尾附加ID值访问表中的某一行。例如:检索user dictionary中_ID值为4的一行,你可以使用这个Content URI:
Uri singleUri = ContentUris.withAppendedId(UserDictionary.Words.CONTENT_URI,4);

 检索Provider中的数据

 

检索Provider中的数据有以下两个基本的步骤:

1、获得Provider的访问权限

2、向Provider发送请求

 

获取权限

访问权限必须在manifest中使用<uses-permission>加上Provider的权限名来获取。Provider详细的访问权限名在Provider的文档中。

例如:User Dictionary Provider的访问权限在mainifest文件中定义为android.permission.READ_USER_DICTIONARY,所以应用程序想访问这个Provider时,就必须获得这个权限。

构造请求

第二步就是构造要发送给Provider的请求了。下面第一段代码是访问User Dictionary Provider时要定义的一些变量:

// A "projection" defines the columns that will be returned for each rowString[] mProjection ={ UserDictionary.Words._ID, // Contract class constant for the _ID column name UserDictionary.Words.WORD, // Contract class constant for the word column name UserDictionary.Words.LOCALE // Contract class constant for the locale column name};// Defines a string to contain the selection clauseString mSelectionClause = null;// Initializes an array to contain selection argumentsString[] mSelectionArgs = {""};

接下来,下面将要看到的第二段代码以User Dictionary Provider为例展示了如何使用ContentResolver.query()。一个provider client请求相当于一条SQL语句的请求,它包含一组选择和排序的标准,返回值是表中的一组列。

这些返回的列被称为“projection(the variable mProjection)”

对检索行进行指定的表示式可分为“选择字句”和“选择参数”,“选择字句”是一些逻辑和无辜的仙人掌表达式、列名、一些值(mSelectionClause)的组合。如果你指定用参数?来替代一个值,查询方法从选择参数数组(mSelectionArgs)中检索值。

在第二段代码中,如果你不输入单词,“选择字句”是空的,查询操作返回Provider中的所有单词。如果你输入单词,“选择字句”被设置为UserDictionary.Words.WORD + " = ?"并且选择数组(mSelectionArgs)中的第一个元素被设置为用户所输入的单词。

/* * This defines a one-element String array to contain the selection argument. */String[] mSelectionArgs = {""};// Gets a word from the UImSearchString = mSearchWord.getText().toString();// Remember to insert code here to check for invalid or malicious input.// If the word is the empty string, gets everythingif (TextUtils.isEmpty(mSearchString)) { // Setting the selection clause to null will return all words mSelectionClause = null; mSelectionArgs[0] = "";} else { // Constructs a selection clause that matches the word that the user entered. mSelectionClause = UserDictionary.Words.WORD + " = ?"; // Moves the user's input string to the selection arguments. mSelectionArgs[0] = mSearchString;}// Does a query against the table and returns a Cursor objectmCursor = getContentResolver().query( UserDictionary.Words.CONTENT_URI, // The content URI of the words table mProjection, // The columns to return for each row mSelectionClause // Either null, or the word the user entered mSelectionArgs, // Either empty, or the string the user entered mSortOrder); // The sort order for the returned rows// Some providers return null if an error occurs, others throw an exceptionif (null == mCursor) { /* * Insert code here to handle the error. Be sure not to use the cursor! You may want to * call android.util.Log.e() to log this error. * */// If the Cursor is empty, the provider found no matches} else if (mCursor.getCount() < 1) { /* * Insert code here to notify the user that the search was unsuccessful. This isn't necessarily * an error. You may want to offer the user the option to insert a new row, or re-type the * search term. */} else { // Insert code here to do something with the results}

这条查询类似于SQL语句:

SELECT _ID, word, locale FROM words WHERE word = <userinput> ORDER BY word ASC;

防止恶意输入

查询结果的展示

ContentResolver.query()的client方法总是返回一个Cursor,它包含着满足查询条件的行。一个Cursor对象提供对行和列的随机访问,使用cursor可以遍历结果集中的每一行,

 

 













转载于:https://www.cnblogs.com/sage-blog/p/3912691.html

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。