In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly introduces the relevant knowledge of "what is the use of Content Provider in Android". The editor shows you the operation process through an actual case. The method of operation is simple and fast, and it is practical. I hope this article "what is the use of Content Provider in Android" can help you solve the problem.
Content Provider (content provider)
In a content provider, unlike file storage, it can specify which parts it wants to share, rather than global sharing.
For every application, if you want to access the data shared in the content provider, you must use the ContentResolver class to obtain the instance through getContent-Resolver, and provide a series of CRUD (add, delete, change and search) methods in ContentResolver to operate among them.
Insert, adding data
Update, update data
Delete, delete data
Query, query data
Unlike CRUD in SQLiteDatebase,ContentResolver, which uses a Uri parameter instead, this parameter is called a content parameter and consists of two parts: authority and path. Authority is mainly used to distinguish different applications, and path is used to distinguish different tables of the same application.
For example:
Protected void onCreate (Bundle savedInstanceState) {super.onCreate (savedInstanceState); setContentView (R.layout.activity_main); Button addData = (Button) findViewById (R.id.add_data); addData.setOnClickListener (new View.OnClickListener () {@ Override public void onClick (View v) {/ / add data Uri uri = Uri.parse ("content://com.example.databasetest.provider/book"); ContentValues values = new ContentValues () Values.put ("name", "A Clash of Kings"); values.put ("author", "George Martin"); values.put ("pages", 1040); values.put ("price", 55.55); Uri newUri = getContentResolver (). Insert (uri, values); newId = newUri.getPathSegments (). Get (1);}}); Button queryData = (Button) findViewById (R.id.query_data) QueryData.setOnClickListener (new View.OnClickListener () {@ Override public void onClick (View v) {/ / query data Uri uri = Uri.parse ("content://com.example.databasetest.provider/book"); Cursor cursor = getContentResolver (). Query (uri, null, null); if (cursor! = null) {while (cursor.moveToNext ()) {String name = cursor.getString (cursor. GetColumnIndex ("name"); String author = cursor.getString (cursor. GetColumnIndex ("author"); int pages = cursor.getInt (cursor.getColumnIndex ("pages")); double price = cursor.getDouble (cursor. GetColumnIndex ("price"); Log.d ("MainActivity", "book name is" + name); Log.d ("MainActivity", "book author is" + author); Log.d ("MainActivity", "book pages is" + pages); Log.d ("MainActivity", "book price is" + price);} cursor.close ();}); Button updateData = (Button) findViewById (R.id.update_data) UpdateData.setOnClickListener (new View.OnClickListener () {@ Override public void onClick (View v) {/ / Update data Uri uri = Uri.parse ("content://com.example.databasetest.provider/book/" + newId); ContentValues values = new ContentValues (); values.put ("name", "A Storm of Swords"); values.put ("pages", 1216); values.put ("price", 24.05) GetContentResolver () .update (uri, values, null, null);}}); Button deleteData = (Button) findViewById (R.id.delete_data); deleteData.setOnClickListener (new View.OnClickListener () {@ Override public void onClick (View v) {/ / delete data Uri uri = Uri.parse ("content://com.example.databasetest.provider/book/" + newId); getContentResolver () .delete (uri, null, null) });}
It seems very simple, but is it necessarily safe? Or does everyone share data in this way, and if not? This is about creating your own content provider.
Inherit ContentProvider and implement his six abstract methods
OnCreate ()
Query (Uri uri,String [] projection,String selection,String [] selectionArgs,String sortOrder)
Insert (Uri uri,ContentValues values)
Updaya (Uri uri,ContentValues values)
Updata (Uri uri,ContenValues values,String selection,String [] selectionArgs)
Delete (Uri uri,String selection,String [] selectionArgs)
GetType (Uri uri)
This is the end of the content about "what is the use of Content Provider in Android". Thank you for your reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.