所以基本上我通過本教程 http://mindtherobot.com/blog/37/android-architecture-tutorial-developing-an-app-with-a-background-service-using-ipc/Parcelable遇到IOException異常寫序列化對象
就因爲它是多了還是少了什麼我想要實現。我希望我的GPS服務在後臺運行,不斷地收集GPS座標並將其保存到數組中,這樣用戶就可以交換到另一個活動並返回到Map活動,並讓它仍然繪製最多的整個記錄路徑。
所以我實現了本教程來設置IPC,它似乎工作或多或少,但我有一個問題獲取數據回到我的活動。這裏是我的文件,首先是我的Parcelable對象,它是一個將保存GPSObject的列表。接下來是我的GPSObject,它只是一個可序列化的對象,其中包含來自Android位置對象的一些信息。
package com.android.SnowFever;
import java.util.ArrayList;
import java.util.List;
import com.android.SnowFever.*;
import com.google.android.maps.GeoPoint;
import android.os.Parcel;
import android.os.Parcelable;
public final class GPSResult implements Parcelable {
public static final Creator<GPSResult> CREATOR = new Creator<GPSResult>() {
@Override
public GPSResult[] newArray(int size) {
return new GPSResult[size];
}
@Override
public GPSResult createFromParcel(Parcel source) {
return new GPSResult(source);
}
};
private List<GPSObject> geoPoints;
public GPSResult() {
geoPoints = new ArrayList<GPSObject>();
}
@SuppressWarnings("unchecked")
private GPSResult(Parcel source) {
geoPoints = source.readArrayList(GPSService.class.getClassLoader());
}
public void addGPS(GPSObject location) {
geoPoints.add(location);
}
public List<GPSObject> getGeoPoints() {
return geoPoints;
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeList(geoPoints);
}
}
這是我GPSObject類
package com.android.SnowFever;
import java.io.Serializable;
import com.google.android.maps.GeoPoint;
import android.location.Location;
public class GPSObject implements Serializable{
/**
*
*/
private static final long serialVersionUID = 1L;
float spd, acc;
double lat, lng, alt;
GeoPoint gp;
public GPSObject(Location l) {
lat = l.getLatitude() * 1E6;
lng = l.getLongitude() * 1E6;
alt = l.getAltitude();
spd = l.getSpeed();
acc = l.getAccuracy();
gp = new GeoPoint((int)lat,(int)lng);
}
}
將文件添加到我的GPSResult名單我打電話
latestGPSResult.getGeoPoints().add(new GPSObject(location));
,我得到線程主題中包含錯誤,較大塊下面的錯誤窗口
11-01 18:49:06.818: E/JavaBinder(20754): *** Uncaught remote exception! (Exceptions are not yet supported across processes.)
11-01 18:49:06.818: E/JavaBinder(20754): java.lang.RuntimeException: Parcelable encountered IOException writing serializable object (name = com.android.SnowFever.GPSObject)
11-01 18:49:06.818: E/JavaBinder(20754): at android.os.Parcel.writeSerializable(Parcel.java:1176)
11-01 18:49:06.818: E/JavaBinder(20754): at android.os.Parcel.writeValue(Parcel.java:1130)
11-01 18:49:06.818: E/JavaBinder(20754): at android.os.Parcel.writeList(Parcel.java:519)
11-01 18:49:06.818: E/JavaBinder(20754): at com.android.SnowFever.GPSResult.writeToParcel(GPSResult.java:52)
11-01 18:49:06.818: E/JavaBinder(20754): at com.android.SnowFever.GPSApi$Stub.onTransact(GPSApi.java:52)
11-01 18:49:06.818: E/JavaBinder(20754): at android.os.Binder.execTransact(Binder.java:320)
11-01 18:49:06.818: E/JavaBinder(20754): at android.os.BinderProxy.transact(Native Method)
11-01 18:49:06.818: E/JavaBinder(20754): at com.android.SnowFever.GPSListener$Stub$Proxy.handleGPSUpdated(GPSListener.java:76)
11-01 18:49:06.818: E/JavaBinder(20754): at com.android.SnowFever.GPSService$GeoUpdateHandler.onLocationChanged(GPSService.java:91)