In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)05/31 Report--
This article shows you how to store Android data, which is concise and easy to understand, which will definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.
ContentProvider is a mechanism for sharing data between different applications on the Android platform. An application can use this mechanism if it needs to allow other programs to manipulate its own data. And this way ignores the underlying data storage implementation, ContentProvider provides a unified way to achieve data operation through Uri. The steps are as follows:
1. Define a ContentProvider in the current application.
two。 Register this ContentProvider in the AndroidManifest.xml of the current application
3. Other applications get data for this ContentProvider through ContentResolver and Uri.
ContentResolver provides methods such as insert (), delete (), query (), and update (). It is used to access the data in ContentProvider.
Uri is a universal resource marker, which is divided into four parts:
A: standard prefixes that cannot be changed, including "content://", "tel://", etc. When the current suffix is "content://", it means that the data is controlled by a Content Provider
The identity of the B:URI, which is declared by the authorities attribute, and is used to define which ContentProvider provides the data. For third-party applications, in order to ensure the uniqueness of the URI identity, it must be a complete, lowercase class name. For example, "content://com.test.data.myprovider"
C: path, which can be approximately understood as the name of the table in the database to be operated, such as name in "content://hx.android.text.myprovider/name"
D: if the URI contains an ID; representing the record to be obtained, the data corresponding to the id is returned. If there is no ID, all is returned.
The following is a code example to demonstrate how to get data between applications.
In application A, inherit the ContProvider class and override its methods.
Public class MyProvider extends ContentProvider {@ Override public int delete (Uri uri, String selection, String [] selectionArgs) {/ / TODO Auto-generated method stub return 0;} @ Override public String getType (Uri uri) {/ / TODO Auto-generated method stub return null;} @ Override public Uri insert (Uri uri, ContentValues values) {return null Initialize a database @ Override public boolean onCreate () {SQLiteDatabase db = this.getContext () .openOrCreateDatabase ("test_db.db3", Context.MODE_PRIVATE, null) in Create; db.execSQL ("create table tab (_ id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT NOT NULL)"); ContentValues values = new ContentValues (); values.put ("name", "test") Db.insert ("tab", "_ id", values); db.close (); return true;} / implement the query method @ Override public Cursor query (Uri uri, String [] projection, String selection, String [] selectionArgs, String sortOrder) {SQLiteDatabase db = this.getContext (). OpenOrCreateDatabase ("test_db.db3", Context.MODE_PRIVATE, null) Cursor c = db.query ("tab", null,null, null,null, null,null); return c;} @ Override public int update (Uri uri, ContentValues values, String selection, String [] selectionArgs) {/ / TODO Auto-generated method stub return 0;}}
Declare this ContentProvider in its AndroidManifest.xml, where the authorities attribute defines the Uri identity of this ContentProvider.
In application B, the data in the ContentProvider of program An is obtained through ContentResolver.
Public class MainActivity extends Activity {@ Override public void onCreate (Bundle savedInstanceState) {super.onCreate (savedInstanceState); setContentView (R.layout.main); / / get context Context ctx = MainActivity.this; / / get ContentResolver object ContentResolver resolver = ctx.getContentResolver () / / get Uri object Uri uri = Uri.parse ("content://com.test.MyProvider"); / / get data Cursor c = resolver.query (uri, null, null); c.moveToFirst (); for (int iTuno; I
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.