android - 如何将任何对象从 Activity 传递到服务?

标签 android android-intent android-activity bluetooth-lowenergy

我想将“BluetoothDevice”类的对象从 Activity 传递到服务。我正在获取传递字符串或任何原始类型但不传递对象的语法。请帮忙。提前致谢。

最佳答案

您应该创建一个包含要传递的数据的类,并从 SerializedParcelable 扩展该类,然后您可以将该类的对象作为 Extra到您的Intent并在您的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");

关于android - 如何将任何对象从 Activity 传递到服务?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27309125/

相关文章:

android - Firebase 结合了免费和付费的分析和错误报告

android - 通过 Intent 传递 ArrayList

android - 从 Textview 获取值(value)

android - 在哪里重新创建 Activity ?

android - 如何获取某一列中具有相同值的行列表?

android - Ionic6 电容器 : After adding "cordova-plugin-advanced-http", 应用程序在工作室中构建开始给出错误找不到符号 CordovaPluginPathHandler

android - 将音频文件流式传输到 soundpool load() Android

android - 使用 Intent 共享时没有图像扩展名?

java - 如何修复图像裁剪的放大尺寸?

android - BadTokenException即使我检查Activity是否为isFinishing()