我试图将对象传递给Broadcastreceiver
。
该机制在不传递任何数据的情况下运行良好,但是当我尝试传递一个对象时,程序崩溃了。
这是代码:
public void SetFutureStartTime(int startHour, int startMin)
{
Context context = this;
m_alarmMgr = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(context, AlarmReceiver_BoilerOn.class);
Bundle args = new Bundle();
args.putSerializable("tcpClientObj",(Serializable)m_TcpClient);
intent.putExtra("DATA",args);
m_alarmIntent = PendingIntent.getBroadcast(context, 0, intent, 0); // --------------> The code crushes here
// Set the alarm to start at 21:32 PM
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, startHour);
calendar.set(Calendar.MINUTE, startMin);
m_alarmMgr.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), m_alarmIntent);
}
这是Broadcastreceiver
实施:
public class AlarmReceiver_BoilerOn extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
Bundle args = intent.getBundleExtra("DATA");
final TcpClient tcpClient = (TcpClient) args.getSerializable("tcpClientObj");
}
}
这是错误日志:
11-19 19:35:59.553: D/AndroidRuntime(2389): Shutting down VM 11-19 19:35:59.553: D/AndroidRuntime(2389): --------- beginning of crash 11-19 19:35:59.571: E/AndroidRuntime(2389): FATAL EXCEPTION: main 11-19 19:35:59.571: E/AndroidRuntime(2389): Process: boiler.boiler, PID: 2389 11-19 19:35:59.571: E/AndroidRuntime(2389): java.lang.RuntimeException: Parcelable encountered IOException writing
可序列化对象(name = boiler.boiler.TcpClient) 11-19 19:35:59.571:E / AndroidRuntime(2389):在android.os.Parcel.writeSerializable(Parcel.java:1526) 11-19 19:35:59.571:E / AndroidRuntime(2389):在android.os.Parcel.writeValue(Parcel.java:1474) 11-19 19:35:59.571:E / AndroidRuntime(2389):在android.os.Parcel.writeArrayMapInternal(Parcel.java:723) 11-19 19:35:59.571:E / AndroidRuntime(2389):在android.os.BaseBundle.writeToParcelInner(BaseBundle.java:1408) 11-19 19:35:59.571:E / AndroidRuntime(2389):在android.os.Bundle.writeToParcel(Bundle.java:1133) 11-19 19:35:59.571:E / AndroidRuntime(2389):在android.os.Parcel.writeBundle(Parcel.java:763) 11-19 19:35:59.571:E / AndroidRuntime(2389):在android.os.Parcel.writeValue(Parcel.java:1392) 11-19 19:35:59.571:E / AndroidRuntime(2389):在android.os.Parcel.writeArrayMapInternal(Parcel.java:723) 11-19 19:35:59.571:E / AndroidRuntime(2389):在android.os.BaseBundle.writeToParcelInner(BaseBundle.java:1408) 11-19 19:35:59.571:E / AndroidRuntime(2389):在android.os.Bundle.writeToParcel(Bundle.java:1133) 11-19 19:35:59.571:E / AndroidRuntime(2389):在android.os.Parcel.writeBundle(Parcel.java:763) 11-19 19:35:59.571:E / AndroidRuntime(2389):在android.content.Intent.writeToParcel(Intent.java:8655) 11-19 19:35:59.571:E / AndroidRuntime(2389):在android.app.ActivityManagerProxy.startActivity(ActivityManagerNative.java:3052) 11-19 19:35:59.571:E / AndroidRuntime(2389):在android.app.Instrumentation.execStartActivity(Instrumentation.java:1518) 11-19 19:35:59.571:E / AndroidRuntime(2389):在android.app.Activity.startActivityForResult(Activity.java:4224) 11-19 19:35:59.571:E / AndroidRuntime(2389):在android.support.v4.app.BaseFragmentActivityJB.startActivityForResult(BaseFragmentActivityJB.java:50) 11-19 19:35:59.571:E / AndroidRuntime(2389):在android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:79) 11-19 19:35:59.571:E / AndroidRuntime(2389):在android.app.Activity.startActivityForResult(Activity.java:4183) 11-19 19:35:59.571:E / AndroidRuntime(2389):在android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:859) 11-19 19:35:59.571:E / AndroidRuntime(2389):在android.app.Activity.startActivity(Activity.java:4507) 11-19 19:35:59.571:E / AndroidRuntime(2389):在android.app.Activity.startActivity(Activity.java:4475) 11-19 19:35:59.571:E / AndroidRuntime(2389):在boiler.boiler.MainActivity.SetFutureStartTime(MainActivity.java:414) 11-19 19:35:59.571:E / AndroidRuntime(2389):在boiler.boiler.MainActivity $ 2 $ 3.onClick(MainActivity.java:185) 11-19 19:35:59.571:E / AndroidRuntime(2389):在android.view.View.performClick(View.java:5610) 11-19 19:35:59.571:E / AndroidRuntime(2389):在android.view.View $ PerformClick.run(View.java:22265) 11-19 19:35:59.571:E / AndroidRuntime(2389):在android.os.Handler.handleCallback(Handler.java:751) 11-19 19:35:59.571:E / AndroidRuntime(2389):在android.os.Handler.dispatchMessage(Handler.java:95) 11-19 19:35:59.571:E / AndroidRuntime(2389):在android.os.Looper.loop(Looper.java:154) 11-19 19:35:59.571:E / AndroidRuntime(2389):在android.app.ActivityThread.main(ActivityThread.java:6077) 11-19 19:35:59.571:E / AndroidRuntime(2389):at java.lang.reflect.Method.invoke(Native Method) 11-19 19:35:59.571:E / AndroidRuntime(2389):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:866) 11-19 19:35:59.571:E / AndroidRuntime(2389):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756) 11-19 19:35:59.571:E / AndroidRuntime(2389):引起:java.io.NotSerializableException: boiler.boiler.MainActivity $ ConnectTask $ 1 11-19 19:35:59.571:E / AndroidRuntime(2389):at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1224) 11-19 19:35:59.571:E / AndroidRuntime(2389):at java.io.ObjectOutputStream.defaultWriteFields(ObjectOutputStream.java:1584) 11-19 19:35:59.571:E / AndroidRuntime(2389):at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1549) 11-19 19:35:59.571:E / AndroidRuntime(2389):at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1472) 11-19 19:35:59.571:E / AndroidRuntime(2389):at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1218) 11-19 19:35:59.571:E / AndroidRuntime(2389):at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:346) 11-19 19:35:59.571:E / AndroidRuntime(2389):在android.os.Parcel.writeSerializable(Parcel.java:1521) 11-19 19:35:59.571:E / AndroidRuntime(2389):......还有31个 11-19 19:35:59.590:W / ActivityManager(1590):强制完成活动boiler.boiler / .MainActivity
我错过了什么?
感谢。
答案 0 :(得分:0)
如果m_TcpClient实现了Parcelable接口,则最好的选择是通过Intent Extras将其实例作为Parcelable传递。您不必将其强制转换为Serializable或Parcelable。 但是,请记住,AlarmManager存在一个众所周知的错误。它丢失了不可分割的附加内容,因此您必须先将数据编组,而不是直接传递它。以下是一些基本方法:
fun Parcelable.marshall() : ByteArray {
val parcel = Parcel.obtain()
writeToParcel(parcel, 0)
val bytes = parcel.marshall()
parcel.recycle()
return bytes
}
fun <T> ByteArray.unmarshall(creator: Parcelable.Creator<T>): T {
val parcel = unmarshall()
val result = creator.createFromParcel(parcel)
parcel.recycle()
return result
}
private fun ByteArray.unmarshall(): Parcel {
val parcel = Parcel.obtain()
parcel.unmarshall(this, 0, size)
parcel.setDataPosition(0)
return parcel
}
使用此方法,您可以将ByteArray用作额外的意图
intent.putExtra(Receiver.EXTRA, m_TcpClient.marshall())
,然后像下面这样在BroadcastReceiver中获取它:
intent.getByteArrayExtra(EXTRA)?.run {
val m_TcpClient = unmarshall(TcpClient.CREATOR)
}