1

我想将“BluetoothDevice”类的对象从活动传递到服务。我正在获取传递字符串或任何基本类型但不传递对象的语法。请帮忙。提前Thanx。如何将活动中的任何对象传递给服务?

+0

http://hmkcode.com/android-passing-java-object-another-activity/ – 2014-12-05 05:06:19

+0

你可以使用https://code.google.com/p/google-gson/ @praveen gupta – TheFlash 2014-12-05 05:06:19

回答

1

你应该创建一个类包含数据的传递和扩展,从SerializableParcelable,然后你可以把这个类的一个对象作为ExtraIntent并在Service

编辑使用它: 为rom4ek提到,BluetoothDevice是已经实现了Parcelable,所有你需要做的是:

Bundle b = new Bundle(); 
b.putParcelable("data", yourBluetoothDeviceObject); 
yourIntent.putExtra(b); 

和检索数据:

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

注意,'BluetoothDevice'已经实现了'Parcelable',所以你所需要的只是putExtra。 http://developer.android.com/reference/android/bluetooth/BluetoothDevice.html – rom4ek 2014-12-05 05:08:51

+0

答案更新。感谢提及。 ;) – 2014-12-05 05:15:53

+1

Thanx既是你,yeah @ rom4ek我们甚至不需要创建bundle的实例。我喜欢这样,工作正常。 发件人活动 - startConnectionService.putExtra(“MyClass”,deviceToConnect); 接收器服务 - BluetoothDevice deviceToConnect =(BluetoothDevice)intent.getParcelableExtra(“MyClass”); – doga 2014-12-05 06:10:33

0

你可以让你的复杂对象实施SerializableParcelable,然后你可以通过它使用putExtra(String name, Serializable value)putExtra(String name, Parcelable value)到服务

相关问题