If you don't want, or can't, use AsyncTask, you can also create threads yourself:
// Declare some specifications
private class TaskSpec {
TaskSpec() {}
public String myString;
}
TaskSpec spec = new TaskSpec();
spec.myString = "something";
// Starting a thread
MyTask task = new MyTask(spec);
Thread myThread = new Thread(task);
myThread.start();
// This is the code that will run in the thread
private class myTask implements Runnable {
mySpec spec;
boolean stopped;
myTask(mySpec _spec) {
spec = _spec;
stopped = false;
}
public void stopTask() {
stopped = true;
}
@Override
public void run() {
String myString = spec.myString;
// ...
int numTries = 0;
while (numTries < MainActivity.MAX_TRIES && !stopped) {
numTries++;
// ...
}
}
}
You can synchronize using:
// Declare this in the activity
private Object mLock;
Then:
synchronized(mLock) {
// Code that will be syncronized
}