参考:http://blog.csdn.net/yan8024/article/details/6444368
package com.HelloWorld;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import android.content.ContentProvider;
import android.content.ContentValues;
import android.content.Context;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.os.Environment;
public class MyProvider extends ContentProvider {
private final static String DB_DIR = "lp";
private final static String DB_NAME = "contacts.db";
private final static String TABLE_NAME = "PRMContacts";
private Context mContext;
private SQLiteDatabase db ;
private static final String AUTHORITY = "com.helloword.myprovider" ;
private static UriMatcher uriMatcher;
private static final int ONE = 1 ;
private static final int MORE = 2 ;
static
{
// 添加访问ContentProvider的Uri
uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
uriMatcher.addURI(AUTHORITY, "one" , ONE);
uriMatcher.addURI(AUTHORITY, "more/*" , MORE);
}
@Override
public int delete(Uri uri, String selection, String[] selectionArgs) {
// TODO Auto-generated method stub
return 0;
}
@Override
public String getType(Uri uri) {
// TODO Auto-generated method stub
return null;
}
@Override
public Uri insert(Uri uri, ContentValues values) {
// TODO Auto-generated method stub
return null;
}
@Override
public boolean onCreate() {
mContext = getContext();
db = openDatabase();
return true;
}
@Override
public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
Cursor cursor = null;
System.out.println("query");
switch(uriMatcher.match(uri)){
case ONE:
cursor = db.query(TABLE_NAME, projection, selection, selectionArgs, null, null, sortOrder);
break;
case MORE:
String word = uri.getPathSegments().get(1);
cursor = db.rawQuery("select * from "+TABLE_NAME+" where displayname like ?", new String[]{word+"%"});
break;
default:
throw new IllegalArgumentException("无效参数");
}
return cursor;
}
@Override
public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
// TODO Auto-generated method stub
return 0;
}
private SQLiteDatabase openDatabase()
{
if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
FileOutputStream fos = null;
InputStream is = null;
try
{
String path = Environment.getExternalStorageDirectory().getAbsolutePath() + "/"+DB_DIR;
// 获得dictionary.db文件的绝对路径
String databaseFilename = path + "/" + DB_NAME;
File dir = new File(path);
if(!dir.exists()){
dir.mkdir();
}
File db = new File(databaseFilename);
if(!db.exists()){
fos = new FileOutputStream(db);
is = mContext.getResources().openRawResource(R.raw.contacts);
byte[] buffer = new byte[1024];
int length = 0;
while((length = is.read(buffer))!=-1){
fos.write(buffer, 0, length);
}
}
SQLiteDatabase database = SQLiteDatabase.openOrCreateDatabase(databaseFilename, null);
return database;
}
catch (Exception e){
}finally{
if(fos!=null){
try {
fos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(is!=null){
try {
is.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
return null;
}
}
玩转Android Provider
最新推荐文章于 2022-11-26 07:00:00 发布