问题描述
我创建的ContentProvider这是另一个ContentProvider的代理(对于安全问题,并给获得的完整的应用程序功能的一部分)。
I’m creating ContentProvider which is a proxy of another ContentProvider (for security issues and to give access to part of functionality of full app).
public class GFContactsProvider extends ContactsProvider implements
DatabaseConstants {
private Context mContext;
private ContentResolver mContentResolver;
@Override
public boolean onCreate() {
mContext = getContext();
mContentResolver = mContext.getContentResolver();
}
@Override
public Cursor query(Uri uri, String[] projection, String selection,
String[] selectionArgs, String sortOrder) {
Cursor result = mContentResolver.query(ContactsContract.getContactsURI(Long.parseLong(address.get(1))), null, null, null, ContactsContract.ContactColumns.SHOW_NAME);
return result;
}
}
调用内CP从我的CP后,我recive意外的异常:
After calling inner CP from my CP I recive unexpected exception:
java.lang.UnsupportedOperationException: Only CrossProcessCursor cursors are supported across process for now
的例外是光标由CP包装并传输包裹,外CP不能再包装,所以我在这里有一个问题。当我检查类返回游标我收到CursorWrapperInner。有没有什么办法可以解开光标(从这个CWI到正规光标)在我的外部CP(但不是transfering所有数据MatrixCursor环路 - 它太费时间)。
The exception concerns the wrapping of the Cursor by the CP and transfer it wrapped, Outer CP can't wrap it again so I have a problem here. When I checked class of returned cursor I received CursorWrapperInner. Is there any way to unwrap cursor (from this CWI to regular Cursor) in my outer CP (but not by transfering all data to MatrixCursor in loop – it’s too time-consuming).
推荐答案
有没有需要解开光标。问题是,如果你的内容提供商提供结果到运行在另一个进程中的客户端,您可以从查询返回的光标()必须实现CrossProcessCursor接口。它不是在文件(AFAICS)说,但是你可以看到这个从你的日志。
There's no need to "unwrap" the cursor. The problem is, if your content provider is providing results to a client that runs in another process, the Cursor you returned from query() must implement CrossProcessCursor interface. It's not stated in document (AFAICS), but you can see this from your log.
所有你需要做的就是实现CrossProcessCursor接口,环绕你的光标。
All you need to do is implement CrossProcessCursor interface, and wrap it around your cursor.
// your query statement does not seem right..BTW
Cursor result = mContentResolver.query(...);
// now, you return a CrossProcessCursorWrapper.
return new CrossProcessCursorWrapper(result);
的CrossProcessCursor方法实现从AbstractCursor移植。一些细微的修改是由这样编译器的开心:
Implementation of CrossProcessCursor methods are ported from AbstractCursor. Some slight modifications are made so the compiler's happy:
public class CrossProcessCursorWrapper extends CursorWrapper implements
CrossProcessCursor {
public CrossProcessCursorWrapper(Cursor cursor) {
super(cursor);
}
@Override
public CursorWindow getWindow() {
return null;
}
@Override
public void fillWindow(int position, CursorWindow window) {
if (position < 0 || position > getCount()) {
return;
}
window.acquireReference();
try {
moveToPosition(position - 1);
window.clear();
window.setStartPosition(position);
int columnNum = getColumnCount();
window.setNumColumns(columnNum);
while (moveToNext() && window.allocRow()) {
for (int i = 0; i < columnNum; i++) {
String field = getString(i);
if (field != null) {
if (!window.putString(field, getPosition(), i)) {
window.freeLastRow();
break;
}
} else {
if (!window.putNull(getPosition(), i)) {
window.freeLastRow();
break;
}
}
}
}
} catch (IllegalStateException e) {
// simply ignore it
} finally {
window.releaseReference();
}
}
@Override
public boolean onMove(int oldPosition, int newPosition) {
return true;
}
}
这篇关于光标包装/通过拆中的ContentProvider的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!