作用:
向其他应用程序提供访问数据的接口
两种方式:
1.创建自己的ContentProvider
2.已存在的ContentProvider
重要性:
数据模型
URI
一 .
自定义ContentProvider
写一个类继承ContentProvider 提供内容 同时要创建一个表 Money
实现里面的方法,完成增删改查操作
需要一个SQliteOpenHelder类
···
public class MySQLHelper extends SQLiteOpenHelper{
public MySQLHelper(Context context){
super(context, "bankdb", null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("create table money(id integer primary key autoincrement,name varchar(20),count folat(11,3)) ");
db.execSQL("insert into money values(null,'张三',1000)");
db.execSQL("insert into money values(null,'李四',0)");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
}
}
···
************************************ContentProvider********************************
···
public class MyContentProvider extends ContentProvider{
static UriMatcher uriMatcher ;
static final int QUERY_CODE=0;
static final int INSERT_CODE=1;
private static final int UPDATE_CODE = 2;
private SQLiteDatabase rd;
static{
uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);//uri匹配
uriMatcher.addURI("com.beicai.cn", "query", QUERY_CODE);
uriMatcher.addURI("com.beicai.cn", "insert", INSERT_CODE);
uriMatcher.addURI("com.beicai.cn", "update", UPDATE_CODE);
}
@Override //初始化
public boolean onCreate() {
MySQLHelper mySQLHelper = new MySQLHelper(getContext());
rd = mySQLHelper.getReadableDatabase();
return false;
}
@Override
public Cursor query(Uri uri, String[] projection, String selection,
String[] selectionArgs, String sortOrder) {
int match = uriMatcher.match(uri);//content://com.beicai.cn/query
Cursor cursor =null;
switch (match) {
case QUERY_CODE:
cursor = rd.query("money", projection, selection, selectionArgs, null, null, sortOrder);
break;
case INSERT_CODE:
break;
}
return cursor;
}
@Override
public String getType(Uri uri) {
// TODO Auto-generated method stub
return null;
}
@Override
public Uri insert(Uri uri, ContentValues values) {
long x = rd.insert("money", null, values);
//rd.insert(table, nullColumnHack, values)
Uri uri2 = Uri.parse("content://com.beicai.cn/insert"+x);
return uri2;
}
@Override
public int delete(Uri uri, String selection, String[] selectionArgs) {
int delete = rd.delete("money", selection, selectionArgs);
Uri delete1 = Uri.parse("content://com.beicai.cn/delete"+delete);
return delete;
}
@Override
public int update(Uri uri, ContentValues values, String selection,
String[] selectionArgs) {
int match = uriMatcher.match(uri);
int update = 0;
switch (match) {
case UPDATE_CODE:
// rd.execSQL(sql);
update = rd.update("money", values, selection, selectionArgs);
break;
default:
break;
}
return update;
}
}
···
在清单文件里面注册:
···
<provider android:name="com.example.ytest2_mycontentprovider.MyContentProvider"
android:authorities="com.beicai.cn"
>
</provider>
···
*****************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:orientation="vertical"
tools:context="${relativePackage}.${activityClass}" >
<Button
android:id="@+id/btn1"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:text="查询" />
<Button
android:id="@+id/btn2"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:text="添加" />
<Button
android:id="@+id/btn3"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:text="删除" />
<Button
android:id="@+id/btn4"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:text="修改" />
</LinearLayout>
···
MainActivity中
···
package com.example.ytest2_mycontentprovider;
import android.app.Activity;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Toast;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.btn1).setOnClickListener(new OnClickListener() {
@Override // 查询
public void onClick(View v) {
//内容提供者 内容解析者
ContentResolver contentResolver = getContentResolver();
Uri uri = Uri.parse("content://com.beicai.cn/query");
Cursor cursor = contentResolver.query(uri, null, null, null, null);//null 查询所有
if (cursor!=null) {
cursor.moveToFirst();
cursor.moveToPrevious();
while (cursor.moveToNext()) {
int id = cursor.getInt(0);
String name = cursor.getString(1);
float count = cursor.getFloat(2);
Toast.makeText(MainActivity.this, "id:"+id+" name"+name+" count:"+count, Toast.LENGTH_SHORT).show();
}
}
}
});
//添加
findViewById(R.id.btn2).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
ContentResolver contentResolver = getContentResolver();
Uri uri = Uri.parse("content://com.beicai.cn/insert");
ContentValues values = new ContentValues();
values.put("count", 400);
values.put("name", "小华");
// values:要修改字段的值
// * whereClause:要修改的条件
// * whereArgs:条件的值
Uri insert = contentResolver.insert(uri, values);
if (insert!=null) {
Toast.makeText(MainActivity.this, "添加成功", Toast.LENGTH_SHORT).show();
}
}
});
//修改
findViewById(R.id.btn4).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
ContentResolver contentResolver = getContentResolver();
Uri uri = Uri.parse("content://com.beicai.cn/update");
ContentValues values = new ContentValues();
values.put("count", +100);
int update = contentResolver.update(uri, values, "name='小华'", null);
// MySQLHelper mySQLHelper = new MySQLHelper(MainActivity.this);
if (update>0) {
Toast.makeText(MainActivity.this, "修改成功", 0).show();
}
}
});
//删除
findViewById(R.id.btn3).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
ContentResolver contentResolver = getContentResolver();
Uri uri = Uri.parse("content://com.beicai.cn/delete");
int delete = contentResolver.delete(uri, "name='小华'", null);
if (delete>0) {
Toast.makeText(MainActivity.this, "删除成功", 0).show();
}
}
});
}
}
···
**********************系统定义***********
写一个类保存信息
···
public class MusicBean {
int id;
String tilte;
String album;
long size;
String artist;
String url;
int duration;
public MusicBean(int id, String tilte, String album, long size, String artist,
String url, int duration) {
super();
this.id = id;
this.tilte = tilte;
this.album = album;
this.size = size;
this.artist = artist;
this.url = url;
this.duration = duration;
}
@Override
public String toString() {
return "MusicBean [id=" + id + ", tilte=" + tilte + ", album=" + album
+ ", size=" + size + ", artist=" + artist + ", url=" + url
+ ", duration=" + duration + "]";
}
}
···
清单文件中设置权限:
···
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
···
****************MainActivity中****************
···
public class MainActivity extends Activity {
private ArrayList<MusicBean> list =new ArrayList<MusicBean>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.btn1).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//内容解析者
ContentResolver contentResolver = getContentResolver();
Cursor cursor = contentResolver.query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, null, null, null, null);
if (cursor!=null) {
cursor.moveToFirst();
cursor.moveToPrevious();
while (cursor.moveToNext()) {
//歌曲ID:MediaStore.Audio.Media._ID
int id = cursor.getInt(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media._ID));
//歌曲的名称 :MediaStore.Audio.Media.TITLE
String tilte = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.TITLE));
//歌曲的专辑名:MediaStore.Audio.Media.ALBUM
String album = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.ALBUM));
//歌曲文件的大小 :MediaStore.Audio.Media.SIZE
long size = cursor.getLong(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.SIZE));
//歌曲的歌手名: MediaStore.Audio.Media.ARTIST
String artist = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.ARTIST));
//歌曲文件的路径 :MediaStore.Audio.Media.DATA
String url = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA));
//歌曲的总播放时长 :MediaStore.Audio.Media.DURATION
int duration = cursor.getInt(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DURATION));
MusicBean musicBean = new MusicBean(id, tilte, album, size, artist, url, duration);
list.add(musicBean);
}
TextView tv = (TextView) findViewById(R.id.tv);
tv.setText(list.toString());
// for (int i = 0; i < list.size(); i++) {
// MusicBean musicBean = list.get(i);
//
// }
}
}
});
}
}
···
网友评论