Using services to run code in the background
I know this is asked in a lot of questions, and there's a lot of
information about it. However I have yet to find an example or complete
explanation, on how to just set up a service and run some code in it
whilst your Apps running.
Specifically I want to run this code:
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.Scanner;
public class SearchingFilesMain {
public static void main(String[] arg) {
String[] array = new String[1000];
int checker4 = 0;
String checker3 = "";
String checker2 = "";
String checker1 = "";
String checker = "";
int num = 0;
try {
Scanner scan = new Scanner(new BufferedReader(new FileReader(
"C:\\Users\\User\\Desktop\\asciiTracks.txt")));
while (checker != null) {
String pattern = "Array Start";
checker = scan.findWithinHorizon(pattern, 0);
if(checker.equals("Array Start")){
String pattern2 = "Array Size";
checker3 = scan.findWithinHorizon(pattern2, 300);
if(checker3 != null && checker3.equals("Array Size")){
checker4 =
Integer.parseInt(scan.findInLine("(10000|\\d{1,4})"));
System.out.println(checker4);
checker1 = scan.findWithinHorizon("DATA ASCII", 500);
if(checker1 != null && checker1.equals("DATA ASCII")){
scan.nextLine();
for(int counter = 0; counter<checker4; counter++){
array[num] = scan.nextLine();
num++;
if(num >999){
num = 0;
}else{}
}
for(int counter1 = 0 ; counter1 < 1000 ; counter1++){
if(array[counter1] != null){
System.out.println(array[counter1]);
String values = (array[counter1]).toString();
System.out.println(values);
String[] valueArray = values.split("\\s+");
//no value stored in valueArray[0] index for
some reason.
System.out.println(valueArray[1]);
System.out.println(valueArray[2]);
System.out.println(valueArray[3]);
System.out.println(valueArray[4]);
}else{
break;
}
}
}else{
System.out.println("DATA ASCII not found");
}
}else{System.out.println("no array size");
//similar code to that above must go in here at a later
point.
//added similar code so far works perfectly at searching
through file!
checker1 = scan.findWithinHorizon("DATA ASCII", 500);
if(checker1 != null && checker1.equals("DATA ASCII")){
scan.nextLine();
for(int counter = 0; counter<checker4; counter++){
array[num] = scan.nextLine();
num++;
if(num >999){
num = 0;
}else{}
}
for(int counter1 = 0 ; counter1 < 1000 ; counter1++){
if(array[counter1] != null){
System.out.println(array[counter1]);
String values = (array[counter1]).toString();
System.out.println(values);
String[] valueArray = values.split("\\s+");
//no value stored in valueArray[0] for some reason.
System.out.println(valueArray[1]);
System.out.println(valueArray[2]);
System.out.println(valueArray[3]);
System.out.println(valueArray[4]);
}else{
break;
}
}
}else{
System.out.println("DATA ASCII not found");
}
}
}else{System.out.println("no array start");}}}catch
(FileNotFoundException e) {}}}
This code searches through a file looking for keywords and numbers,
finding the data related to them and storing it in Arrays. I simply want
to run this code in a service in the background alongside my App. I want
it to send data back to my App, and update the UI (therefore i believe i
need to use a bindservice() ) and start and stop when my App starts and
stops.
I want it to be left running the entire time, as at some point it will be
reading in data from a network socket.
Any example code you think might work and a small explanation on why it
works, or an url to a very simplistic tutorial with example code would be
great. Services are completely new to me so if there's a better way of
doing what Ive described please feel free to tell me.
No comments:
Post a Comment