我正在尝试使用Intent Service来解析mysql中的数据并将其置于主要活动中。
MyTestService.java
public class MyTestService extends IntentService {
public MyTestService() {
super("MyTestService");
// TODO Auto-generated constructor stub
}
@Override
protected void onHandleIntent(Intent intent) {
Parameter result = downloadData();
Log.d("msg", result.toString());
Intent in = new Intent();
in.setAction(Receiver.ACTION_RESP);
in.addCategory(Intent.CATEGORY_DEFAULT);
in.putExtra("123", (Parcelable) result);
sendBroadcast(in);
}
private Parameter downloadData() {
Parameter parameter = new Parameter();
String data = (new LandSlideHttpClient()).getDeviceData();
try {
parameter = JSONLandslideParser.getParameter(data);
} catch (Exception e) {
}
return parameter;
}
}
MainActivity.java
public class MainActivity extends ActionBarActivity {
private Receiver receiver;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
IntentFilter filter = new IntentFilter(Receiver.ACTION_RESP);
filter.addCategory(Intent.CATEGORY_DEFAULT);
receiver = new Receiver();
registerReceiver(receiver, filter);
Intent intent = new Intent(this, MyTestService.class);
startService(intent);
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
unregisterReceiver(receiver);
}
public class Receiver extends BroadcastReceiver {
public static final String ACTION_RESP = "abcdef";
@Override
public void onReceive(Context context, Intent intent) {
Parameter result = (Parameter) intent.getParcelableExtra("123");
TextView textView = (TextView) findViewById(R.id.textview);
textView.setText(result.id.getID());
}
}
}
当我运行我的代码时,Logcat是
04-22 04:14:32.512: E/AndroidRuntime(4472): java.lang.RuntimeException: Error receiving broadcast Intent { act=abcdef cat=[android.intent.category.DEFAULT] flg=0x10 (has extras) } in com.example.testservice.MainActivity$Receiver@416fd1c0
>04-22 04:14:32.512: E/AndroidRuntime(4472): at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:773
04-22 04:14:32.512: E/AndroidRuntime(4472): at android.os.Handler.handleCallback(Handler.java:730)
04-22 04:14:32.512: E/AndroidRuntime(4472): at android.os.Handler.dispatchMessage(Handler.java:92)
04-22 04:14:32.512: E/AndroidRuntime(4472): at android.os.Looper.loop(Looper.java:137)
04-22 04:14:32.512: E/AndroidRuntime(4472): at android.app.ActivityThread.main(ActivityThread.java:5103)
04-22 04:14:32.512: E/AndroidRuntime(4472): at java.lang.reflect.Method.invokeNative(Native Method)
04-22 04:14:32.512: E/AndroidRuntime(4472): at java.lang.reflect.Method.invoke(Method.java:525)
04-22 04:14:32.512: E/AndroidRuntime(4472): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
04-22 04:14:32.512: E/AndroidRuntime(4472): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
04-22 04:14:32.512: E/AndroidRuntime(4472): at dalvik.system.NativeStart.main(Native Method)
04-22 04:14:32.512: E/AndroidRuntime(4472): Caused by: android.os.BadParcelableException: Parcelable protocol requires a Parcelable.Creator object called CREATOR on class com.example.testservice.Parameter
04-22 04:14:32.512: E/AndroidRuntime(4472): at android.os.Parcel.readParcelableCreator(Parcel.java:2114)
04-22 04:14:32.512: E/AndroidRuntime(4472): at android.os.Parcel.readParcelable(Parcel.java:2055)
04-22 04:14:32.512: E/AndroidRuntime(4472): at android.os.Parcel.readValue(Parcel.java:1971)
04-22 04:14:32.512: E/AndroidRuntime(4472): at android.os.Parcel.readMapInternal(Parcel.java:2255)
04-22 04:14:32.512: E/AndroidRuntime(4472): at android.os.Bundle.unparcel(Bundle.java:223)
04-22 04:14:32.512: E/AndroidRuntime(4472): at android.os.Bundle.getParcelable(Bundle.java:1170)
04-22 04:14:32.512: E/AndroidRuntime(4472): at android.content.Intent.getParcelableExtra(Intent.java:4501)
04-22 04:14:32.512: E/AndroidRuntime(4472): at com.example.testservice.MainActivity$Receiver.onReceive(MainActivity.java:59)
04-22 04:14:32.512: E/AndroidRuntime(4472): at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:763)
04-22 04:14:32.512: E/AndroidRuntime(4472): ... 9 more
Parameter.java
public class Parameter implements Parcelable{
public class currentData {
private float temp;
private float acc;
private float moisture;
private float battery;
private String date;
private String time;
public float getTemp() {
return temp;
}
public void setTemp(float temp) {
this.temp = temp;
}
public float getAcc() {
return acc;
}
public void setAcc(float acc) {
this.acc = acc;
}
public float getMoisture() {
return moisture;
}
public void setMoisture(float moisture) {
this.moisture = moisture;
}
public float getBattery() {
return battery;
}
public void setBattery(float battery) {
this.battery = battery;
}
public String getTime() {
return time;
}
public void setTime(String time) {
this.time = time;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
}
public class ID {
private String ID;
public String getID() {
return ID;
}
public void setID(String iD) {
ID = iD;
}
}
public ID id = new ID();
public currentData currentData = new currentData();
@Override
public int describeContents() {
// TODO Auto-generated method stub
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
// TODO Auto-generated method stub
}
}