android - Send seekbar's values instead of text string -
i'm developing app based on bluetoothchat example have main activity 2 buttons:
1. find devices 2. configuration
with first button search devices same code in mentioned example.
with second button, enter activity have 3 seekbars textviews under each 1 showing current seekbar's value.
what need modify bluetoothchat example's code send array 3 values instead of text.
this code of activity i'm trying develop this.
how can this?
/********************* * * oncreate * ********************/ @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.configuration); bar1 = (seekbar)findviewbyid(r.id.fastbar); bar1.setonseekbarchangelistener(this); progress1 = (textview)findviewbyid(r.id.out1); bar2 = (seekbar)findviewbyid(r.id.climbbar); bar2.setonseekbarchangelistener(this); progress2 = (textview)findviewbyid(r.id.out2); bar3 = (seekbar)findviewbyid(r.id.platformbar); bar3.setonseekbarchangelistener(this); progress3 = (textview)findviewbyid(r.id.out3); mpreferences = preferencemanager.getdefaultsharedpreferences(this); /**initialize buffer outgoing messages*/ globalvar.moutstringbuffer = new stringbuffer(""); } /********************* * * seekbar * ********************/ /**method each seekbar's value , save textview*/ @override public void onprogresschanged(seekbar bar, int progress, boolean fromuser) { switch (bar.getid()) { case r.id.fastbar: progress1.settext(progress + "%"); break; case r.id.climbbar: progress2.settext(progress + "%"); break; case r.id.platformbar: progress3.settext(progress + "%"); break; } } @override public void onstarttrackingtouch(seekbar seekbar) { } @override public void onstoptrackingtouch(seekbar seekbar) { seekbar.setsecondaryprogress(seekbar.getprogress()); } /********************* * * onresume * ********************/ /**load seekbar's values sharedpreferences*/ @override protected void onresume() { super.onresume(); loadedprogress1 = mpreferences.getint(key_progress_1, 50); bar1.setprogress(loadedprogress1); loadedprogress2 = mpreferences.getint(key_progress_2, 90); bar2.setprogress(loadedprogress2); loadedprogress3 = mpreferences.getint(key_progress_3, 70); bar3.setprogress(loadedprogress3); } /********************* * * onpause * ********************/ /**save seekbar's values on sharedfreferences*/ @override protected void onpause() { super.onpause(); sharedpreferences.editor editor = mpreferences.edit(); savedprogress1 = bar1.getprogress(); editor.putint(key_progress_1, savedprogress1); savedprogress2 = bar2.getprogress(); editor.putint(key_progress_2, savedprogress2); savedprogress3 = bar3.getprogress(); editor.putint(key_progress_3, savedprogress3); editor.commit(); } /********************* * * options * ********************/ @override public boolean oncreateoptionsmenu(menu menu) { /**inflate menu; adds items action bar if present.*/ getmenuinflater().inflate(r.menu.menu_conf, menu); return true; } /**options menu call send function*/ @override public boolean onoptionsitemselected(menuitem item) { switch (item.getitemid()) { case r.id.send: sendvalues(); break; case r.id.reset: resetvalues(null); break; } return true; } /********************* * * send & reset * ********************/ public void sendvalues() { /**seekbars values*/ string message1 = progress1.gettext().tostring(); string message2 = progress2.gettext().tostring(); string message3 = progress3.gettext().tostring(); /**check we're connected before trying anything*/ if (globalvar.mtransmission.getstate() != globalvar.state_connected) { toast.maketext(this, r.string.not_connected, toast.length_short).show(); return; } /** * *here i'm trying create function send 3 values in string type message *this code setupchat() function, wrong in case */ /**get message bytes , tell write*/ byte[] send = message.getbytes(); globalvar.mtransmission.write(send); /**reset out string buffer zero*/ globalvar.moutstringbuffer.setlength(0); } public void resetvalues() { defaultvalue1 = 50; bar1.setprogress(defaultvalue1); defaultvalue2 = 90; bar2.setprogress(defaultvalue1); defaultvalue3 = 70; bar3.setprogress(defaultvalue1); }
this new function created:
public void sendvalues() { /**send seekbars values*/ string message = progress1.gettext().tostring()+":"+progress2.gettext().tostring()+":"+progress3.gettext().tostring(); string[] values = message.split(":"); (string value : values) { int number = integer.valueof(value); } /**check we're connected before trying anything*/ if (globalvar.mtransmission.getstate() != globalvar.state_connected) { toast.maketext(this, r.string.not_connected, toast.length_short).show(); return; } /**get message bytes , tell transmission write*/ byte[] send = message.getbytes(); globalvar.mtransmission.write(send); /**reset out string buffer zero*/ globalvar.moutstringbuffer.setlength(0); }
you can send values string regularexpression ex. "5:15:33"
, split when receive string on ":"
this.
string str = bar1.gettext().tostring()+":"+bar2.gettext().tostring()+":"+bar3.gettext().tostring();
send str string reciver and, can int's out of string array
string[] values = str.split(":"); (string value : values) { int number = integer.valueof(value); }
Comments
Post a Comment