含义:不同应用之间数据交互的中介(接口)
案例:
(1)MainActivity
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MyOpenHelper helper = new MyOpenHelper(getApplicationContext());
SQLiteDatabase db = helper.getWritableDatabase();
//取出数据
Cursor cursor = db.query("info", null, null, null, null, null, null);
if (cursor!=null&&cursor.getCount()>0) {
while (cursor.moveToNext()) {
String name = cursor.getString(1);
String money = cursor.getString(2);
System.out.println("name:"+name+"-----"+money);
}
cursor.close();
}
}
@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 boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
(2)MyOpenHelper
public class MyOpenHelper extends SQLiteOpenHelper {
public MyOpenHelper(Context context) {
//super(context, name, factory, version);
/**
* context 想下文
* name 数据库名称
* factory 游标工厂
* version 版本
*/
super(context, "Account.db", null, 1);
}
/**
* 当数据库第一次创建的时候调用,适合做表结构的初始化
*/
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("create table info (_id integer primary key autoincrement,name varchar(20),money varchar(20))");
db.execSQL("insert into info(name,money) values(?,?)",new String[]{"张三","5000"});
db.execSQL("insert into info(name,money) values(?,?)",new String[]{"李四","3000"});
}
/**
* 用于数据库升级
*/
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
(3)MyProvider
public class MyProvider extends ContentProvider {
// 1、定义一个一个Url路径匹配器
private static final UriMatcher mURLMatcher = new UriMatcher(UriMatcher.NO_MATCH);
private static final int QUERYSUCCESS = 0;
private static final int INSERTSUCCESS = 1;
private static final int UPDATESUCCESS = 2;
private static final int DELETESUCCESS = 3;
private MyOpenHelper myOpenHelper;
// 2、创建一个静态代码块,在这里面添加Uri
static {
mURLMatcher.addURI("com.luochenlong.provider", "query", QUERYSUCCESS);
mURLMatcher.addURI("com.luochenlong.provider", "insert", INSERTSUCCESS);
mURLMatcher.addURI("com.luochenlong.provider", "update", UPDATESUCCESS);
mURLMatcher.addURI("com.luochenlong.provider", "delete", DELETESUCCESS);
}
// 当内容提供者初始化会执行此方法
@Override
public boolean onCreate() {
// 3、初始化myOpenHelper对象,操作数据库
myOpenHelper = new MyOpenHelper(getContext());
return false;
}
@Override
public String getType(Uri uri) {
return null;
}
// 对外接口
/**
* 插入
*/
@Override
public Uri insert(Uri uri, ContentValues values) {
int code = mURLMatcher.match(uri);
//Toast.makeText(getContext(), "", 1000).show();
if (code == INSERTSUCCESS) {
// 说明路径匹配成功
SQLiteDatabase db = myOpenHelper.getReadableDatabase();
long insert = db.insert("info", null, values);
Uri uri2 = Uri.parse("com.luochenlong/" + insert);
if (insert > 0) {
// 发送一条消息,说明数据库被操作了
getContext().getContentResolver().notifyChange(uri, null);
}
db.close();
return uri2;
} else {
throw new IllegalArgumentException("uri路径不匹配 请检查路径");
}
}
/**
* 删除
*/
@Override
public int delete(Uri uri, String selection, String[] selectionArgs) {
int code = mURLMatcher.match(uri);
if (code == DELETESUCCESS) {
// 匹配成功
SQLiteDatabase db = myOpenHelper.getReadableDatabase();
int delete = db.delete("info", selection, selectionArgs);
if (delete > 0) {
// 发送一条消息,说明数据被操作了
getContext().getContentResolver().notifyChange(uri, null);
}
return delete;
}
return 0;
}
/**
* 查询
*/
@Override
public Cursor query(Uri uri, String[] projection, String selection,
String[] selectionArgs, String sortOrder) {
int code = mURLMatcher.match(uri);
if (code == QUERYSUCCESS) {
// 说明路径匹配成功
SQLiteDatabase db = myOpenHelper.getReadableDatabase();
// 调用query方法
Cursor cursor = db
.query("info", null, null, null, null, null, null);
// 发送一条消息 说明数据库被操作
getContext().getContentResolver().notifyChange(uri, null);
// db.close();
// 小细节 这个cursor不能关
return cursor;
} else {
throw new IllegalArgumentException("uri路径不匹配 请检测路径");
}
}
/**
* 修改
*/
@Override
public int update(Uri uri, ContentValues values, String selection,
String[] selectionArgs) {
int code = mURLMatcher.match(uri);
if (code == UPDATESUCCESS) {
// 路径匹配成功
SQLiteDatabase db = myOpenHelper.getReadableDatabase();
// 代表影响的行数
int update = db.update("info", values, selection, selectionArgs);
if (update > 0) {
getContext().getContentResolver().notifyChange(uri, null);
}
return update;
} else {
throw new IllegalArgumentException("uri路径不匹配 请检查路径");
}
}
}
(4)配置文件
<!-- 配置内容提供者 -->
<provider
android:name="com.luochenlong.MyProvider"
android:authorities="com.luochenlong.provider"
android:exported="true"
>
</provider>
(5)访问应用MainActivity
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
//增加
public void click1(View v) {
Uri uri = Uri.parse("content://com.luochenlong.provider/insert");
ContentValues values = new ContentValues();
values.put("name", "zhaoliu");
values.put("money", 1000);
// 插入一条数据
Uri uri2 = getContentResolver().insert(uri, values);
System.out.println("uri2:" + uri2);
}
//删除
public void click2(View v) {
Uri uri = Uri.parse("content://com.luochenlong.provider/delete");
int delete = getContentResolver().delete(uri, "name=?", new String[]{"zhaoliu"});
Toast.makeText(getApplicationContext(), "删除了"+delete+"行", 1).show();
}
//查询
public void click3(View v) {
Uri uri = Uri.parse("content://com.luochenlong.provider/query");
Cursor cursor = getContentResolver().query(uri,
new String[] { "name", "money" }, null, null, null);
if (cursor != null) {
while (cursor.moveToNext()) {
String name = cursor.getString(0);
String money = cursor.getString(1);
System.out.println("name=" + name + ";money=" + money);
}
}
}
//修改
public void click4(View v) {
Uri uri = Uri.parse("content://com.luochenlong.provider/update");
ContentValues values = new ContentValues();
values.put("money","10000000");
int update = getContentResolver().update(uri, values, "name=?", new String[]{"zhaoliu"});
Toast.makeText(getApplicationContext(), "更新了"+update+"行", 1).show();
}
@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 boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}