java - Service losing bind (mBoundService = null) -
i've got working version fragment pasing json string main activity has tcp socket service bound it:
main_activity:
... private serviceconnection mconnection = new serviceconnection() { @override public void onserviceconnected(componentname name, ibinder service) { mboundservice = ((socket_service.localbinder) service).getservice(); } @override public void onservicedisconnected(componentname name) { mboundservice = null; log.d("tcp client: main_activity", "onservicedisconnected called!"); } }; ... public void writebyte(string jsonobject) { if (mboundservice != null) { mboundservice.sendmessage(jsonobject); } } ...
fragment:
... main_activity activity = (main_activity) getactivity(); activity.writebyte(jsonobject.tostring()); ...
---------------------------------------------------------------------------------------------
i'd create separate class (no fragment) create json string, pass main_activity passes service. did that, it's not working, because mboundservice gets value "null" , cant pass service anymore.
fragment:
json pwmjason = new json(); pwmjason.cratepwmjson(pwm1,pwm2,pwm3,pwm4,pwm5,pwm6,pwm7);
json.class:
package de.l.led.control.background; import android.app.fragment; import android.util.log; import org.json.jsonarray; import org.json.jsonexception; import org.json.jsonobject; import de.l.led.control.main_activity; public class json extends main_activity{ public void cratepwmjson(int pwm1, int pwm2, int pwm3, int pwm4, int pwm5, int pwm6, int pwm7){ jsonobject jsonobject = new jsonobject(); jsonarray jsonarray = new jsonarray(); try { //jsonarray.put(""); jsonarray.put(pwm1 * 64); jsonarray.put(pwm2 * 64); jsonarray.put(pwm3 * 64); jsonarray.put(pwm4 * 64); jsonarray.put(pwm5 * 64); jsonarray.put(pwm6 * 64); jsonarray.put(pwm7 * 64); jsonobject.put("pwms", jsonarray); } catch (jsonexception e) { e.printstacktrace(); } log.d("jso", "received json string: " + jsonobject.tostring()); main_activity activity = new main_activity(); activity.writebyte(jsonobject.tostring()); } }
any ideas?
thanks
i propose use this:
public class jsonhelper{ public static string cratepwmjson(int pwm1, int pwm2, int pwm3, int pwm4, int pwm5, int pwm6, int pwm7){ jsonobject jsonobject = new jsonobject(); jsonarray jsonarray = new jsonarray(); try { //jsonarray.put(""); jsonarray.put(pwm1 * 64); jsonarray.put(pwm2 * 64); jsonarray.put(pwm3 * 64); jsonarray.put(pwm4 * 64); jsonarray.put(pwm5 * 64); jsonarray.put(pwm6 * 64); jsonarray.put(pwm7 * 64); jsonobject.put("pwms", jsonarray); } catch (jsonexception e) { e.printstacktrace(); } log.d("jso", "received json string: " + jsonobject.tostring()); return jsonobject.tostring(); } }
then in fragment can call like:
string pwmjsonasstring = jsonhelper().cratepwmjson(pwm1,pwm2,pwm3,pwm4,pwm5,pwm6,pwm7); main_activity activity = (main_activity) getactivity(); activity.writebyte(pwmjsonasstring);
Comments
Post a Comment