How to pass any object from activity to service?

情到浓时终转凉″ 提交于 2019-12-06 11:50:16

问题


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.


回答1:


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

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);

and for retrieving data :

Bundle b = yourIntent.getExtra();
BluetoothDevice device = (BluetoothDevice) b.getParcelable("data");



回答2:


You can make your complex object implement Serializable or Parcelable and then you can pass it to Service using putExtra(String name, Serializable value) or putExtra(String name, Parcelable value)



来源:https://stackoverflow.com/questions/27309125/how-to-pass-any-object-from-activity-to-service

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!