第四大组件ContentProvider
功能:数据共享
按照文件路径和格式
无论哪种数据持久化方法,数据共享的接口统一为ContentProvider
系统自带应用的数据
通讯:拨号 短信 联系人
多媒体:音乐 视频 图片 拍照
其他:Home Launcher
一、新建ex0814_contentprovider项目
ContentProvider负责组织应用程序的数据,向其他应用程序提供数据。
清单配置如下:(增加属性authorities)
<application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <provider android:name="com.example.ex0814_contentprovider.MyContentProvider" android:authorities="com.example.provider.student" android:enabled="true" android:exported="true" > </provider> </application>
代码如下:
//ex0814_contentresolver项目和ex0814_contentprovider项目。 public class MyContentProvider extends ContentProvider {//创建一个数据共享接口ContentProvider,让其他的应用可以对他进行增删改查 private SQLiteDatabase mDb; private static final String TABLE_NAME = "student"; private static final String CULUMN_NAME_NAME = "name"; private static final String CULUMN_NAME_PHONE = "phone"; // 步骤: // 1.新建一个项目,删除Activity和清单中的 Activity的配置 // 2.新建android object,选择contentprovider,并设置 authorities属性 // 3.字符串建议:packagename + provider + 数据功能名 // 4.在provider类中,创建MySqliteOpenhelper的内部类 // 5.在onCreate()里面获取SQLiteDatabase对象 // 6.实现增删改查的方法 public MyContentProvider() { } //重写增删改查的方法 @Override public int delete(Uri uri, String selection, String[] selectionArgs) { int delete = mDb.delete(TABLE_NAME, selection, selectionArgs); return delete; } @Override public String getType(Uri uri) { // TODO: Implement this to handle requests for the MIME type of the data // at the given URI. throw new UnsupportedOperationException("Not yet implemented"); } @Override public Uri insert(Uri uri, ContentValues values) {//values由ex0814_contentresolver项目传来 long insert = mDb.insert(TABLE_NAME, null, values); Uri withAppendedId = ContentUris.withAppendedId(uri, insert); return withAppendedId; } @Override public boolean onCreate() { MySqliteOpenhelper openhelper = new MySqliteOpenhelper(getContext(), 1); mDb = openhelper.getWritableDatabase(); return false; } @Override public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) { Cursor query = mDb.query(TABLE_NAME, projection, selection, selectionArgs, null, null, sortOrder); return query; } @Override public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) { int update = mDb.update(TABLE_NAME, values, selection, selectionArgs); return update; } public class MySqliteOpenhelper extends SQLiteOpenHelper { public MySqliteOpenhelper(Context context, int version) { super(context, "cuijing.db", null, version); // TODO Auto-generated constructor stub } @Override public void onCreate(SQLiteDatabase db) {//创建表,一运行,会去检查是否有表,如果已经有了,或者版本号更新,都不会再执行onCreate(); Log.e("MySqliteOpenhelper", "onCreate"); db.execSQL("CREATE TABLE student(_id INTEGER PRIMARY KEY AUTOINCREMENT,name VARCHAR(20),phone VARCHAR(20))"); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {//如果已经有表存在,并且版本号有更新,就会执行onUpgrade(); Log.e("MySqliteOpenhelper", "onUpgrade"); db.execSQL("ALTER TABLE student ADD COLUMN weight INTEGER"); } } }
二、新建ex0814_contentresolver项目
使用其他应用的Provider的方法:
1. getContentResolver()
2. resolver.query(uri, )
ContentResolver则负责获取ContentProvider提供的数据,修改/添加/删除更新数据等。
代码如下:
public class MainActivity extends Activity implements OnClickListener {//ex0814_contentresolver查询ex0814_contentprovider项目的数据库,通过ex0814_contentprovider提供的provider共享接口。 private EditText mEt_name; private EditText mEt_phone; private ContentResolver contentResolver; private TextView textView; private ListView listView; private static final String TABLE_NAME = "student"; private static final String CULUMN_NAME_NAME = "name"; private static final String CULUMN_NAME_PHONE = "phone"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); contentResolver = getContentResolver(); findViewById(R.id.btn_baocun).setOnClickListener(this); findViewById(R.id.btn_delete).setOnClickListener(this); findViewById(R.id.btn_selector).setOnClickListener(this); findViewById(R.id.btn_update).setOnClickListener(this); listView = (ListView) findViewById(R.id.listView1); mEt_name = (EditText) findViewById(R.id.et_name); mEt_phone = (EditText) findViewById(R.id.et_phone); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } @Override public void onClick(View v) { switch (v.getId()) { case R.id.btn_baocun: insert(); break; case R.id.btn_delete: delete(); break; case R.id.btn_selector: selector(); break; case R.id.btn_update: update(); break; default: break; } } private void update() {//修改 Editable et_name = mEt_name.getText(); Editable et_phone = mEt_phone.getText(); ContentValues values = new ContentValues(); values.put(CULUMN_NAME_PHONE, et_phone.toString());//修改号码 contentResolver.update(Uri.parse("content://com.example.provider.student"), values, "name=?", new String[]{et_name.toString()});//根据名字修改号码 } private void selector() {//查询 Cursor cursor = contentResolver.query(Uri.parse("content://com.example.provider.student"), null, null, null, null); String[] from = {CULUMN_NAME_NAME,CULUMN_NAME_PHONE}; int[] to = {android.R.id.text1,android.R.id.text2}; // 使用 SimpleCursorAdapter,要把创建表中的自增ID修改成_id SimpleCursorAdapter adapter = new SimpleCursorAdapter(MainActivity.this, android.R.layout.simple_list_item_2, cursor, from, to); listView.setAdapter(adapter); } private void delete() {//删除 Editable et_name = mEt_name.getText(); contentResolver.delete(Uri.parse("content://com.example.provider.student"), "name=?", new String[]{et_name.toString()});//删除的是数据库里editext显示为name的数据 } private void insert() {//把ex0814_contentresolver项目里editext的内容插入到ex0814_contentprovider项目的数据库(database)里 ContentValues values = new ContentValues(); Editable et_name = mEt_name.getText(); Editable et_phone = mEt_phone.getText(); values.put("name", et_name.toString()); values.put("phone", et_phone.toString()); contentResolver.insert(Uri.parse("content://com.example.provider.student"), values);//uri格式必须以content://开头,uri内容为ex0814_contentprovider的anthoritise(属性) } }