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)