61.内容提供者ContentProvider--模拟增删改查
来源:互联网 发布:支付宝收费淘宝卖家 编辑:程序博客网 时间:2024/06/05 21:51
ContentProvider
安卓四大组件之一
作用:把私有数据暴露给其他的应用,通常把私有数据库的数据暴露给其他的应用。
安卓代码模拟执行某一个方法:
创建一个class文件Test.java继承来自android.test.AndroidTestCase。然后在清单文件里面添加配置项,androdi:name的配置是固定的,包名就是package,user-libiary的名字也是固定的。
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.ldw.selfContentProvider" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="17" /> <instrumentation android:name="android.test.InstrumentationTestRunner" android:targetPackage="com.ldw.selfContentProvider" ></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.ldw.selfContentProvider.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> </application></manifest>
模拟创建一个数据库:
package com.ldw.selfContentProvider;import android.test.AndroidTestCase;public class Test extends AndroidTestCase {public void test(){MyOpenHelper my = new MyOpenHelper(getContext(), "po.db", null, 1);my.getWritableDatabase();}}
APP1:创建内容提供者
内容提供者需要在清单文件总配置,authorities是内容提供者的地址,通过这个地址可以访问内容提供者,精确的知道访问的是哪一个内容提供者,exported:可以导出数据,获取到数据。
可以在Uri后面添加参数来进一步限定访问哪一个数据表,比如下面的Uri是com.ldw.po限定访问的是po表:
Uri.parse("content://com.ldw.po/po")
配置范例如下:
<provider android:name="com.ldw.selfContentProvider.poProvider" android:authorities="com.ldw.po" android:exported="true" ></provider>
MyOpenHelper.java
package com.ldw.selfContentProvider;import android.content.Context;import android.database.sqlite.SQLiteDatabase;import android.database.sqlite.SQLiteDatabase.CursorFactory;import android.database.sqlite.SQLiteOpenHelper;public class MyOpenHelper extends SQLiteOpenHelper {public MyOpenHelper(Context context, String name, CursorFactory factory,int version) {super(context, name, factory, version);// TODO Auto-generated constructor stub}@Overridepublic void onCreate(SQLiteDatabase db) {// TODO Auto-generated method stubdb.execSQL("create table po(_id integer primary key autoincrement, name char(10), money integer(20))");}@Overridepublic void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {// TODO Auto-generated method stub}}
poProvider.java
package com.ldw.selfContentProvider;import android.content.ContentProvider;import android.content.ContentValues;import android.database.Cursor;import android.database.sqlite.SQLiteDatabase;import android.net.Uri;public class poProvider extends ContentProvider {private MyOpenHelper my;SQLiteDatabase db;//内容提供者创建的时候调用@Overridepublic boolean onCreate() {// TODO Auto-generated method stubmy = new MyOpenHelper(getContext(), "po.db", null, 1);db = my.getWritableDatabase();return false;}@Overridepublic Cursor query(Uri uri, String[] projection, String selection,String[] selectionArgs, String sortOrder) {// TODO Auto-generated method stubCursor cursor = db.query("po", projection, selection, selectionArgs, null, null, sortOrder, null);return cursor;}@Overridepublic String getType(Uri uri) {// TODO Auto-generated method stubreturn null;}//这个方法提供其他的方法调用,用于往po数据库中插入数据//values由其他的应用传入,封装需要被传入的数据,@Overridepublic Uri insert(Uri uri, ContentValues values) {// TODO Auto-generated method stubdb.insert("po", null, values);return uri;}@Overridepublic int delete(Uri uri, String selection, String[] selectionArgs) {// TODO Auto-generated method stub//返回值是删除的行数int i = db.delete("po", selection, selectionArgs);return i;}@Overridepublic int update(Uri uri, ContentValues values, String selection,String[] selectionArgs) {// TODO Auto-generated method stub//返回值是int i = db.update("po", values, selection, selectionArgs);return i;}}
APP2:访问自己定义的内容提供者
activity_main.xml
<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:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" android:orientation="vertical" > <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="插入" android:onClick="insert" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="删除" android:onClick="delete" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="修改" android:onClick="update" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="查询" android:onClick="query" /> </LinearLayout>
MainActivity.java
package com.ldw.visit;import android.app.Activity;import android.content.ContentResolver;import android.content.ContentValues;import android.database.Cursor;import android.net.Uri;import android.os.Bundle;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){ //通过内容提供者把数据插入people数据库 //拿到contentResolver ContentResolver cr = getContentResolver(); ContentValues values = new ContentValues(); values.put("name", "AAA"); values.put("money", "15000"); //Uri前面必须带前缀,是内容提供者的地址 cr.insert(Uri.parse("content://com.ldw.po"), values); values.put("name", "BBB"); values.put("money", "15000"); //Uri前面必须带前缀,是内容提供者的地址 cr.insert(Uri.parse("content://com.ldw.po"), values); values.put("name", "CCC"); values.put("money", "15000"); //Uri前面必须带前缀,是内容提供者的地址 cr.insert(Uri.parse("content://com.ldw.po"), values); values.put("name", "DDD"); values.put("money", "15000"); //Uri前面必须带前缀,是内容提供者的地址 cr.insert(Uri.parse("content://com.ldw.po"), values); } public void delete(View v){ //通过内容提供者把数据插入people数据库 //拿到contentResolver ContentResolver cr = getContentResolver(); int i = cr.delete(Uri.parse("content://com.ldw.po"), "name = ?", new String[]{"AAA"}); System.out.println(i); } public void update(View v){ //通过内容提供者把数据插入people数据库 //拿到contentResolver ContentResolver cr = getContentResolver(); ContentValues values = new ContentValues(); values.put("name", "aaa"); int i = cr.update(Uri.parse("content://com.ldw.po"), values, "name = ?", new String[]{"AAA"}); System.out.println(i); } public void query(View v){ //通过内容提供者把数据插入people数据库 //拿到contentResolver ContentResolver cr = getContentResolver(); Cursor cursor = cr.query(Uri.parse("content://com.ldw.po"), null, null, null, null); while(cursor.moveToNext()){ String name = cursor.getString(1); String money = cursor.getString(2); System.out.println("name:" + name + ";" + "money" + money); } } }
0 0
- 61.内容提供者ContentProvider--模拟增删改查
- Android中内容提供者ContentProvider实现数据库增删改查
- Android中内容提供者ContentProvider实现数据库增删改查
- android的与ContentResolver(内容访问者)对ContentProvider(内容提供者)的增删查改
- ContenProvider数据内容提供者,增删改查
- 内容提供者于接受者的简单增删改查
- ContentProvider实现增删查改
- 自定义内容提供者(增删改)
- Android中内容提供者解析----增删查改其他应用的数据库
- 安卓内容提供者实现对数据的增删改查
- 安卓内容提供者实现对数据的增删改查
- ContentProvider-SqliteDatabase实现增删查改(1)
- List模拟增删改查
- ContentProvider内容提供者
- Android ContentProvider(内容提供者)
- ContentProvider 内容提供者
- ContentProvider内容提供者(一)
- ContentProvider内容提供者(二)
- LWIP socket层与协议栈层交互过程分析
- Ollydbg异常处理设置项
- String、StringBuffer和StringBuilder的区别
- (转)五大常用算法:分治、动态规划、贪心、回溯和分支界定
- 基本常用命令
- 61.内容提供者ContentProvider--模拟增删改查
- 在shell脚本中捕获java的System.exit()的状态
- SpringMVC整合Shiro
- 记录常用Linux 命令
- apache_httpd的安装与配置
- 算法训练 字符串的展开 时间限制:1.0s 内存限制:256.0MB
- hdu4411(#最小费用流)
- NumPy学习笔记(3)--排序与索引
- swift-tableView滚动,里面图片发生偏移动画