I would like to implement an auto-negotiation feature similar to Google. What is the best way to do this and are there any components that have already been implemented that can help?
I'm really worried about how often you need to send requests. I assume that the request should be sent after a while. So, for example, when the user is struggling with a new character, some kind of timer should start, which, by the end, starts the function, comparing the inputs from the beginning of the timer and ending. Judging by comparison, it was decided whether to send or not to send. Should I use threads for this. Will it be resource-intensive execution?
Another option: to make (true) Thread for some time, which receives the text, sends a request and updates the presentation of the results of the main threads. Although, I'm not sure how this is done from the point of view of Android.
Thanks for any suggestions.
UPD: I came up with a simple solution to use AsyncTasks. Each time a new SearchInBackgroundTask is launched, it sets a flag that prohibits the launch of any other SearchInBackgroundTask instance, unless the original completes its work. After the flag is set so that it allows another SearchInBackgroundTask execution, the last input line is processed under conditions if it is not empty and does not equal the previously processed one.
So, suggest any thoughts on this - is this a great solution?
public class AsyncTaskForSearchActivity extends Activity {
protected boolean _threadIsWorking = false;
protected String _searchResult;
protected String _searchString;
protected String _processedSearchString;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TextView searchbox = (TextView) findViewById(R.id.searchbox);
searchbox.addTextChangedListener(_searchboxWatcher);
}
protected TextWatcher _searchboxWatcher = new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
@Override
public void afterTextChanged(Editable s) {
_searchString = s.toString();
if(_threadIsWorking == false){
_performSearchTask();
}
}
};
protected Handler _onRetrieveResult = new Handler(){
public void dispatchMessage(android.os.Message msg) {
if(_threadIsWorking == false && _searchString.length() > 0 && _processedSearchString != _searchString){
_performSearchTask();
}
};
};
protected class SearchInBackgroundTask extends AsyncTask<String, Void, Void>{
protected String _s;
@Override
protected Void doInBackground(String... params) {
_s = params[0];
try {
Thread.sleep(6000);
} catch (InterruptedException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
_searchResult = "xxx";
_threadIsWorking = false;
_onRetrieveResult.sendEmptyMessage(0);
}
}
protected void _performSearchTask(){
_threadIsWorking = true;
_processedSearchString = _searchString;
Toast.makeText(AsyncTaskForSearchActivity.this, _processedSearchString, 1).show();
(new SearchInBackgroundTask()).execute(_processedSearchString);
}
}