android 学习随笔二十一(内容提供者 )
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了android 学习随笔二十一(内容提供者 )相关的知识,希望对你有一定的参考价值。
一、内容提供者
* 应用的数据库是不允许其他应用访问的
* 内容提供者的作用就是让别的应用访问到你的私有数据
* 自定义内容提供者,继承ContentProvider类,重写增删改查方法,在方法中写增删改查数据库的代码,举例增方法
@Override
public Uri insert(Uri uri, ContentValues values) {
db.insert("person", null, values);
return uri;
}
* 在清单文件中定义内容提供者的标签,注意必须要有authorities属性,这是内容提供者的主机名,功能类似地址
<provider android:name="com.itheima.contentprovider.PersonProvider"
android:authorities="com.itheima.person"
android:exported="true"
></provider>
package com.itheima.mycontentprovider.db; import java.io.Serializable; import android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteDatabase.CursorFactory; import android.database.sqlite.SQLiteOpenHelper; import android.os.Parcelable; public class MyOpenHelper extends SQLiteOpenHelper { public MyOpenHelper(Context context) { super(context, "people.db", null, 2); } @Override public void onCreate(SQLiteDatabase db) { db.execSQL("create table person(_id integer primary key autoincrement, name char(10), phone char(20), money integer(10))"); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { db.execSQL("create table handsome(_id integer primary key autoincrement, name char(10), phone char(20))"); } }
package com.itheima.mycontentprovider; import com.itheima.mycontentprovider.db.MyOpenHelper; 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 SQLiteDatabase db; //创建uri匹配器 UriMatcher um = new UriMatcher(UriMatcher.NO_MATCH); { //添加匹配规则 //arg0:主机名 //arg1:路径 //arg2:匹配码 um.addURI("com.itheima.people", "person", 1);//content://com.itheima.people/person um.addURI("com.itheima.people", "handsome", 2);//content://com.itheima.people/handsome um.addURI("com.itheima.people", "person/#", 3);//content://com.itheima.people/person/10 } //内容提供者创建时调用 @Override public boolean onCreate() { MyOpenHelper oh = new MyOpenHelper(getContext()); db = oh.getWritableDatabase(); return false; } //values:其他应用要插的数据 @Override public Uri insert(Uri uri, ContentValues values) { if(um.match(uri) == 1){ db.insert("person", null, values); //数据库改变了,内容提供者发出通知 //arg0:通知发到哪个uri上,注册在这个uri上的内容观察者都可以收到通知 getContext().getContentResolver().notifyChange(uri, null); } else if(um.match(uri) == 2){ db.insert("handsome", null, values); getContext().getContentResolver().notifyChange(uri, null); } else{ throw new IllegalArgumentException("uri传错啦傻逼"); } return uri; } @Override public int delete(Uri uri, String selection, String[] selectionArgs) { int i = 0; if(um.match(uri) == 1){ i = db.delete("person", selection, selectionArgs); } else if(um.match(uri) == 2){ i = db.delete("handsome", selection, selectionArgs); } else{ throw new IllegalArgumentException("uri又传错啦傻逼"); } return i; } @Override public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) { int i = db.update("person", values, selection, selectionArgs); return i; } @Override public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) { Cursor cursor = null; if(um.match(uri) == 1){ cursor = db.query("person", projection, selection, selectionArgs, null, null, sortOrder, null); } else if(um.match(uri) == 2){ cursor = db.query("handsome", projection, selection, selectionArgs, null, null, sortOrder, null); } else if(um.match(uri) == 3){ //取出uri末尾携带的数字 long id = ContentUris.parseId(uri); cursor = db.query("person", projection, "_id = ?", new String[]{"" + id}, null, null, sortOrder, null); } return cursor; } //返回通过指定uri获取的数据的mimetype @Override public String getType(Uri uri) { if(um.match(uri) == 1){ return "vnd.android.cursor.dir/person"; } else if(um.match(uri) == 2){ return "vnd.android.cursor.dir/handsome"; } else if(um.match(uri) == 3){ return "vnd.android.cursor.item/person"; } return null; } }
package com.itheima.mycontentprovider; import android.os.Bundle; import android.app.Activity; import android.view.Menu; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } @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; } }
package com.itheima.mycontentprovider; import com.itheima.mycontentprovider.db.MyOpenHelper; import android.test.AndroidTestCase; public class Test extends AndroidTestCase { public void test(){ MyOpenHelper oh = new MyOpenHelper(getContext()); oh.getWritableDatabase(); } }
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.itheima.mycontentprovider" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="17" /> <instrumentation android:name="android.test.InstrumentationTestRunner" android:targetPackage="com.itheima.mycontentprovider"></instrumentation> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <uses-library android:name="android.test.runner"/> <activity android:name="com.itheima.mycontentprovider.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> <provider android:name="com.itheima.mycontentprovider.PersonProvider" android:authorities="com.itheima.people" android:exported="true" > </provider> </application> </manifest>
* 创建一个其他应用,访问自定义的内容提供者,实现对数据库的插入操作
public void click(View v){
//得到内容分解器对象
ContentResolver cr = getContentResolver();
ContentValues cv = new ContentValues();
cv.put("name", "小方");
cv.put("phone", 138856);
cv.put("money", 3000);
//url:内容提供者的主机名
cr.insert(Uri.parse("content://com.itheima.person"), cv);
}
package com.itheima.other; 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; import android.view.View; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } public void insert(View v){ //通过内容提供者把数据插入01数据库 //1.获取contentResolver ContentResolver resolver = getContentResolver(); //2.访问内容提供者,插入数据 ContentValues values = new ContentValues(); values.put("name", "流氓润"); values.put("phone", 138992); values.put("money", 14000); //arg0:指定内容提供者的主机名 resolver.insert(Uri.parse("content://com.itheima.people/person"), values); values.clear(); values.put("name", "侃哥"); values.put("phone", 15999); //arg0:指定内容提供者的主机名 resolver.insert(Uri.parse("content://com.itheima.people/handsome"), values); } public void delete(View v){ ContentResolver resolver = getContentResolver(); int i = resolver.delete(Uri.parse("content://com.itheima.people"), "name = ?", new String[]{"凤姐"}); System.out.println(i); } public void update(View v){ ContentResolver resolver = getContentResolver(); ContentValues values = new ContentValues(); values.put("money", 16001); int i = resolver.update(Uri.parse("content://com.itheima.people"), values, "name = ?", new String[]{"春晓"}); System.out.println(i); } public void query(View v){ ContentResolver resolver = getContentResolver(); Cursor cursor = resolver.query(Uri.parse("content://com.itheima.people/person"), null, null, null, null); while(cursor.moveToNext()){ String name = cursor.getString(1); String phone = cursor.getString(2); String money = cursor.getString(3); System.out.println(name + ";" + phone + ";" + money); } } public void queryOne(View v){ ContentResolver resolver = getContentResolver(); Cursor cursor = resolver.query(Uri.parse("content://com.itheima.people/person/4"), null, null, null, null); if(cursor.moveToNext()){ String name = cursor.getString(1); String phone = cursor.getString(2); String money = cursor.getString(3); System.out.println(name + ";" + phone + ";" + money); } } }
------------------------------------------
以上是关于android 学习随笔二十一(内容提供者 )的主要内容,如果未能解决你的问题,请参考以下文章