android - How to setExtras from Activity to Service? -
so need send btdevice defined in 1 activity service in params can it. here code of service:
public class servicio extends jobservice implements serviceconnection { private static bluetoothdevice btdevice; public temperaturafragment temperaturafragment; private metawearboard mwboard; @override public boolean onstartjob(jobparameters params) { mjobhandler.sendmessage( message.obtain( mjobhandler, 1, params ) ); params.getextras(btdevice = getparcelableextra(extra_bt_device)); getapplicationcontext().bindservice(new intent(this, btleservice.class), (serviceconnection) this, bind_auto_create); return true; } @override public boolean onstopjob(jobparameters params) { return false; } private handler mjobhandler = new handler(new handler.callback() { @override public boolean handlemessage( message msg ) { toast.maketext( getapplicationcontext(), "jobservice task running", toast.length_short ) .show(); jobfinished( (jobparameters) msg.obj, false ); return true; } } );
and here object want send defined in activity:
btdevice= getintent().getparcelableextra(extra_bt_device);
i want use builder.setextras(the object here)
, in service getextras(get object)
set extras bluetoothdevice , needs bundle. idea? help.
i put , doesn't work, because data read, not sent
intent serviceintent = new intent(servicio.class.getname()) serviceintent.putextra(1, getintent().getparcelableextra(extra_bt_device)); getapplicationcontext().startservice(serviceintent);
because setextras()
method of intent
takes input value bundle
object , bluetoothdevice
object not bundle
class object.
you can pass primitive data type , serialized ,parcelable
, arraylist
through bundle , intent .
bound services
a bound service server in client-server interface. allows components (such activities) bind service, send requests, receive responses, , perform interprocess communication (ipc). bound service typically lives while serves application component , not run in background indefinitely.
bound service example in android
and find how call service class method activity
define interface service use communicate events:
edit
you can send data in service
through intent
below code:
intent serviceintent = new intent(yourservice.class.getname()) serviceintent.putextra("userid", "123456"); context.startservice(serviceintent);
and can retrieve intent
object in callback
method of service
class called :
when service
started onstartcommand()
method called in method can retrieve value (userid)
intent object example
public int onstartcommand (intent intent, int flags, int startid) { string userid = intent.getstringextra("userid"); return start_sticky; }
edit 2
you can send bluetoothdevice
object directly intent below code because bluetoothdevice
class implements parcelable
interface.
intent serviceintent = new intent(yourservice.class.getname()) bundle bundle = new bundle(); bundle.putparcelable("btdevice",bluetoothdevice); serviceintent.putextras(bundle); context.startservice(serviceintent);
and can retrieve below in service class: in onstartcommand
method
bluetoothdevice btdevice = (bluetoothdevice ) bundle.getparcelable("btdevice");
Comments
Post a Comment