In the previous note, we wrote our own provider. In this note, we will access the data through the URI interface of the content provider and rewrite the example in the android learning note (step 4. Here we do not fully describe how to write the relevant UI. You can go to the Notes (step 4) To View Details, focusing on how to implement data access.
Read Information
The method of reading information, which has been introduced in Note (7). The Code is as follows:
Private voidRead(){
/* Read through managedQuery. The 1st parameter represents the URI, and the 2nd parameter represents the information to be read. The 3rd parameter represents the restriction condition, similar to the WHERE parameter in SQL; 4th parameters and 3rd parameters are used together. Specifically, the "?" parameter in the third parameter is supported." The 5th parameters are similar to order by in SQL. If you want to read a specific instance instead of collect, you can set Uri uri = ContentUris. withAppendedId (GravityProvider. Constants. CONTENT_URI, 2 );
*/
Cursor cursor =ManagedQuery(Gravityprovider. constants. content_uri, projection, null );
If (adapter = NULL ){
Adapter = new simplecursoradapter (this, R. layout. chapter_22_test1,
Cursor,
New String [] {gravityprovider. constants. Title, gravityprovider. constants. Value },
New int [] {R. Id. c22_name, R. Id. c22_gravity });
SetListAdapter (adapter );
} Else {
Adapter. changeCursor (cursor );
ConstantsCursor. close ();
}
ConstantsCursor = cursor;
}
Synchronization Information
The Content Provider can be accessed by multiple applications, the application can add or delete data, and other applications can also add or delete data. The Provider provides a notification mechanism, when the Uri points to a data change, the client is notified to allow the client to synchronize data in real time.
// Step 1 (A): define the ContentObserver object for communication processing
Private ContentObserver observer = null;
// Step 3: deregister a notification that the Uri data has changed
Protected void onPause (){
............
GetContentResolver ().UnregisterContentObserver(Observer );
Observer = null;
}
// Step 2: register and notify when the data of a Uri changes
Protected void onResume (){
............
Observer = new MyObserver ();
GetContentResolver ().RegisterContentObserver(GravityProvider. Constants. CONTENT_URI, true, observer );
}
// Step 1 (B): ContentOberver is an abstract class. We need to define the processing of notifications we receive.
Private class MyObserverExtends ContentObserver{
Public MyObserver (){
Super (new Handler ());
}
// Describes how to handle the notification received by the Provider. In this example, you only need to refresh the list.
Public voidOnChange(Boolean selfChange ){
Super. onChange (selfChange );
Read ();
}
}
Add data
The Uri of the added data must be a CollectionUri, which has two interfaces: insert () and bulkInsert (). The former is an instance, and the latter is an array.
Private void addData (DialogWrapper wrapper ){
ContentValues cv = new ContentValues (2 );
Cv. put (GravityProvider. Constants. TITLE, wrapper. getTitle ());
Cv. put (GravityProvider. Constants. VALUE, wrapper. getGravity ());
GetContentResolver (). insert (GravityProvider. Constants. CONTENT_URI, cv );
}
Delete data
Delete is used to delete data, which can be the Uri of the instance or collection.
Uri uri = ContentUris. withAppendedId (GravityProvider. Constants. CONTENT_URI, rowId );
GetContentResolver ().Delete(Uri, null, null );
Note BLOB
BLOB (Binary Large Objects), such as some image information, can be supported in many databases, including SQLite. Content Provider cannot directly access these binary data through cursor. You can use getInputStream () and getOutpurStream () on ContentProvider to read and write binary data. For an image, Andriod does not need to copy the data out and put it somewhere, and then display it. You can directly implement Uri display.
Access from other applications
We can access data in different activities in one application. In more common cases, we can access data in other applications. The data access was successful.
Related links:
My Android development articles