Android应用程序组件Content Provider在应用程序之间共享数据的原理分析(3)
16lz
2021-01-23
Step 11. CursorWindow.writeToParcel 这个函数定义在frameworks/base/core/java/android/database/CursorWindow.java文件中:
- publicclassCursorWindowextendsSQLiteClosableimplementsParcelable{
- ......
- publicvoidwriteToParcel(Parceldest,intflags){
- ......
- dest.writeStrongBinder(native_getBinder());
- ......
- }
- ......
- }
- staticjobjectnative_getBinder(JNIEnv*env,jobjectobject)
- {
- CursorWindow*window=GET_WINDOW(env,object);
- if(window){
- sp<IMemory>memory=window->getMemory();
- if(memory!=NULL){
- sp<IBinder>binder=memory->asBinder();
- returnjavaObjectForIBinder(env,binder);
- }
- }
- returnNULL;
- }
- #defineGET_WINDOW(env,object)((CursorWindow*)env->GetIntField(object,gWindowField))
- classCursorWindow
- {
- public:
- ......
- sp<IMemory>getMemory(){returnmMemory;}
- ......
- }
这一步其实就是前面Step 10的逆操作,把请求参数从数据流data中读取出来。这里我们同样是重点关注下面这两个参数读取的步骤:
- abstractpublicclassContentProviderNativeextendsBinderimplementsIContentProvider{
- ......
- @Override
- publicbooleanonTransact(intcode,Parceldata,Parcelreply,intflags)
- throwsRemoteException{
- try{
- switch(code){
- caseQUERY_TRANSACTION:
- {
- data.enforceInterface(IContentProvider.descriptor);
- Uriurl=Uri.CREATOR.createFromParcel(data);
- //String[]projection
- intnum=data.readInt();
- String[]projection=null;
- if(num>0){
- projection=newString[num];
- for(inti=0;i<num;i++){
- projection[i]=data.readString();
- }
- }
- //Stringselection,String[]selectionArgs...
- Stringselection=data.readString();
- num=data.readInt();
- String[]selectionArgs=null;
- if(num>0){
- selectionArgs=newString[num];
- for(inti=0;i<num;i++){
- selectionArgs[i]=data.readString();
- }
- }
- StringsortOrder=data.readString();
- IContentObserverobserver=IContentObserver.Stub.
- asInterface(data.readStrongBinder());
- CursorWindowwindow=CursorWindow.CREATOR.createFromParcel(data);
- //Flagforwhethercallerwantsthenumberof
- //rowsinthecursorandthepositionofthe
- //"_id"columnindex(or-1ifnon-existent)
- //Onlytobereturnedifbinder!=null.
- booleanwantsCursorMetadata=data.readInt()!=0;
- IBulkCursorbulkCursor=bulkQuery(url,projection,selection,
- selectionArgs,sortOrder,observer,window);
- reply.writeNoException();
- if(bulkCursor!=null){
- reply.writeStrongBinder(bulkCursor.asBinder());
- if(wantsCursorMetadata){
- reply.writeInt(bulkCursor.count());
- reply.writeInt(BulkCursorToCursorAdaptor.findRowIdColumnIndex(
- bulkCursor.getColumnNames()));
- }
- }else{
- reply.writeStrongBinder(null);
- }
- returntrue;
- }
- ......
- }
- }catch(Exceptione){
- DatabaseUtils.writeExceptionToParcel(reply,e);
- returntrue;
- }
- returnsuper.onTransact(code,data,reply,flags);
- }
- ......
- }
- CursorWindowwindow=CursorWindow.CREATOR.createFromParcel(data);
- //Flagforwhethercallerwantsthenumberof
- //rowsinthecursorandthepositionofthe
- //"_id"columnindex(or-1ifnon-existent)
- //Onlytobereturnedifbinder!=null.
- booleanwantsCursorMetadata=data.readInt()!=0;
- if(wantsCursorMetadata){
- reply.writeInt(bulkCursor.count());
- ......
- }
- publicclassCursorWindowextendsSQLiteClosableimplementsParcelable{
- ......
- privateCursorWindow(Parcelsource){
- IBindernativeBinder=source.readStrongBinder();
- ......
- native_init(nativeBinder);
- }
- ......
- publicstaticfinalParcelable.Creator<CursorWindow>CREATOR
- =newParcelable.Creator<CursorWindow>(){
- publicCursorWindowcreateFromParcel(Parcelsource){
- returnnewCursorWindow(source);
- }
- ......
- };
- ......
- }
更多相关文章
- Android中,把XML文件转换成Object对象的方法
- 初学Android,五大布局对象(六)
- Android中intent传递对象和Bundle的用法
- C语言函数以及函数的使用
- 理解Android回调函数
- Android 基本属性绘制文本对象FontMetrics介绍
- Android Camera 数据流
- android Activity之间传递对象
- FregClient进程,创建一个BpFregService类型的代理对象