Android -- ContentProvider cross-Program Data Access

Source: Internet
Author: User

Two programs A and B, and program A have two classes. One inherits Activity, and the other inherits ContentProviderB. The other inherits Activity and creates A database in program, create two tables in the database to enable program B to access the data in program A to inherit the Activity class: Create A database and create A table in the database, insert data in the table [html] package cn. mrzhu. test24; import android. app. activity; import android. content. contentValues; import android. database. sqlite. SQLiteDatabase; import android. OS. bundle; public class Provider extends Activity {/** Called when the activity is first created. * // @ Override public void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. main); // create a database SQLiteDatabase db = openOrCreateDatabase ("test24.db", MODE_PRIVATE, null); // create a table db.exe cSQL ("create table test1 (_ id integer, name varchar (20), age integer) "); db.exe cSQL (" create table test2 (_ id integer, name varchar (20), age integer )"); // Add data ContentValues values = new ContentValues (); values to the test1 table. put ("name", "zhangsan"); values. put ("age", 30); db. insert ("test1", "name", values); values. clear (); values. put ("name", "lisi"); values. put ("age", 40); db. insert ("test1", "name", values); // Add data values to table test2. clear (); values. put ("name", "Peter"); values. put ("age", 20); db. insert ("test2", "name", values); values. clear (); values. put ("name", "Mike"); values. put ("age", 25); db. insert ("test2", "name", values) ;}} class that inherits ContentProvider in program A: AndroidManifest. register [html] <provider android: name = "MyContentProvider" android: authorities = "cn. mrzhu. test24 "> </provider> android: authorities =" cn. mrzhu. test24 "provides A uri for access to program B. Only program B can access data in program A [html] package cn. mrzhu. test24; import android. content. contentProvider; import android. content. contentValues; import android. content. context; import android. content. uriMatcher; import android. database. cursor; import android. database. sqlite. SQLiteDatabase; import android.net. uri; public class MyContentProvider extends ContentProvider {// Uri filter. This filter can be used to retrieve the UriMatcher um; static final String AUTHORITY = "cn. mrzhu. test24 "; @ Override public int delete (Uri uri, String selection, String [] selectionArgs) {return 0 ;}@ Override public String getType (Uri uri) {// TODO Auto-generated method stub return null;} @ Override public Uri insert (Uri uri, ContentValues values) {// TODO Auto-generated method stub return null ;} @ Override public boolean onCreate () {// initialize the filter um = new UriMatcher (UriMatcher. NO_MATCH); // Add the content to be filtered. The uri sent from other programs is filtered. If the field is test1, the code is 1 um. addURI (AUTHORITY, "test1", 1); um. addURI (AUTHORITY, "test2", 2); return false ;}@ Override public Cursor query (Uri uri, String [] projection, String selection, String [] selectionArgs, String sortOrder) {SQLiteDatabase db = getContext (). openOrCreateDatabase ("test24.db", Context. MODE_PRIVATE, null); Cursor cs = null; // filter the uri and obtain the returned value int code = um. match (uri); // execute different query statements switch (code) {case 1: cs = db based on different return values. rawQuery ("select * from test1", null); break; case 2: cs = db. rawQuery ("select * from test2", null); break;} // After the query statement is executed, return the return cs;} @ Override public int update (Uri uri, contentValues values, String selection, String [] selectionArgs) {// TODO Auto-generated method stub return 0 ;}} B Program: access the data [html] package cn of program A through the uri provided by the class inheriting ContentProvider in program. mrzhu. test24x; import android. app. activity; import android. content. contentResolver; import android. database. cursor; import android.net. uri; import android. OS. bundle; import android. util. log; public class Main extends Activity {/** Called when the activity is first created. * // @ Override public void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. main); // obtain the ContentResolver object ContentResolver cr = getContentResolver (); // specify the uri and specify the table Uri = uri for the operation. parse ("content: // cn. mrzhu. test24/test1 "); // run the query method to return a result set Cursor cs = cr. query (uri, null, null); // traverses the result set and retrieves the data while (cs. moveToNext () {Log. I ("System. out "," _ id "+ cs. getInt (0) + "name" + cs. getString (1) + "age" + cs. getInt (2 ));}}}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.