Android的ContentProvider小记
来源:互联网 发布:头像源码 编辑:程序博客网 时间:2024/05/17 03:46
Android应用程序组件ContentProvider担任共享应用程序间数据的任务,它无须手动实例化,客户端可通过ContentResolver对象实现对ContentProvider的增、删、查、改的操作。
ContentResolver contentResolver = getContentResolver();
ContentProvider使用基于数据库模型的简单表格来提供其中的数据,访问ContentProvider的数据集则需要用到URI,URI的统一格式为:
content:// com.season.myprovider / tab / 1标准前缀 authority部分 路径部分 ID值
Android系统为常用数据类型提供了很多预定义的ContentProvider(声音,视频,图片,联系人等),大多位于android.provider 包中,下面是书上一个读取联系人信息的小demo。
布局文件就一个RelativeLayout里放一个TextView
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".ContactActivity"> <TextView android:id="@+id/tv" android:layout_width="match_parent" android:layout_height="match_parent" ></TextView></RelativeLayout>
java代码如下:
public class ContactActivity extends AppCompatActivity { //联系人表中的一些字段名 private String[] columns = {ContactsContract.Contacts._ID, ContactsContract.Contacts.DISPLAY_NAME, ContactsContract.CommonDataKinds.Phone.NUMBER, ContactsContract.CommonDataKinds.Phone.CONTACT_ID}; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_contract); TextView tv = (TextView) findViewById(R.id.tv); tv.setText(getQueryData()); } //获取查询到的信息 private String getQueryData() { StringBuilder builder = new StringBuilder(); ContentResolver resolver = getContentResolver(); //查询联系人 Cursor cursor = resolver.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null); while (cursor.moveToNext()) { int idIndex = cursor.getColumnIndex(columns[0]); int nameIndex = cursor.getColumnIndex(columns[1]); int id = cursor.getInt(idIndex); String name = cursor.getString(nameIndex); Cursor phone = resolver.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, columns[3] + "=" + id, null, null); while (phone.moveToNext()) { String number = phone.getString(phone.getColumnIndex(columns[2])); builder.append(name + ": " + number + "\n"); } } cursor.close(); return builder.toString(); }}
最后记得加权限:
<uses-permission android:name="android.permission.READ_CONTACTS" />
关于自定义ContentProvider
步骤:
- 建立数据库存储系统。到多数ContentProvider使用Android文件存储方法或者SQLite数据库保存数据,但是开发人员可以使用任何方式存储。Android提供了SQLiteOpenHelper类帮助创建数据库,SQLiteDatabase类帮助管理数据库
- 继承 ContentProvider类提供数据访问方式
- 在应用程序的AndroidManifest文件中声明ContentProvider。
下面是一个小demo:
MyContentProvider 类
public class MyContentProvider extends ContentProvider { SQLiteDatabase database; public static final Uri URI = Uri.parse("content://com.season.learncontentprovider"); private DBHelper dbHelper = null; private String DB_name = "tab"; @Override public boolean onCreate() { dbHelper = new DBHelper(getContext(), DB_name, null, 1); database = dbHelper.getWritableDatabase(); return false; } @Override public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) { Cursor cursor = database.query(DB_name, null, null, null, null, null, null); return cursor; } @Override public String getType(Uri uri) { return null; } @Override public Uri insert(Uri uri, ContentValues values) { long id = database.insert(DB_name, "_id", values); //Uri.withAppendedPath能轻松将ID加到URI Uri newUri = Uri.withAppendedPath(uri, id + ""); getContext().getContentResolver().notifyChange(uri, null); return newUri; } @Override public int delete(Uri uri, String selection, String[] selectionArgs) { getContext().getContentResolver().notifyChange(uri, null); return database.delete(DB_name, selection, selectionArgs); } @Override public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) { return 0; }//利用SQLiteOpenHelper来创建数据库表 private class DBHelper extends SQLiteOpenHelper { public DBHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) { super(context, name, factory, version); } @Override public void onCreate(SQLiteDatabase db) { db.execSQL("create table tab(" + "_id integer primary key autoincrement," + "name text not null," + "sex text not null)"); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { db.execSQL("drop table if exists " + DB_name); onCreate(db); } }}
AndroidManifest文件中声明ContentProvider
<provider android:name=".MyContentProvider" android:exported="true" android:authorities="com.season.learncontentprovider"/>
MainActivity类
public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); findViewById(R.id.btnWrite).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { write(); Toast.makeText(MainActivity.this, "写入成功", Toast.LENGTH_SHORT).show(); } }); } public void write() { ContentValues cv = new ContentValues(); cv.put("name", "season"); cv.put("sex", "boy"); getContentResolver().insert(MyContentProvider.URI, cv); }}
布局文件如下
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" > <TextView android:id="@+id/textView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="season boy" android:textSize="25dp" /> <Button android:id="@+id/btnWrite" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" android:layout_below="@+id/textView" android:layout_marginTop="37dp" android:text="写入数据" /></RelativeLayout>
结果如下:
到这里就已经完成了一个应用程序的自定义contentprovider共享数据的准备工作了,下面将再新建一个项目来读取该项目的共享数据集了
布局文件就一个LinearLayout里放一个ListView:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <ListView android:id="@+id/list" android:layout_width="match_parent" android:layout_height="match_parent"></ListView></LinearLayout>
MainActivity 类
public class MainActivity extends AppCompatActivity { ListView listView; private static final Uri uri = Uri.parse("content://com.season.learncontentprovider"); //列名数组 private String[] column = new String[]{"_id", "name", "sex"}; //继承CursorAdapter的一个自定义adapter private ExampleAdapter adapter; //继承了ContentObserver的自定义观察者类,用于监控provider数据改变 private InfoObserver observer; private ContentResolver contentResolver; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); listView = (ListView) findViewById(R.id.list); contentResolver = getContentResolver(); Cursor cursor = contentResolver.query(uri, null, null, null, null); observer = new InfoObserver(new Handler(), cursor); //注册一个观察者 contentResolver.registerContentObserver(uri, true, observer); adapter = new ExampleAdapter(this, cursor, true); listView.setAdapter(adapter); listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() { @Override public boolean onItemLongClick(AdapterView<?> parent, final View view, int position, long id) { new AlertDialog.Builder(MainActivity.this) .setMessage("删除?") .setPositiveButton("是", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { contentResolver.delete(uri, "_id = '" + ((ViewHolder) view.getTag()).id + "'", null); } }) .setNegativeButton("否", null) .show(); return true; } }); } @Override protected void onDestroy() { super.onDestroy(); contentResolver.unregisterContentObserver(observer); } private class InfoObserver extends ContentObserver { private Cursor cursor; public InfoObserver(Handler handler, Cursor cursor) { super(handler); this.cursor = cursor; } @Override public void onChange(boolean selfChange) { cursor.requery(); adapter.notifyDataSetChanged(); } } class ExampleAdapter extends CursorAdapter { public ExampleAdapter(Context context, Cursor c, boolean autoRequery) { super(context, c, autoRequery); } @Override public View newView(Context context, Cursor cursor, ViewGroup parent) { ViewHolder holder = new ViewHolder(); View view = LayoutInflater.from(context).inflate(R.layout.item, null); holder.tv_name = (TextView) view.findViewById(R.id.name); holder.tv_sex = (TextView) view.findViewById(R.id.sex); view.setTag(holder); return view; } @Override public void bindView(View view, Context context, Cursor cursor) { ViewHolder holder = (ViewHolder) view.getTag(); holder.id = cursor.getInt(cursor.getColumnIndex(column[0])); holder.tv_name.setText(cursor.getString(cursor.getColumnIndex(column[1]))); holder.tv_sex.setText(cursor.getString(cursor.getColumnIndex(column[2]))); } } class ViewHolder { int id; TextView tv_name; TextView tv_sex; }}
最后结果大致如下:
以上是本人对ContentProvider的一些理解,欢迎纠错补充。
0 0
- Android的ContentProvider小记
- android随笔小记ContentProvider
- android随笔小记ContentProvider
- 【Android开发小记--19】数据存储3--ContentProvider
- 有关android 的 contentprovider
- Android ContentProvider的使用
- Android ContentProvider的介绍
- Android ContentProvider的介绍
- 【Android】ContentProvider的使用
- 关于android的contentProvider
- Android ContentProvider的使用
- android的contentProvider
- android ContentProvider的使用
- android的ContentProvider
- android ContentProvider的使用
- Android ContentProvider的介绍
- android--ContentProvider的使用
- android的ContentProvider
- Libevent源码分析-----evbuffer结构与基本操作
- jenkins疑难解答
- Linux下使用shell脚本自动监控重启tomcat
- Android 沉浸式模式
- angularjs 密码一致性校验
- Android的ContentProvider小记
- Android学习笔记--2.创建的第一个Android程序
- 2016.08.22工作日志
- Spring
- Binder机制
- JS图片切换,幻灯+缩略图的实例效果
- 数据结构实验之栈七:出栈序列判定
- javascript的splice、slice、split的比较
- 使用libevent编写高并发HTTP server