当前位置:编程学习 > JAVA >>

ContentProvider的基本使用

ContentProvider:内容提供者。她主要完成这么一件事:共享数据。将一个应用的数据共享给其他应用
 
 
 
代码如下:
 
1、SQLiteProvider
 
 
[java]  
package com.njupt.practice_contentprovider1.provider;  
  
import com.njupt.practice_contentprovider1.dao.DBOpenHelper;  
  
import android.content.ContentProvider;  
import android.content.ContentUris;  
import android.content.ContentValues;  
import android.content.UriMatcher;  
import android.database.Cursor;  
import android.database.sqlite.SQLiteDatabase;  
import android.net.Uri;  
  
public class SQliteProvider extends ContentProvider{  
  
    private UriMatcher matcher = new UriMatcher(UriMatcher.NO_MATCH);  
    private DBOpenHelper helper;  
    public static final int PERSON = 1;  
    public static final int PERSON_ID = 2;  
      
    @Override  
    public boolean onCreate() {  
        helper = new DBOpenHelper(getContext());  
        matcher.addURI("jd_love_ztmm","person",PERSON);  
        matcher.addURI("jd_love_ztmm", "person/#", PERSON_ID);  
        return true;  
    }  
      
    @Override  
    public Cursor query(Uri uri, String[] projection, String selection,  
            String[] selectionArgs, String sortOrder) {  
          
        SQLiteDatabase db = helper.getReadableDatabase();  
        switch(matcher.match(uri)){  
        case PERSON_ID:  
            long id = ContentUris.parseId(uri);  
            selection = selection == null ? "id = " + id : selection + " AND id = " + id;  
        case PERSON:  
            return db.query("person", null, selection, selectionArgs, null, null, sortOrder);  
        default:  
            throw new RuntimeException("Uri不能识别: " + uri);  
        }  
    }  
      
    @Override  
    public Uri insert(Uri uri, ContentValues values) {  
        SQLiteDatabase db =  helper.getWritableDatabase();  
        switch(matcher.match(uri)){  
        case PERSON:  
            long id = db.insert("person", "id", values);  
            return ContentUris.withAppendedId(uri, id);  
        default:  
            throw new RuntimeException("Uri不能识别: " + uri);  
        }  
          
    }  
      
    @Override  
    public int delete(Uri uri, String selection, String[] selectionArgs) {  
          
        SQLiteDatabase db = helper.getWritableDatabase();  
        switch(matcher.match(uri)){  
            case PERSON_ID:  
                long id = ContentUris.parseId(uri);  
                selection = selection == null ? "id = " + id : selection + " AND id = " + id;  
            case PERSON:  
                return db.delete("person", selection, selectionArgs);  
            default:  
                throw new RuntimeException("Uri不能识别: " + uri);  
        }  
    }  
      
      
    @Override  
    public int update(Uri uri, ContentValues values, String selection,  
            String[] selectionArgs) {  
        SQLiteDatabase db = helper.getWritableDatabase();  
        switch(matcher.match(uri)){  
        case PERSON_ID:  
            long id = ContentUris.parseId(uri);  
            selection = selection == null ? "id = " + id : selection + "AND id = " + id;   
        case PERSON:  
            return db.update("person", values, selection, selectionArgs);  
        default:  
            throw new RuntimeException("Uri不能识别: " + uri);  
        }  
    }  
      
      
    @Override  
    public String getType(Uri uri) {  
        // TODO Auto-generated method stub  
        return null;  
    }  
}  
 
 
2、ProviderTest
 
测试类
 
 
[java 
package com.njupt.pc1;  
  
import android.content.ContentProvider;  
import android.content.ContentResolver;  
import android.content.ContentValues;  
import android.database.Cursor;  
import android.net.Uri;  
import android.test.AndroidTestCase;  
  
public class ProviderTest extends AndroidTestCase{  
  
    public void testQuery(){  
          
        ContentResolver resolver = getContext().getContentResolver();  
        Uri uri = Uri.parse("content://jd_love_ztmm/person");  
        Cursor c = resolver.query(uri,null,null,null,
补充:软件开发 , Java ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,