1. ContentProvider的概念解析:
android官方指出数据存储的方式:Shared Preferences,网络存储,文件存储,外储存储,SQLite
这些存储都只是在单独的一个应用程序之中达到一个数据的共享。当我们操作其他应用程序的
一些数据,如操作系统里的媒体库,通讯录等,这时要通过ContentProvider来满足我们的要求。
小知识:
ContentProvider为存储和获取数据提供了统一的接口。对数据进行封装,不用关心数据存储的细节。
使用表的形式来组织数据。
使用ContentProvider 可以在不同的应用程序共享数据
android为常见的一些数据提供了默认ContentProvider(音频,视频,图片,通讯录等)
2. Uri介绍
为系统的每一个资源给其一个名字,比方说通话记录
每个ContentProvider都拥有一个公共的URI,这个URI用于表示这个ContentProvider所提供的数据
3.ContentResolver是对ContentProvider所提供的数据访问
4.运用:
package com.zking.db;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
/**
* Created by Administrator on 2017/7/17 0017.
*/
public class DBHelper extends SQLiteOpenHelper{
public DBHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
super(context, name, factory, version);
Log.i("test","构造方法");
}
@Override
public void onCreate(SQLiteDatabase db) {
Log.i("test","onCreate");
db.execSQL("create table login(uname,upass)");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Log.i("test","更新版本");
}
}
package com.zking.sqlite;
import android.database.sqlite.SQLiteDatabase;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import com.zking.db.DBHelper;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
DBHelper dbHelper=new DBHelper(this,"login.db",null,1);
SQLiteDatabase sqLiteDatabase=dbHelper.getWritableDatabase();
//添加
for (int i = 1; i <=5 ; i++) {
sqLiteDatabase.execSQL("insert into login(uname,upass) values('言"+i+"','12"+i+"')");
}
}
}
package com.zking.contentprovider;
import android.content.ContentProvider;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.Log;
import com.zking.db.DBHelper;
/**
* Created by Administrator on 2017/7/17 0017.
*/
public class SQLContentProvider extends ContentProvider{
@Override
public boolean onCreate() {
Log.i("test","onCreate");
return false;
}
@Nullable
@Override
public Cursor query(@NonNull Uri uri, @Nullable String[] projection, @Nullable String selection, @Nullable String[] selectionArgs, @Nullable String sortOrder) {
Log.i("test","query");
DBHelper dbHelper=new DBHelper(getContext(),"login.db",null,1);
SQLiteDatabase sqLiteDatabase=dbHelper.getWritableDatabase();
return sqLiteDatabase.rawQuery("select * from login",null);
}
@Nullable
@Override
public String getType(@NonNull Uri uri) {
Log.i("test","getType");
return null;
}
@Nullable
@Override
public Uri insert(@NonNull Uri uri, @Nullable ContentValues values) {
Log.i("test","insert");
return null;
}
@Override
public int delete(@NonNull Uri uri, @Nullable String selection, @Nullable String[] selectionArgs) {
Log.i("test","delete");
return 0;
}
@Override
public int update(@NonNull Uri uri, @Nullable ContentValues values, @Nullable String selection, @Nullable String[] selectionArgs) {
Log.i("test","update");
return 0;
}
}
配置:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.zking.sqlite">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<provider
android:authorities="com.zking.sqlite.LOGIN"
android:name="com.zking.contentprovider.SQLContentProvider"
android:exported="true"></provider>
</application>
</manifest>
调用数据
package com.zking.contentprovider;
import android.content.ContentResolver;
import android.database.Cursor;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//获取内容访问者
ContentResolver contentResolver=getContentResolver();
Uri uri=Uri.parse("content://com.zking.sqlite.LOGIN");
Cursor cursor=contentResolver.query(uri,null,null,null,null);
while (cursor.moveToNext()){
String uname=cursor.getString(cursor.getColumnIndex("uname"));
String upass=cursor.getString(cursor.getColumnIndex("upass"));
Log.i("test",uname+"\t\t\t:"+upass);
}
}
}