First, add the content provider code to the application that needs to provide the content provider.
Package com. example. sqllite;
Import com. example. sqllite. servise. dbopenhelp;
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;
Public class personprovider extends contentprovider {
Private dbopenhelp;
// URI matching to check whether the request matches
Private Static final urimatcher matcher = new urimatcher (urimatcher. no_match );
Private Static final int success = 1;
Static {
Matcher. adduri ("com. contentprovide. providers. personprovides", "person", success );
}
@ Override
Public Boolean oncreate (){
Dbopenhelp = new dbopenhelp (this. getcontext ());
Return true;
}
@ Override
Public int Delete (URI Uri, string selection, string [] selectionargs ){
// Todo auto-generated method stub
Return 0;
}
@ Override
Public String GetType (URI ){
// Todo auto-generated method stub
Return NULL;
}
@ Override
Public URI insert (URI Uri, contentvalues values ){
Sqlitedatabase DB = dbopenhelp. getwritabledatabase ();
Switch (matcher. Match (URI )){
Case success:
Long rowid = dB. insert ("person", "name", values );
// URI inserturi = URI. parse ("content: // com. contentprovide. providers. personprovides/person/" + rowid );
Uri inserturi = contenturis. withappendedid (Uri, rowid );
DB. Close ();
Return inserturi;
Default:
Throw new illegalargumentexception ("unknown UIR ");
}
}
@ Override
Public cursor query (URI Uri, string [] projection, string selection,
String [] selectionargs, string sortorder ){
// Todo auto-generated method stub
Return NULL;
}
@ Override
Public int Update (URI Uri, contentvalues values, string selection,
String [] selectionargs ){
// Todo auto-generated method stub
Return 0;
}
}
2. Add configuration items to the original application
<? XML version = "1.0" encoding = "UTF-8"?>
<Manifest xmlns: Android = "http://schemas.android.com/apk/res/android"
Package = "com. example. sqllite"
Android: versioncode = "1"
Android: versionname = "1.0" type = "codeph" text = "/codeph">
<Uses-SDK
Android: minsdkversion = "8"
Android: targetsdkversion = "18"/>
<Application
Android: allowbackup = "true"
Android: icon = "@ drawable/ic_launcher"
Android: Label = "@ string/app_name"
Android: theme = "@ style/apptheme">
<Activity
Android: Name = "com. example. sqllite. mainactivity"
Android: Label = "@ string/app_name">
<Intent-filter>
<Action Android: Name = "android. Intent. Action. Main"/>
<Category Android: Name = "android. Intent. Category. launcher"/>
</Intent-filter>
</Activity>
<! -- Add content provider configuration -->
<Provider Android: Name = ". personprovider" Android: Authorities = "com. contentprovide. providers. personprovides"/>
</Application>
</Manifest>
3. access in the new application
Uri uri = URI. parse ("content: // com. contentprovide. providers. personprovides/person ");
// Access the help class of contentprovider
Contentresolver resolver = This. getapplicationcontext (). getcontentresolver ();
Contentvalues values = new contentvalues ();
Values. Put ("name", "hahha ");
Values. Put ("Age", 33 );
Values. Put ("amount", "123 ");
Resolver. insert (Uri, values );
Content Provider contentprovider