如何将任何对象从活动传递到服务? [英] How to pass any object from activity to service?

查看:66
本文介绍了如何将任何对象从活动传递到服务?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将"BluetoothDevice"类的对象从活动传递到服务.我正在获取用于传递字符串或任何原始类型但不传递对象的语法.请帮忙.提前感谢.

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.

推荐答案

您应该创建一个包含要传递的数据的类,并从SerializableParcelable扩展该数据,然后可以将该类的对象放入ExtraIntent并在Service

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

正如rom4ek所提到的,BluetoothDevice已经实现了Parcelable,您要做的就是:

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

以及用于检索数据:

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

这篇关于如何将任何对象从活动传递到服务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆