今天我们来谈谈4大组件之一的contentprovier,在android里面数据Shared Preferences、网络存储、文件存储、外储存储、SQLite这5中储存方式,但是如果要用到程序间的数据共享,就要用到contentprovider了,contentprovider主要是通过配置文件申明这个数据可以读取,然后通过contentresolver和contentprovider建立连接,contentprovider里面的uri有具体的格式,如:content://com.jk.contentproviderdemo.MyProvider/person,第一个部分是固定的,第二个包名,,第三个表名,也就是数据库的名字,可能还有一些细节没有说到,但是大概的话应该就是这个样子,如果有需要补充的会在后面更新
package com.jk.contentproviderdemo.dao; import android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteDatabase.CursorFactory; import android.database.sqlite.SQLiteOpenHelper; public class PersonSQLiteOpenHelper extends SQLiteOpenHelper { public static String DATABASENANE = "person.db"; public PersonSQLiteOpenHelper(Context context) { // create a database named person.db super(context, DATABASENANE, null, 1); } @Override public void onCreate(SQLiteDatabase db) { // create a table that contain _id,name,number db.execSQL("create table person (_id integer primary key autoincrement , name text , number text)"); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { } }
package com.jk.contentproviderdemo; import com.jk.contentproviderdemo.dao.PersonSQLiteOpenHelper; import android.content.ContentProvider; import android.content.ContentValues; import android.content.UriMatcher; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.net.Uri; public class MyProvider extends ContentProvider { // override the delete method @Override public int delete(Uri uri, String selection, String[] selectionArgs) { System.out.println("delete"); return 0; } // override the get type method @Override public String getType(Uri uri) { System.out.println("get type"); return null; } // override the insert method @Override public Uri insert(Uri uri, ContentValues values) { System.out.println("insert"); return null; } // override the onCreate method @Override public boolean onCreate() { System.out.println("oncreate"); return false; } // override the query method @Override public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) { System.out.println("query"); // get a databasehelper class object PersonSQLiteOpenHelper per = new PersonSQLiteOpenHelper(getContext()); // get a writable object SQLiteDatabase db = per.getWritableDatabase(); // begin to query and get a Cursor Cursor c = db.query("person", projection, selection, selectionArgs, null, null, null); return c; } // override the update method @Override public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) { System.out.println("updata"); return 0; } }
package com.jk.contentproviderdemo; import com.jk.contentproviderdemo.dao.PersonSQLiteOpenHelper; import android.os.Bundle; import android.app.Activity; import android.content.ContentValues; import android.database.sqlite.SQLiteDatabase; import android.view.Menu; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // insert a data init(); } private void init() { // PersonSQLiteOpenHelper per=new PersonSQLiteOpenHelper(this); // SQLiteDatabase db=per.getWritableDatabase(); // ContentValues contentvalues=new ContentValues(); // contentvalues.put("name", "ni"); // contentvalues.put("number", "1413"); // db.insert("person", null, contentvalues); } }
<provider android:name="com.jk.contentproviderdemo.MyProvider" android:authorities="com.jk.contentproviderdemo.MyProvider" android:exported="true"></provider> </application>
package com.example.contentproviderdemotest; import android.net.Uri; import android.os.Bundle; import android.app.Activity; import android.content.ContentResolver; import android.content.ContentValues; import android.database.Cursor; import android.view.Menu; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ContentResolver cr = this.getContentResolver(); // init uri Uri myUri = Uri .parse("content://com.jk.contentproviderdemo.MyProvider/person"); // get a object that can save data ContentValues cv = new ContentValues(); // call the insert method that belong to uri cr.insert(myUri, cv); // call the query method that belong to uri Cursor c = cr.query(myUri, null, null, null, null); // show the data while (c.moveToNext()) { int id = c.getInt(c.getColumnIndex("_id")); String name = c.getString(1); String number = c.getString(2); System.out.println(id + name + number); } } }