Provider中查询的一个Cursor对象问题

本文介绍了一种在Android开发中ContentProvider组件遇到的跨进程Cursor传递问题及其解决方案。详细解释了为何某些Cursor对象无法正确跨进程传递,并提供了一个有效的方法来避免这个问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

文章转自:http://archive.cnblogs.com/a/1984501/<wbr style="line-height:25px"><div style="line-height:25px"> <span style="line-height:25px; font-family:verdana,Arial,Helvetica"></span> <p style="line-height:25px; margin-top:5px; margin-right:auto; margin-bottom:5px; margin-left:auto; padding-top:0px; padding-bottom:0px; font-size:14px"> 昨天写一个provider的时候出现了如下问题:</p> <p style="line-height:25px; margin-top:5px; margin-right:auto; margin-bottom:5px; margin-left:auto; padding-top:0px; padding-bottom:0px; font-size:14px">   写完之后在同一个进程内使用provider完全没有问题,但是一旦到其他进程(另一个apk)里使用的时候就会报错</p> <div style="line-height:23px; background-color:rgb(245,245,245); font-family:'Courier New'; font-size:13px; border-top-width:1px; border-right-width:1px; border-bottom-width:1px; border-left-width:1px; border-top-style:solid; border-right-style:solid; border-bottom-style:solid; border-left-style:solid; border-top-color:rgb(204,204,204); border-right-color:rgb(204,204,204); border-bottom-color:rgb(204,204,204); border-left-color:rgb(204,204,204); padding-top:5px; padding-right:5px; padding-bottom:5px; padding-left:5px; word-break:break-all; overflow-x:auto; overflow-y:auto"> <pre style="line-height:1.8; margin-top:0px; margin-bottom:0px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; font-family:verdana,Arial,Helvetica; font-size:14px; color:rgb(0,0,0)"><div style="line-height:25px"> <span style="line-height:25px; font-family:'Courier New'; color:rgb(0,0,0)">E</span><span style="line-height:25px; font-family:'Courier New'; color:rgb(0,0,0)">/</span><span style="line-height:25px; font-family:'Courier New'; color:rgb(0,0,0)">DatabaseUtils( </span><span style="line-height:25px; font-family:'Courier New'; color:rgb(128,0,128)">339</span><span style="line-height:25px; font-family:'Courier New'; color:rgb(0,0,0)">): Writing exception to parcel<br style="line-height:10px">E</span><span style="line-height:25px; font-family:'Courier New'; color:rgb(0,0,0)">/</span><span style="line-height:25px; font-family:'Courier New'; color:rgb(0,0,0)">DatabaseUtils( </span><span style="line-height:25px; font-family:'Courier New'; color:rgb(128,0,128)">339</span><span style="line-height:25px; font-family:'Courier New'; color:rgb(0,0,0)">): java.lang.UnsupportedOperationException: Only CrossProcessCursor cursors are supported across process </span><span style="line-height:25px; font-family:'Courier New'; color:rgb(0,0,255)">for</span><span style="line-height:25px; font-family:'Courier New'; color:rgb(0,0,0)"> now<br style="line-height:10px">E</span><span style="line-height:25px; font-family:'Courier New'; color:rgb(0,0,0)">/</span><span style="line-height:25px; font-family:'Courier New'; color:rgb(0,0,0)">DatabaseUtils( </span><span style="line-height:25px; font-family:'Courier New'; color:rgb(128,0,128)">339</span><span style="line-height:25px; font-family:'Courier New'; color:rgb(0,0,0)">): at android.database.CursorToBulkCursorAdaptor.</span><span style="line-height:25px; font-family:'Courier New'; color:rgb(0,0,0)">&lt;</span><span style="line-height:25px; font-family:'Courier New'; color:rgb(0,0,0)">init</span><span style="line-height:25px; font-family:'Courier New'; color:rgb(0,0,0)">&gt;</span><span style="line-height:25px; font-family:'Courier New'; color:rgb(0,0,0)">(CursorToBulkCursorAdaptor.java:</span><span style="line-height:25px; font-family:'Courier New'; color:rgb(128,0,128)">97</span><span style="line-height:25px; font-family:'Courier New'; color:rgb(0,0,0)">)<br style="line-height:10px">E</span><span style="line-height:25px; font-family:'Courier New'; color:rgb(0,0,0)">/</span><span style="line-height:25px; font-family:'Courier New'; color:rgb(0,0,0)">DatabaseUtils( </span><span style="line-height:25px; font-family:'Courier New'; color:rgb(128,0,128)">339</span><span style="line-height:25px; font-family:'Courier New'; color:rgb(0,0,0)">): at android.content.ContentProvider$Transport.bulkQuery(ContentProvider.java:</span><span style="line-height:25px; font-family:'Courier New'; color:rgb(128,0,128)">122</span><span style="line-height:25px; font-family:'Courier New'; color:rgb(0,0,0)">)<br style="line-height:10px">E</span><span style="line-height:25px; font-family:'Courier New'; color:rgb(0,0,0)">/</span><span style="line-height:25px; font-family:'Courier New'; color:rgb(0,0,0)">DatabaseUtils( </span><span style="line-height:25px; font-family:'Courier New'; color:rgb(128,0,128)">339</span><span style="line-height:25px; font-family:'Courier New'; color:rgb(0,0,0)">): at android.content.ContentProviderNative.onTransact(ContentProviderNative.java:</span><span style="line-height:25px; font-family:'Courier New'; color:rgb(128,0,128)">98</span><span style="line-height:25px; font-family:'Courier New'; color:rgb(0,0,0)">)<br style="line-height:10px">E</span><span style="line-height:25px; font-family:'Courier New'; color:rgb(0,0,0)">/</span><span style="line-height:25px; font-family:'Courier New'; color:rgb(0,0,0)">DatabaseUtils( </span><span style="line-height:25px; font-family:'Courier New'; color:rgb(128,0,128)">339</span><span style="line-height:25px; font-family:'Courier New'; color:rgb(0,0,0)">): at android.os.Binder.execTransact(Binder.java:</span><span style="line-height:25px; font-family:'Courier New'; color:rgb(128,0,128)">287</span><span style="line-height:25px; font-family:'Courier New'; color:rgb(0,0,0)">)<br style="line-height:10px">E</span><span style="line-height:25px; font-family:'Courier New'; color:rgb(0,0,0)">/</span><span style="line-height:25px; font-family:'Courier New'; color:rgb(0,0,0)">DatabaseUtils( </span><span style="line-height:25px; font-family:'Courier New'; color:rgb(128,0,128)">339</span><span style="line-height:25px; font-family:'Courier New'; color:rgb(0,0,0)">): at dalvik.system.NativeStart.run(Native Method)<br style="line-height:10px">E</span><span style="line-height:25px; font-family:'Courier New'; color:rgb(0,0,0)">/</span><span style="line-height:25px; font-family:'Courier New'; color:rgb(0,0,0)">DatabaseUtils( </span><span style="line-height:25px; font-family:'Courier New'; color:rgb(128,0,128)">339</span><span style="line-height:25px; font-family:'Courier New'; color:rgb(0,0,0)">): Caused by: java.lang.ClassCastException: android.content.ContentResolver$CursorWrapperInner<br style="line-height:10px">E</span><span style="line-height:25px; font-family:'Courier New'; color:rgb(0,0,0)">/</span><span style="line-height:25px; font-family:'Courier New'; color:rgb(0,0,0)">DatabaseUtils( </span><span style="line-height:25px; font-family:'Courier New'; color:rgb(128,0,128)">339</span><span style="line-height:25px; font-family:'Courier New'; color:rgb(0,0,0)">): at android.database.CursorToBulkCursorAdaptor.</span><span style="line-height:25px; font-family:'Courier New'; color:rgb(0,0,0)">&lt;</span><span style="line-height:25px; font-family:'Courier New'; color:rgb(0,0,0)">init</span><span style="line-height:25px; font-family:'Courier New'; color:rgb(0,0,0)">&gt;</span><span style="line-height:25px; font-family:'Courier New'; color:rgb(0,0,0)">(CursorToBulkCursorAdaptor.java:</span><span style="line-height:25px; font-family:'Courier New'; color:rgb(128,0,128)">81</span><span style="line-height:25px; font-family:'Courier New'; color:rgb(0,0,0)">)</span> </div></pre> </div> <p style="line-height:25px; margin-top:5px; margin-right:auto; margin-bottom:5px; margin-left:auto; padding-top:0px; padding-bottom:0px; font-size:14px"> 这就奇怪了,ContentProvider不就是问了跨进程才出现的啊。现在已跨进程就出问题。。。。那不就相当与没用了。</p> <p style="line-height:25px; margin-top:5px; margin-right:auto; margin-bottom:5px; margin-left:auto; padding-top:0px; padding-bottom:0px; font-size:14px"> provider的query方法里面大概是这样写的</p> <div style="line-height:23px; background-color:rgb(245,245,245); font-family:'Courier New'; font-size:13px; border-top-width:1px; border-right-width:1px; border-bottom-width:1px; border-left-width:1px; border-top-style:solid; border-right-style:solid; border-bottom-style:solid; border-left-style:solid; border-top-color:rgb(204,204,204); border-right-color:rgb(204,204,204); border-bottom-color:rgb(204,204,204); border-left-color:rgb(204,204,204); padding-top:5px; padding-right:5px; padding-bottom:5px; padding-left:5px; word-break:break-all; overflow-x:auto; overflow-y:auto"> <pre style="line-height:1.8; margin-top:0px; margin-bottom:0px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; font-family:verdana,Arial,Helvetica; font-size:14px; color:rgb(0,0,0)"><div style="line-height:25px"> <span style="line-height:25px; font-family:'Courier New'; color:rgb(0,0,0)">String whereInext </span><span style="line-height:25px; font-family:'Courier New'; color:rgb(0,0,0)">=</span><span style="line-height:25px; font-family:'Courier New'; color:rgb(0,0,0)"> </span><span style="line-height:25px; font-family:'Courier New'; color:rgb(0,0,0)">""</span><span style="line-height:25px; font-family:'Courier New'; color:rgb(0,0,0)">;<br style="line-height:10px"></span><span style="line-height:25px; font-family:'Courier New'; color:rgb(0,0,255)">if</span><span style="line-height:25px; font-family:'Courier New'; color:rgb(0,0,0)">(number.indexOf(</span><span style="line-height:25px; font-family:'Courier New'; color:rgb(0,0,0)">"</span><span style="line-height:25px; font-family:'Courier New'; color:rgb(0,0,0)">1</span><span style="line-height:25px; font-family:'Courier New'; color:rgb(0,0,0)">"</span><span style="line-height:25px; font-family:'Courier New'; color:rgb(0,0,0)">) </span><span style="line-height:25px; font-family:'Courier New'; color:rgb(0,0,0)">&gt;=</span><span style="line-height:25px; font-family:'Courier New'; color:rgb(0,0,0)"> </span><span style="line-height:25px; font-family:'Courier New'; color:rgb(0,0,0)">0</span><span style="line-height:25px; font-family:'Courier New'; color:rgb(0,0,0)"> </span><span style="line-height:25px; font-family:'Courier New'; color:rgb(0,0,0)">||</span><span style="line-height:25px; font-family:'Courier New'; color:rgb(0,0,0)"> number.indexOf(</span><span style="line-height:25px; font-family:'Courier New'; color:rgb(0,0,0)">"</span><span style="line-height:25px; font-family:'Courier New'; color:rgb(0,0,0)">0</span><span style="line-height:25px; font-family:'Courier New'; color:rgb(0,0,0)">"</span><span style="line-height:25px; font-family:'Courier New'; color:rgb(0,0,0)">) </span><span style="line-height:25px; font-family:'Courier New'; color:rgb(0,0,0)">&gt;=</span><span style="line-height:25px; font-family:'Courier New'; color:rgb(0,0,0)"> </span><span style="line-height:25px; font-family:'Courier New'; color:rgb(0,0,0)">0</span><span style="line-height:25px; font-family:'Courier New'; color:rgb(0,0,0)">){<br style="line-height:10px"></span><span style="line-height:25px; font-family:'Courier New'; color:rgb(0,128,0)">//</span><span style="line-height:25px; font-family:'Courier New'; color:rgb(0,128,0)">search the contacts directly,no need to get the where at all</span><span style="line-height:25px; font-family:'Courier New'; color:rgb(0,128,0)"><br style="line-height:10px"></span><span style="line-height:25px; font-family:'Courier New'; color:rgb(0,0,0)"> </span><span style="line-height:25px; font-family:'Courier New'; color:rgb(0,0,255)">return</span><span style="line-height:25px; font-family:'Courier New'; color:rgb(0,0,0)"> </span><span style="line-height:25px; font-family:'Courier New'; color:rgb(0,0,255)">null</span><span style="line-height:25px; font-family:'Courier New'; color:rgb(0,0,0)"> ;<br style="line-height:10px"> } </span><span style="line-height:25px; font-family:'Courier New'; color:rgb(0,0,255)">else</span><span style="line-height:25px; font-family:'Courier New'; color:rgb(0,0,0)"> {<br style="line-height:10px"> whereInext </span><span style="line-height:25px; font-family:'Courier New'; color:rgb(0,0,0)">=</span><span style="line-height:25px; font-family:'Courier New'; color:rgb(0,0,0)"> </span><span style="line-height:25px; font-family:'Courier New'; color:rgb(0,0,255)">this</span><span style="line-height:25px; font-family:'Courier New'; color:rgb(0,0,0)">.getPinYinWhere(number);<br style="line-height:10px"> }<br style="line-height:10px"><br style="line-height:10px"></span><span style="line-height:25px; font-family:'Courier New'; color:rgb(0,0,255)">return</span><span style="line-height:25px; font-family:'Courier New'; color:rgb(0,0,0)"> db.query(TABLE_CONTACT_INDEX, </span><span style="line-height:25px; font-family:'Courier New'; color:rgb(0,0,255)">null</span><span style="line-height:25px; font-family:'Courier New'; color:rgb(0,0,0)">, whereInext, </span><span style="line-height:25px; font-family:'Courier New'; color:rgb(0,0,255)">null</span><span style="line-height:25px; font-family:'Courier New'; color:rgb(0,0,0)">, </span><span style="line-height:25px; font-family:'Courier New'; color:rgb(0,0,255)">null</span><span style="line-height:25px; font-family:'Courier New'; color:rgb(0,0,0)">, </span><span style="line-height:25px; font-family:'Courier New'; color:rgb(0,0,255)">null</span><span style="line-height:25px; font-family:'Courier New'; color:rgb(0,0,0)"> , </span><span style="line-height:25px; font-family:'Courier New'; color:rgb(0,0,255)">null</span><span style="line-height:25px; font-family:'Courier New'; color:rgb(0,0,0)">);<br style="line-height:10px"></span> </div></pre> </div> <p style="line-height:25px; margin-top:5px; margin-right:auto; margin-bottom:5px; margin-left:auto; padding-top:0px; padding-bottom:0px; font-size:14px"> 看 上去好像没错。。。返回的也是一个Cursor对象。。。其实不然。。。代码稍作修改就行了,正确的代码如下:</p> <div style="line-height:23px; background-color:rgb(245,245,245); font-family:'Courier New'; font-size:13px; border-top-width:1px; border-right-width:1px; border-bottom-width:1px; border-left-width:1px; border-top-style:solid; border-right-style:solid; border-bottom-style:solid; border-left-style:solid; border-top-color:rgb(204,204,204); border-right-color:rgb(204,204,204); border-bottom-color:rgb(204,204,204); border-left-color:rgb(204,204,204); padding-top:5px; padding-right:5px; padding-bottom:5px; padding-left:5px; word-break:break-all; overflow-x:auto; overflow-y:auto"> <pre style="line-height:1.8; margin-top:0px; margin-bottom:0px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; font-family:verdana,Arial,Helvetica; font-size:14px; color:rgb(0,0,0)"><div style="line-height:25px"> <span style="line-height:25px; font-family:'Courier New'; color:rgb(0,0,0)"> String whereInext </span><span style="line-height:25px; font-family:'Courier New'; color:rgb(0,0,0)">=</span><span style="line-height:25px; font-family:'Courier New'; color:rgb(0,0,0)"> </span><span style="line-height:25px; font-family:'Courier New'; color:rgb(0,0,0)">""</span><span style="line-height:25px; font-family:'Courier New'; color:rgb(0,0,0)">;<br style="line-height:10px"></span><span style="line-height:25px; font-family:'Courier New'; color:rgb(0,0,255)">if</span><span style="line-height:25px; font-family:'Courier New'; color:rgb(0,0,0)">(number.indexOf(</span><span style="line-height:25px; font-family:'Courier New'; color:rgb(0,0,0)">"</span><span style="line-height:25px; font-family:'Courier New'; color:rgb(0,0,0)">1</span><span style="line-height:25px; font-family:'Courier New'; color:rgb(0,0,0)">"</span><span style="line-height:25px; font-family:'Courier New'; color:rgb(0,0,0)">) </span><span style="line-height:25px; font-family:'Courier New'; color:rgb(0,0,0)">&gt;=</span><span style="line-height:25px; font-family:'Courier New'; color:rgb(0,0,0)"> </span><span style="line-height:25px; font-family:'Courier New'; color:rgb(0,0,0)">0</span><span style="line-height:25px; font-family:'Courier New'; color:rgb(0,0,0)"> </span><span style="line-height:25px; font-family:'Courier New'; color:rgb(0,0,0)">||</span><span style="line-height:25px; font-family:'Courier New'; color:rgb(0,0,0)"> number.indexOf(</span><span style="line-height:25px; font-family:'Courier New'; color:rgb(0,0,0)">"</span><span style="line-height:25px; font-family:'Courier New'; color:rgb(0,0,0)">0</span><span style="line-height:25px; font-family:'Courier New'; color:rgb(0,0,0)">"</span><span style="line-height:25px; font-family:'Courier New'; color:rgb(0,0,0)">) </span><span style="line-height:25px; font-family:'Courier New'; color:rgb(0,0,0)">&gt;=</span><span style="line-height:25px; font-family:'Courier New'; color:rgb(0,0,0)"> </span><span style="line-height:25px; font-family:'Courier New'; color:rgb(0,0,0)">0</span><span style="line-height:25px; font-family:'Courier New'; color:rgb(0,0,0)">){<br style="line-height:10px"></span><span style="line-height:25px; font-family:'Courier New'; color:rgb(0,128,0)">//</span><span style="line-height:25px; font-family:'Courier New'; color:rgb(0,128,0)">search the contacts directly,no need to get the where at all</span><span style="line-height:25px; font-family:'Courier New'; color:rgb(0,128,0)"><br style="line-height:10px"></span><span style="line-height:25px; font-family:'Courier New'; color:rgb(0,0,0)"> </span><span style="line-height:25px; font-family:'Courier New'; color:rgb(0,0,255)">return</span><span style="line-height:25px; font-family:'Courier New'; color:rgb(0,0,0)"> </span><span style="line-height:25px; font-family:'Courier New'; color:rgb(0,0,255)">null</span><span style="line-height:25px; font-family:'Courier New'; color:rgb(0,0,0)"> ;<br style="line-height:10px"> } </span><span style="line-height:25px; font-family:'Courier New'; color:rgb(0,0,255)">else</span><span style="line-height:25px; font-family:'Courier New'; color:rgb(0,0,0)"> {<br style="line-height:10px"> whereInext </span><span style="line-height:25px; font-family:'Courier New'; color:rgb(0,0,0)">=</span><span style="line-height:25px; font-family:'Courier New'; color:rgb(0,0,0)"> </span><span style="line-height:25px; font-family:'Courier New'; color:rgb(0,0,255)">this</span><span style="line-height:25px; font-family:'Courier New'; color:rgb(0,0,0)">.getPinYinWhere(number);<br style="line-height:10px"> }<br style="line-height:10px"><br style="line-height:10px"> SQLiteQueryBuilder sqlBuilder </span><span style="line-height:25px; font-family:'Courier New'; color:rgb(0,0,0)">=</span><span style="line-height:25px; font-family:'Courier New'; color:rgb(0,0,0)"> </span><span style="line-height:25px; font-family:'Courier New'; color:rgb(0,0,255)">new</span><span style="line-height:25px; font-family:'Courier New'; color:rgb(0,0,0)"> SQLiteQueryBuilder();<br style="line-height:10px"> sqlBuilder.setTables(TABLE_CONTACT_INDEX);<br style="line-height:10px"> Cursor cursor </span><span style="line-height:25px; font-family:'Courier New'; color:rgb(0,0,0)">=</span><span style="line-height:25px; font-family:'Courier New'; color:rgb(0,0,0)"> sqlBuilder.query(db, </span><span style="line-height:25px; font-family:'Courier New'; color:rgb(0,0,255)">null</span><span style="line-height:25px; font-family:'Courier New'; color:rgb(0,0,0)">, whereInext, </span><span style="line-height:25px; font-family:'Courier New'; color:rgb(0,0,255)">null</span><span style="line-height:25px; font-family:'Courier New'; color:rgb(0,0,0)"> , </span><span style="line-height:25px; font-family:'Courier New'; color:rgb(0,0,255)">null</span><span style="line-height:25px; font-family:'Courier New'; color:rgb(0,0,0)"> , </span><span style="line-height:25px; font-family:'Courier New'; color:rgb(0,0,255)">null</span><span style="line-height:25px; font-family:'Courier New'; color:rgb(0,0,0)"> , </span><span style="line-height:25px; font-family:'Courier New'; color:rgb(0,0,255)">null</span><span style="line-height:25px; font-family:'Courier New'; color:rgb(0,0,0)">);<br style="line-height:10px"> Log.d(</span><span style="line-height:25px; font-family:'Courier New'; color:rgb(0,0,0)">"</span><span style="line-height:25px; font-family:'Courier New'; color:rgb(0,0,0)">cx</span><span style="line-height:25px; font-family:'Courier New'; color:rgb(0,0,0)">"</span><span style="line-height:25px; font-family:'Courier New'; color:rgb(0,0,0)">,</span><span style="line-height:25px; font-family:'Courier New'; color:rgb(0,0,0)">"</span><span style="line-height:25px; font-family:'Courier New'; color:rgb(0,0,0)">--------number is-------</span><span style="line-height:25px; font-family:'Courier New'; color:rgb(0,0,0)">"</span><span style="line-height:25px; font-family:'Courier New'; color:rgb(0,0,0)">+</span><span style="line-height:25px; font-family:'Courier New'; color:rgb(0,0,0)">number</span><span style="line-height:25px; font-family:'Courier New'; color:rgb(0,0,0)">+</span><span style="line-height:25px; font-family:'Courier New'; color:rgb(0,0,0)">"</span><span style="line-height:25px; font-family:'Courier New'; color:rgb(0,0,0)">-----where from number is-----</span><span style="line-height:25px; font-family:'Courier New'; color:rgb(0,0,0)">"</span><span style="line-height:25px; font-family:'Courier New'; color:rgb(0,0,0)">+</span><span style="line-height:25px; font-family:'Courier New'; color:rgb(0,0,0)">whereInext);<br style="line-height:10px"><br style="line-height:10px"></span><span style="line-height:25px; font-family:'Courier New'; color:rgb(0,0,255)">return</span><span style="line-height:25px; font-family:'Courier New'; color:rgb(0,0,0)"> cursor;</span> </div></pre> </div> <p style="line-height:25px; margin-top:5px; margin-right:auto; margin-bottom:5px; margin-left:auto; padding-top:0px; padding-bottom:0px; font-size:14px"> 同样都是返回的Cursor对象。。。为什么上一种方法就会出问题呢。。</p> <p style="line-height:25px; margin-top:5px; margin-right:auto; margin-bottom:5px; margin-left:auto; padding-top:0px; padding-bottom:0px; font-size:14px"> 其实是这样的:</p> <p style="line-height:25px; margin-top:5px; margin-right:auto; margin-bottom:5px; margin-left:auto; padding-top:0px; padding-bottom:0px; font-size:14px"> 这就是java的多态,返回的都是一个Cursor对象。。。这就是为什么向下转型是不安全的。。。</p> <p style="line-height:25px; margin-top:5px; margin-right:auto; margin-bottom:5px; margin-left:auto; padding-top:0px; padding-bottom:0px; font-size:14px"> provider将cursor跨进程传递时将会强制向下转换为CrossProcessCursor类型。。。。</p> <p style="line-height:25px; margin-top:5px; margin-right:auto; margin-bottom:5px; margin-left:auto; padding-top:0px; padding-bottom:0px; font-size:14px"> 就在这时候,只有第二种方法中返回的cursor原始的类型 is a CrossProcessCursor,然而第一种方法返回的curso对象而不是</p> <p style="line-height:25px; margin-top:5px; margin-right:auto; margin-bottom:5px; margin-left:auto; padding-top:0px; padding-bottom:0px; font-size:14px"> 所以在这时候,第一种方法强制类型转换会报错。。。。第二种方法则没有问题,可以转换后跨进程传递!</p> <p style="line-height:25px; margin-top:5px; margin-right:auto; margin-bottom:5px; margin-left:auto; padding-top:0px; padding-bottom:0px; font-size:14px"> <span style="line-height:25px">注意1</span>:<span style="color:#000080; line-height:25px">按照作者的意思第一种方式<span style="line-height:25px; font-family:'Courier New'; white-space:pre">db.query返回的</span>cursor不是个CrossProcessCursor,所以出错。但是我在2.2中测试来看它返回的是CrossProcessCursor。之后查阅2.2中SQLiteDatabase的源码,它返回的是SQLiteCursor,SQLiteCursor继承于AbstractWindowedCursor,而AbstractWindowedCursor又继承于AbstractCursor,而AbstractCursor又是实现了CrossProcessCursor。它和测试结果相符合。作者的Android可能是更早期的版本,才引起他所说的问题。</span></p> <p style="line-height:25px; margin-top:5px; margin-right:auto; margin-bottom:5px; margin-left:auto; padding-top:0px; padding-bottom:0px; font-size:14px"> <span style="color:#000080; line-height:25px">另外我才</span><span style="color:#993300; line-height:25px">android4.0</span><span style="color:#000080; line-height:25px">源码中看到无论</span><span style="color:#0000ff; line-height:25px">SQLiteDatabase</span><span style="color:#000080; line-height:25px">还是</span><span style="color:#0000ff; line-height:25px">SQLiteQueryBuilder</span><span style="color:#000080; line-height:25px">都是调用</span><span style="color:#0000ff; line-height:25px">SQLiteDatabase</span><span style="color:#000080; line-height:25px">的</span><span style="color:#ff9900; line-height:25px">rawQueryWithFactory()</span><span style="color:#000080; line-height:25px">方法来进行查询的,且返回类型都是声明为</span><span style="color:#ff9900; line-height:25px">Cursor.</span></p> <p style="line-height:25px; margin-top:5px; margin-right:auto; margin-bottom:5px; margin-left:auto; padding-top:0px; padding-bottom:0px; font-size:14px"> <span style="line-height:25px">注意2</span>:ContentProvider在进程中传递Cursor对象,需要将其转化<span style="color:#0000ff; line-height:25px">CursorToBulkCursorAdaptor</span>。(具体可以参照源码ContentProviderNative的<span style="color:#0000ff; line-height:25px">bulkQuery</span>函数)</p> <p style="line-height:25px; margin-top:5px; margin-right:auto; margin-bottom:5px; margin-left:auto; padding-top:0px; padding-bottom:0px; font-size:14px"> <span style="color:#000080; line-height:25px">但是</span>其实我通过<span style="color:#0000ff; line-height:25px">ContentResolver</span>最后查询返回所得的Cursor对象<span style="color:#000080; line-height:25px">并不是</span><span style="color:#0000ff; line-height:25px">CursorToBulkCursorAdaptor</span>。</p> </div> </wbr>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值