I'm trying to understand the AsyncTask class, I wrote a simple code which computes if a number is Prime in a not optimized way. I don't want to compute the prime problem in the main thread, so I implemented the following class:
private class PrimeTask extends AsyncTask<Integer, Void, String>{
public String isPrime(Integer n){
for(int i=2; i<n-1; i++){
if(n%i==0){
return "Not Prime";
}
}
return "Is Prime";
}
@Override
protected String doInBackground(Integer ... num){
return isPrime(num[0]);
}
@Override
protected void onPostExecute(String result){
output.setText(result);
}
}
I compute this task every time the user presses the button in the MainActivity.
output = findViewById(R.id.Ouput);
myButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
EditText input = findViewById(R.id.Input);
//output.setText(isPrime(Integer.parseInt(input.getText().toString())));
PrimeTask task = new PrimeTask();
task.execute(Integer.parseInt(input.getText().toString()));
}
});
but when I run the application I still get the following message!
I/Choreographer: Skipped 57 frames! The application may be doing too much work on its main thread
Why do I still have this message? Am I doing anything wrong?