I'm trying to send database cursor from a service(An IntentService) to my Activity(Which implements a Receiver to catch any response from the service). Once i get the data from query cursor = db.query(DBHelper.DB_BOOKS_TABLE, columns, where, whereArgs, groupBy, having, orderBy);
i would like to send back the cursor from the service to his caller thru the receiver. but the bundle structure just let me send tical data(Strings, ints, thinks like that). i know the bundle also let send parcelable objects and Serializable Objects. but how do i convert a typical cursor to a parcelable or serializable cursor. thks.
问题:
回答1:
You don't actually need to use a Bundle
to send anything from the Service
to the Activity
. You can do it like this in the Service
. I am talking strictly about a Service
here and not an IntentService
, as I've not used them before. Using a bound Service
is the same, but just call unbindService()
in your Activity when you're done with it:
private Cursor cursor;
public void initializeCursor() {
cursor = Cursor();
//Now the cursor is created. Send a broadcast to the activity that it's done.
broadcastManager.sendBroadcast("your.package.CURSOR_FINISHED");
}
and have a getCursor()
method for the Activity
:
public Cursor getCursor() {
return this.cursor;
}
And in the BroadcastReceiver
of your Activity
, catch this event:
@Override
public void onReceive(Context context, Intent intent) {
if(intent.getAction().equals("your.package.CURSOR_FINISHED") {
Cursor cursor = myService.getCursor();
unbindService(myServiceConnection);
}
}
As your activity is bound to your service, you will have a service object avaiable in your activity. Once you get the broadcast that the cursor is done, just kill the service.
回答2:
I opt for made my own custom class which implements a Parcelable Interface. since the parcelable cursors like CrossProcessCursor is quite unknown for me.
Inside of my own ParcelableRow i just implement HashMap object. To don't worry anymore about amount of rows or columns, I just map the cursor to my own ParcelableRow object. here is 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 to @CommonsWare to gave me a clue. thumbs up!.
I hope this can be useful to someone, i've just spend some days trying to find something that could satisfy my needs. here are some code examples which i've used. advises are welcome.
Warning It only satisfy me because i'm against the time.
keep coding!