I. propose requirements
Data in Applications A, B, and C must be shared. Therefore, content provider ContentProvider is defined in application B. application A modifies data in application B and inserts A data entry. In this case, the C application needs to receive a notification of data modification and process the corresponding operations.
Ii. Sample Code
[Java]
/**
* Operations on content providers
*
* @ Author XY
*
*/
Public class MainActivity extends Activity
{
@ Override
Public void onCreate (Bundle savedInstanceState)
{
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
}
Public void insert (View v)
{
Uri uri = Uri. parse ("content: // cn. xyCompany. providers. personProvider/person ");
ContentResolver resolver = this. getContentResolver ();
ContentValues values = new ContentValues ();
Values. put ("name", "xy_new_new ");
Values. put ("phone", "xy_new_111 ");
Resolver. insert (uri, values );
}
}
B Application
Package cn. xy. cotentProvider. app. providers;
Import android. content. ContentProvider;
Import android. content. ContentUris;
Import android. content. ContentValues;
Import android. content. UriMatcher;
Import android. database. Cursor;
Import android. database. sqlite. SQLiteDatabase;
Import android.net. Uri;
Import android. util. Log;
Import cn. xy. cotentProvider. service. DBOpeningHelper;
/**
* @ Author XY
*
*/
Public class PersonProvider extends ContentProvider
{
Private DBOpeningHelper dbHelper;
// Use UriMatcher. NO_MATCH (-1) to return the result if no match exists.
Private static final UriMatcher MATCHER = new UriMatcher (UriMatcher. NO_MATCH );
// Matching code
Private static final int CODE_NOPARAM = 1;
Private static final int CODE_PARAM = 2;
Static
{
// The URI to be matched must be in the format of cn. xyCompany. providers. personProvider/person.
// Return CODE_NOPARAM for matching,-1 for non-matching
MATCHER. addURI ("cn. xyCompany. providers. personProvider", "person", CODE_NOPARAM );
// # Indicates the number cn. xyCompany. providers. personProvider/person/10
// Return CODE_PARAM for matching and-1 for non-matching
MATCHER. addURI ("cn. xyCompany. providers. personProvider", "person/#", CODE_PARAM );
}
@ Override
Public boolean onCreate ()
{
DbHelper = new DBOpeningHelper (this. getContext ());
Return true;
}
/**
* External applications insert data to this application
*/
@ Override
Public Uri insert (Uri uri, ContentValues values)
{
SQLiteDatabase db = dbHelper. getWritableDatabase ();
Switch (MATCHER. match (uri ))
{
Case CODE_NOPARAM:
// If the primary key value is an auto-increment id value, the returned value is the primary key value. Otherwise, the value is the row number, but the row number is not the RecNo column.
Long id = db. insert ("person", "name", values );
Uri insertUri = ContentUris. withAppendedId (uri, id );
// Send a change notification (not mandatory). Set the listener to null.
// If a listener is set, no matter how many listeners exist, the listener will be able to receive the notification.
GetContext (). getContentResolver (). policychange (uri, null );
Return insertUri;
Default:
Throw new IllegalArgumentException ("this is unkown uri:" + uri );
}
}
......
}
C Application
Package cn. xt. contentProvider. lisenter;
Import android. app. Activity;
Import android. content. ContentResolver;
Import android. database. ContentObserver;
Import android. database. Cursor;
Import android.net. Uri;
Import android. OS. Bundle;
Import android. OS. Handler;
Import android. util. Log;
Public class MainActivity extends Activity
{
@ Override
Public void onCreate (Bundle savedInstanceState)
{
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
Uri uri = Uri. parse ("content: // cn. xyCompany. providers. personProvider/person ");
ContentResolver resolver = this. getContentResolver ();
Resolver. registerContentObserver (uri, true, new PersonContentObserver (new Handler ()));
}
Private class PersonContentObserver extends ContentObserver
{
Public PersonContentObserver (Handler handler)
{
Super (handler );
}
// Get the notification of data changes. This method can only roughly know the data changes and cannot determine which business operation is performing the changes.
@ Override
Public void onChange (boolean selfChange)
{
// Select * from person order by id desc limit 1 // obtain the most recently inserted value (the serial number is large --> small and the first value is obtained)
Uri uri = Uri. parse ("content: // cn. xyCompany. providers. personProvider/person ");
ContentResolver resolver = MainActivity. this. getContentResolver ();
Cursor cursor = resolver. query (uri, null, "id desc limit 1 ");
If (cursor. moveToFirst ())
{
String name = cursor. getString (cursor. getColumnIndex ("name "));
Log. I ("lisenter", name );
}
}
}