Send cursor to action from service

I am trying to send a database cursor from a service (IntentService) to my activity (which implements a receiver to catch any response from the service). As soon as I get the data from the request cursor = db.query(DBHelper.DB_BOOKS_TABLE, columns, where, whereArgs, groupBy, having, orderBy);, I would like to send the cursor back from the service to my caller through the receiver. but the package structure just allows me to send data (strings, ints, thinks so). I know that the package also allows you to send pickable objects and Serializable Objects. but how to convert a regular cursor to an arbitrary or serializable cursor. Thks.

0
source share
2 answers

I choose my own custom class that implements the Parcelable Interface . since for me such cursors as CrossProcessCursor are completely unknown .

Inside my own ParcelableRow, I just implement a HashMap object . In order to no longer worry about the number of rows or columns, I just map the cursor to my own ParcelableRow object. here is the mi code:

public class ParcelableRow implements Parcelable {
 private HashMap<String, String> colsMap;


 public static final Parcelable.Creator<ParcelableRow> CREATOR
 = new Parcelable.Creator<ParcelableRow>() {

    @Override
    public ParcelableRow createFromParcel(Parcel source) {
        return new ParcelableRow(source);
    }

    @Override
    public ParcelableRow[] newArray(int size) {
        return new ParcelableRow[size];
    }
};


public ParcelableRow(Parcel in) {
    colsMap = new HashMap<String, String>();
    readFromParcel(in);
}
public ParcelableRow() {
    colsMap = new HashMap<String, String>();
}
@Override
public int describeContents() {
    return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {

    for(String key: colsMap.keySet()){

        dest.writeString(key);
        dest.writeString(colsMap.get(key));

    }

}
public void readFromParcel(Parcel parcel) {

    int limit = parcel.dataSize();
    parcel.setDataPosition(0);
    for(int i = 0; i < limit; i++){
        colsMap.put(parcel.readString(), parcel.readString());
    }

}


public void addNewCol(String colName, String colValue){
    colsMap.put(colName, colValue);
}
public String getColumnValue(String colName){
    return colsMap.get(colName);
}

} Thanks @CommonsWare for giving me the key. thumbs up!.

I hope this can be useful to someone, I just spend a few days trying to find something that can satisfy my needs. here are some code examples that I used. welcome to welcome.

, .

!

0

Bundle - Service Activity. Service. Service , IntentService, . Service , unbindService() , :

private Cursor cursor;
public void initializeCursor() {
    cursor = Cursor();
    //Now the cursor is created. Send a broadcast to the activity that it done.
    broadcastManager.sendBroadcast("your.package.CURSOR_FINISHED");
}

getCursor() < <22 > Activity:

public Cursor getCursor() {
    return this.cursor;
}

BroadcastReceiver Activity, :

@Override
public void onReceive(Context context, Intent intent) {
    if(intent.getAction().equals("your.package.CURSOR_FINISHED") {
        Cursor cursor = myService.getCursor();
        unbindService(myServiceConnection);
    }
}

, , . , , .

+1

All Articles