如何将任何对象从活动传递到服务? [英] How to pass any object from activity to service?
问题描述
我想将"BluetoothDevice"类的对象从活动传递到服务.我正在获取用于传递字符串或任何原始类型但不传递对象的语法.请帮忙.提前感谢.
I want to pass object of "BluetoothDevice" class from an activity to service. I am getting syntaxes for passing Strings or any primitive types but not passing objects. Please Help. Thanx in advance.
推荐答案
您应该创建一个包含要传递的数据的类,并从Serializable
或Parcelable
扩展该数据,然后可以将该类的对象放入Extra
到Intent
并在Service
you should create a class containing your data to be passed and extend that from Serializable
or Parcelable
and then you can put an object of that class as an Extra
to your Intent
and use it in your Service
正如rom4ek所提到的,BluetoothDevice
已经实现了Parcelable
,您要做的就是:
EDIT :
as rom4ek mentioned, BluetoothDevice
is already implements Parcelable
and all you have to do is :
Bundle b = new Bundle();
b.putParcelable("data", yourBluetoothDeviceObject);
yourIntent.putExtra(b);
以及用于检索数据:
Bundle b = yourIntent.getExtra();
BluetoothDevice device = (BluetoothDevice) b.getParcelable("data");
这篇关于如何将任何对象从活动传递到服务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!