android - How can I get Intent in a service? -
i have intent:
intent serviceintent = new intent(getapplicationcontext(), servicio.class); serviceintent.putextra(mainactivity.extra_bt_device, btdevice); getapplicationcontext().startservice(serviceintent);
but reason, in service code:
btdevice = (bluetoothdevice) params.getextras().get(mainactivity.extra_bt_device); // btdevice = getparcelableextra(extra_bt_device)
it says intent null, isn't null on activity.
you need bind service activity access service methods. using service object can call service method , pass value activity service. write code in activity-
servicio mservice; @override protected void onresume() { super.onresume(); intent bindintent = new intent(this, servicio.class); bindservice(bindintent, mserviceconnection, context.bind_auto_create); } @override protected void onpause() { super.onpause(); unbindservice(mserviceconnection); mservice = null; } private serviceconnection mserviceconnection = new serviceconnection() { public void onserviceconnected(componentname classname, ibinder rawbinder) { mservice = ((servicio.localbinder) rawbinder).getservice(); } public void onservicedisconnected(componentname classname) { //// mservice.disconnect(mdevice); mservice = null; } };
mservice object of service class using can pass value service.
write in service class
private final ibinder mbinder = new localbinder(); public class localbinder extends binder { public servicio getservice() { return servicio.this; } } @override public ibinder onbind(intent intent) { return mbinder; }
Comments
Post a Comment