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)到服務

相關問題