我需要将 Cursor (SQLiteCursor) 从服务传递到 API 10 上的应用程序,并且很难找到一个像样的(和快速)解决方案。
我看过 CursorWindow 类。这是 Parcelable 但我无法在 API 10 上实例化此类以使用 SQLiteCursor.fillWindow() 因为它没有有效的构造函数。 CursorWindow(boolean) 已弃用。
即使我得到一个包含来自 SQLiteCursor 的数据的 CursorWindow 实例,我如何将这个窗口复制到一个新的 Cursor 中?我应该为此使用什么 Cursor 实现?我看不到扩展 AbstractWindowedCursor 的可用 Cursor。
感谢您的宝贵时间!
最佳答案
我实现了一个 ParcelableCursor 类,它实现了 CrossProcessCursor 和 Parcelable 接口(interface)。如果有人感兴趣,我会发布它。一些操作还不支持/实现,以及使用自定义 BijectiveMap(这很容易实现)。
/**
* Prefer ParcelableCursorForIntent instead.<br/>
* Cursor for IPC. Takes a CursorWindow as data buffer and the number of columns
* that CursorWindow has.<br/>
* <br/>
* <b>NOTE: this Cursor cannot be parceled when sending by intents due to <a
* href="http://code.google.com/p/android/issues/detail?id=4470">an Android
* bug</a>. Please use ParcelableCursorForIntent instead.</b>
*
* @author m0skit0@blablabla.eu
*
*/
public class ParcelableCursor implements Parcelable, CrossProcessCursor {
/** Cursor data window */
protected CursorWindow window = CursorHelper.getCursorWindowInstance();
/** How many columns we have */
protected int numColumns = 0;
/** Column names */
protected BijectiveMap<String, Integer> colNames = new BijectiveHashMap<String, Integer>();
/** Current row */
protected int curRow = -1;
/** Is this cursor closed? */
protected boolean closed = false;
/** CREATOR for Parcelable */
public static final Parcelable.Creator<ParcelableCursor> CREATOR = new Parcelable.Creator<ParcelableCursor>() { // NOPMD
// AM
@Override
public ParcelableCursor createFromParcel(final Parcel in) {
return new ParcelableCursor(in);
}
@Override
public ParcelableCursor[] newArray(final int size) {
return new ParcelableCursor[size];
}
};
/**
* Creates an empty ParcelableCursor. Please consider to use
* {@link #setFromCursor(AbstractWindowedCursor)} or
* {@link #setFromWindow(CursorWindow)} to initialize it.
*/
public ParcelableCursor() {
// Empty ParcelableCursor, don't forget to use #setFromCursor
}
/** Constructor for Parcelable */
public ParcelableCursor(final Parcel in) {
readFromParcel(in); // NOPMD by yasin on 12/7/12 11:55 AM - Android's
// Parceleble
}
/**
* Adds a new column at the end and assigns it this name. This will make
* this cursor to lose all its data, so you have to add all the columns
* before adding any row.
*/
private void addColumn(final String name) {
this.numColumns++;
this.curRow = -1;
this.colNames.put(name, this.numColumns - 1);
}
@Override
public void close() {
this.window.close();
this.closed = true;
}
@Override
public void copyStringToBuffer(final int columnIndex,
final CharArrayBuffer buffer) {
// TODO: what does this do?
}
@Override
public void deactivate() {
// Deprecated, does nothing
}
@Override
public int describeContents() {
// Nothing to do here
return 0;
}
@Override
public void fillWindow(final int position, final CursorWindow window) {
CursorHelper.copyCursorWindow(position, this.window, window);
}
@Override
public byte[] getBlob(final int columnIndex) {
return this.window.getBlob(this.curRow, columnIndex);
}
@Override
public int getColumnCount() {
return this.numColumns;
}
@Override
public int getColumnIndex(final String columnName) {
int ret = -1;
final Integer col = this.colNames.get(columnName);
if (col != null) {
ret = col;
}
return ret;
}
@Override
public int getColumnIndexOrThrow(final String columnName)
throws IllegalArgumentException {
final Integer col = this.colNames.get(columnName);
if (col == null) {
throw new IllegalArgumentException();
}
return col;
}
@Override
public String getColumnName(final int columnIndex) {
return this.colNames.getKey(columnIndex);
}
@Override
public String[] getColumnNames() {
if (DebugConfig.DEBUG) {
Log.d("PARCELCURSOR.getColumnNames()---", "===GETTING COLNAMES===");
}
final Set<Entry<String, Integer>> set = this.colNames.entrySet();
final String[] colArray = new String[set.size()];
for (final String colName : this.colNames.keySet()) {
if (DebugConfig.DEBUG) {
Log.d("-------------PARCELCURSOR.getColumnNames()", colName);
}
final int pos = this.colNames.get(colName);
colArray[pos] = colName;
}
return colArray;
}
@Override
public int getCount() {
return this.window.getNumRows();
}
@Override
public double getDouble(final int columnIndex) {
return this.window.getDouble(this.curRow, columnIndex);
}
@Override
public Bundle getExtras() {
// Does not support Extras
return null;
}
@Override
public float getFloat(final int columnIndex) {
return this.window.getFloat(this.curRow, columnIndex);
}
@Override
public int getInt(final int columnIndex) {
return this.window.getInt(this.curRow, columnIndex);
}
@Override
public long getLong(final int columnIndex) {
return this.window.getLong(this.curRow, columnIndex);
}
@Override
public int getPosition() {
return this.curRow;
}
@Override
public short getShort(final int columnIndex) { // NOPMD by yasin on 12/7/12
// 11:57 AM - Override
return this.window.getShort(this.curRow, columnIndex);
}
@Override
public String getString(final int columnIndex) {
return this.window.getString(this.curRow, columnIndex);
}
@SuppressLint("NewApi")
@Override
public int getType(final int columnIndex) {
final int currentapiVersion = android.os.Build.VERSION.SDK_INT;
int result = 0;
if (currentapiVersion >= android.os.Build.VERSION_CODES.HONEYCOMB) {
result = this.window.getType(this.curRow, columnIndex);
} else {
if (this.window.isNull(this.curRow, columnIndex)) {
result = 0; // FIELD_TYPE_NULL;
} else if (this.window.isFloat(this.curRow, columnIndex)) {
result = 2; // FIELD_TYPE_FLOAT;
} else if (this.window.isLong(this.curRow, columnIndex)) {
result = 1; // FIELD_TYPE_INTEGER;
} else if (this.window.isString(this.curRow, columnIndex)) {
result = 3; // FIELD_TYPE_STRING;
} else if (this.window.isBlob(this.curRow, columnIndex)) {
result = 4; // FIELD_TYPE_BLOB;
}
}
return result;
}
@Override
public boolean getWantsAllOnMoveCalls() {
return false;
}
@Override
public CursorWindow getWindow() {
final CursorWindow ret = CursorHelper.getCursorWindowInstance();
fillWindow(0, ret);
return ret;
}
@Override
public boolean isAfterLast() {
return (this.curRow >= this.window.getNumRows());
}
@Override
public boolean isBeforeFirst() {
return (this.curRow < 0);
}
@Override
public boolean isClosed() {
return this.closed;
}
@Override
public boolean isFirst() {
return (this.curRow == 0);
}
@Override
public boolean isLast() {
return (this.curRow == this.window.getNumRows() - 1);
}
@Override
public boolean isNull(final int columnIndex) {
return this.getType(columnIndex) == FIELD_TYPE_NULL;
}
@Override
public boolean move(final int offset) {
final int oldPos = this.curRow;
this.curRow += offset;
if (this.curRow < -1) {
this.curRow = -1;
return false;
} else if (this.curRow > this.window.getNumRows() - 1) {
this.curRow = this.window.getNumRows() - 1;
return false;
}
return onMove(oldPos, this.curRow);
}
@Override
public boolean moveToFirst() {
if (this.window.getNumRows() == 0) {
return false;
}
final int oldPos = this.curRow;
this.curRow = 0;
return onMove(oldPos, this.curRow);
}
@Override
public boolean moveToLast() {
if (this.window.getNumRows() == 0) {
return false;
}
final int oldPos = this.curRow;
this.curRow = this.window.getNumRows() - 1;
return onMove(oldPos, this.curRow);
}
@Override
public boolean moveToNext() {
final int oldPos = this.curRow++;
if (isAfterLast()) {
this.curRow = this.window.getNumRows();
return false;
}
return onMove(oldPos, this.curRow);
}
@Override
public boolean moveToPosition(final int position) {
if (position < -1 && position >= this.window.getNumRows()) {
return false;
}
final int oldPos = this.curRow;
this.curRow = position;
return onMove(oldPos, this.curRow);
}
@Override
public boolean moveToPrevious() {
final int oldPos = this.curRow--;
if (isBeforeFirst()) {
this.curRow = -1;
return false;
}
return onMove(oldPos, this.curRow);
}
@Override
public boolean onMove(final int oldPosition, final int newPosition) {
// Don't forget to set curRow = -1 if this method returns false
return true;
}
/** Restoring this object from a Parcel */
public void readFromParcel(final Parcel in) {
this.numColumns = in.readInt();
this.colNames = in.readParcelable(ClassLoaderHelper.getClassLoader());
this.curRow = in.readInt();
this.closed = (in.readByte() == 1);
// Closes the cursor before create a new cursor.
if (window != null) {
window.close();
}
this.window = CursorWindow.newFromParcel(in);
}
/** Not supported */
@Override
public void registerContentObserver(final ContentObserver observer) {
// Does nothing
}
/** Not supported */
@Override
public void registerDataSetObserver(final DataSetObserver observer) {
// Does nothing
}
/** Deprecated, not supported */
@Override
public boolean requery() {
return false;
}
/** Not supported */
@Override
public Bundle respond(final Bundle extras) {
// Does nothing
return null;
}
/** Sets this cursor from another windowed Cursor */
public void setFromCursor(final AbstractWindowedCursor cursor) throws CursorIndexOutOfBoundsException, IllegalStateException {
// Reset number of columns
this.numColumns = 0;
// Set column names
final String[] colNames = cursor.getColumnNames();
if (colNames != null) {
for (final String col : colNames) {
addColumn(col);
}
}
// Fill window
this.window.clear();
this.window.setNumColumns(this.numColumns);
cursor.fillWindow(0, this.window);
moveToPosition(-1);
}
/** Sets this cursor from another windowed Cursor */
public void setFromCursor(final MatrixCursor cursor) throws CursorIndexOutOfBoundsException ,IllegalStateException{
// Reset number of columns
this.numColumns = 0;
// Set column names
final String[] colNames = cursor.getColumnNames();
if (colNames != null) {
for (final String col : colNames) {
addColumn(col);
}
}
// Fill window
this.window.clear();
this.window.setNumColumns(this.numColumns);
cursor.fillWindow(0, this.window);
moveToPosition(-1);
}
/** Sets this cursor using a CursorWindow data */
public void setFromWindow(final CursorWindow window) {
CursorHelper.copyCursorWindow(0, window, this.window);
this.numColumns = CursorHelper.getCursorWindowNumCols(window);
moveToPosition(-1);
}
/** Not supported */
@Override
public void setNotificationUri(final ContentResolver cr, final Uri uri) {
// Does nothing
}
/** Not supported */
@Override
public void unregisterContentObserver(final ContentObserver observer) {
// Does nothing
}
/** Not supported */
@Override
public void unregisterDataSetObserver(final DataSetObserver observer) {
// Does nothing
}
@Override
public void writeToParcel(final Parcel out, final int flags) {
out.writeInt(this.numColumns);
out.writeParcelable((Parcelable) this.colNames, 0);
out.writeInt(this.curRow);
out.writeByte(this.closed ? (byte) 1 : 0);
this.window.writeToParcel(out, flags);
}
}
仍在寻找更标准的方法来做到这一点。任何信息将不胜感激!
编辑:这通过了很少的测试,所以在使用之前测试它。
EDIT2:事实上,它充满了错误......我会尽快更新一个错误较少的版本。
EDIT3:更新了我们使用一年以来的工作光标。
关于android - 在进程之间传递游标(Parcelable Cursor),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11790893/
我构建了两个需要相互通信和发送文件的Rails应用程序。例如,一个Rails应用程序会发送请求以查看其他应用程序数据库中的表。然后另一个应用程序将呈现该表的json并将其发回。我还希望一个应用程序将存储在其公共(public)目录中的文本文件发送到另一个应用程序的公共(public)目录。我从来没有做过这样的事情,所以我什至不知道从哪里开始。任何帮助,将不胜感激。谢谢! 最佳答案 无论Rails是什么,几乎所有Web应用程序都有您的要求,大多数现代Web应用程序都需要相互通信。但是有一个小小的理解需要你坚持下去,网站不应直接访问彼此
在MRIRuby中我可以这样做:deftransferinternal_server=self.init_serverpid=forkdointernal_server.runend#Maketheserverprocessrunindependently.Process.detach(pid)internal_client=self.init_client#Dootherstuffwithconnectingtointernal_server...internal_client.post('somedata')ensure#KillserverProcess.kill('KILL',
我正在编写一个gem,我必须在其中fork两个启动两个webrick服务器的进程。我想通过基类的类方法启动这个服务器,因为应该只有这两个服务器在运行,而不是多个。在运行时,我想调用这两个服务器上的一些方法来更改变量。我的问题是,我无法通过基类的类方法访问fork的实例变量。此外,我不能在我的基类中使用线程,因为在幕后我正在使用另一个不是线程安全的库。所以我必须将每个服务器派生到它自己的进程。我用类变量试过了,比如@@server。但是当我试图通过基类访问这个变量时,它是nil。我读到在Ruby中不可能在分支之间共享类变量,对吗?那么,还有其他解决办法吗?我考虑过使用单例,但我不确定这是
在Cooper的书BeginningRuby中,第166页有一个我无法重现的示例。classSongincludeComparableattr_accessor:lengthdef(other)@lengthother.lengthenddefinitialize(song_name,length)@song_name=song_name@length=lengthendenda=Song.new('Rockaroundtheclock',143)b=Song.new('BohemianRhapsody',544)c=Song.new('MinuteWaltz',60)a.betwee
我正在检查一个Rails项目。在ERubyHTML模板页面上,我看到了这样几行:我不明白为什么不这样写:在这种情况下,||=和ifnil?有什么区别? 最佳答案 在这种特殊情况下没有区别,但可能是出于习惯。每当我看到nil?被使用时,它几乎总是使用不当。在Ruby中,很少有东西在逻辑上是假的,只有文字false和nil是。这意味着像if(!x.nil?)这样的代码几乎总是更好地表示为if(x)除非期望x可能是文字false。我会将其切换为||=false,因为它具有相同的结果,但这在很大程度上取决于偏好。唯一的缺点是赋值会在每次运行
我没有找到太多关于如何执行此操作的信息,尽管有很多关于如何使用像这样的redirect_to将参数传递给重定向的建议:action=>'something',:controller=>'something'在我的应用程序中,我在路由文件中有以下内容match'profile'=>'User#show'我的表演Action是这样的defshow@user=User.find(params[:user])@title=@user.first_nameend重定向发生在同一个用户Controller中,就像这样defregister@title="Registration"@user=Use
我正在使用RubyonRails3.0.9,我想生成一个传递一些自定义参数的link_toURL。也就是说,有一个articles_path(www.my_web_site_name.com/articles)我想生成如下内容:link_to'Samplelinktitle',...#HereIshouldimplementthecode#=>'http://www.my_web_site_name.com/articles?param1=value1¶m2=value2&...我如何编写link_to语句“alàRubyonRailsWay”以实现该目的?如果我想通过传递一些
如何在Ruby中按名称传递函数?(我使用Ruby才几个小时,所以我还在想办法。)nums=[1,2,3,4]#Thisworks,butismoreverbosethanI'dlikenums.eachdo|i|putsiend#InJS,Icouldjustdosomethinglike:#nums.forEach(console.log)#InF#,itwouldbesomethinglike:#List.iternums(printf"%A")#InRuby,IwishIcoulddosomethinglike:nums.eachputs在Ruby中能不能做到类似的简洁?我可以只
📢博客主页:https://blog.csdn.net/weixin_43197380📢欢迎点赞👍收藏⭐留言📝如有错误敬请指正!📢本文由Loewen丶原创,首发于CSDN,转载注明出处🙉📢现在的付出,都会是一种沉淀,只为让你成为更好的人✨文章预览:一.分辨率(Resolution)1、工业相机的分辨率是如何定义的?2、工业相机的分辨率是如何选择的?二.精度(Accuracy)1、像素精度(PixelAccuracy)2、定位精度和重复定位精度(RepeatPrecision)三.公差(Tolerance)四.课后作业(Post-ClassExercises)视觉行业的初学者,甚至是做了1~2年
最近因为项目需要,需要将Android手机系统自带的某个系统软件反编译并更改里面某个资源,并重新打包,签名生成新的自定义的apk,下面我来介绍一下我的实现过程。APK修改,分为以下几步:反编译解包,修改,重打包,修改签名等步骤。安卓apk修改准备工作1.系统配置好JavaJDK环境变量2.需要root权限的手机(针对系统自带apk,其他软件免root)3.Auto-Sign签名工具4.apktool工具安卓apk修改开始反编译本文拿Android系统里面的Settings.apk做demo,具体如何将apk获取出来在此就不过多介绍了,直接进入主题:按键win+R输入cmd,打开命令窗口,并将路