I am developing an application in which i have 12 digit byte array to read the state of buttons on simulator. I am writing these byte array on socket to get response. i want to monitor this result continuously and want to get notified when value is changed. currently i am using Timer to do the same which runs after every 10 second,now what i want to do is to run same methods to monitor the response but using Android services.
try {
byte[] data1 = new byte[1024], packet1 =
{
(byte) 0x00,(byte) 0x00,(byte) 0x00,
(byte) 0x00,(byte) 0x00,(byte) 0x06,
(byte) 0x01,(byte) 0x01,(byte) 0x00,
(byte) 0x00,(byte) 0x00,(byte) 0x19
};
o.write(packet1);
i.read(data1, 0, 1024);
byte_to_hex = ConversionMethods.bytesToHex(data1).substring(18, 26);
char[] arr = byte_to_hex.toCharArray();
for (int i = 0; i < arr.length - 1; i += 2)
{
char temp = arr[i];
arr[i] = arr[i + 1];
arr[i + 1] = temp;
}
swapped_result=new String(arr);
result = ConversionMethods.hexStringToNBitBinary(swapped_result, 32);
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
this.runOnUiThread(Timer_Tick);
in Timer_Tick i am setting ToggleButton on|off based on reslut like this
char[] c=result.toCharArray();
int count=0;
for (int i=0;i<result.length();i++)
{
count++;
char j=c[i];
if(count==1)
toggleButton=dimmerLight1;
else if(count==2)
toggleButton=fanDimmer2;
else if(count==3)
toggleButton=fanDimmer1;
if(j=='1')
toggleButton.setChecked(true);
else
toggleButton.setChecked(false);
}
how do i implement this using service ? any idea and advice will be appreciated
Thanks
Here is good example of writing service which functionality is very similar to your task (“The service will be started at boot and periodically fetch data. The service will used by an activity which bind itself to the service. The activity will allow to request the latest data from the service.”) http://www.vogella.com/articles/AndroidServices/article.html#tutorial_ownservice