Estoy tratando de usar el AsyncTask
para ejecutar un hilo en segundo plano
, que es un timer
que reinicia cada 20 segundos
, aqui esta mi codigo pero no logro hacerlo funcionar correctamente, alguien me puede decir cual es mi error?
public class MainActivity extends AppCompatActivity {
private Boolean statusKeyboard = false;
private EditText mTextoEditor1;
private Button mBotonSend;
private TextView mDumpTextView;
private MiTareaAsincrona tarea1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate( savedInstanceState );
setContentView( R.layout.activity_main );
Toolbar toolbar = (Toolbar) findViewById( R.id.toolbar );
setSupportActionBar( toolbar );
mBotonSend = findViewById(R.id.bt2_SendButton);
mDumpTextView = (TextView) findViewById(R.id.tv1_ReadValues);
mTextoEditor1 = (EditText) findViewById(R.id.et1_WriteValues);
mBotonSend.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String message = mTextoEditor1.getText().toString();
if (mTextoEditor1.getText().toString().trim().equals("")) {
Toast.makeText(MainActivity.this, "Por favar Ingresar al texto...", Toast.LENGTH_SHORT).show();
} else {
mDumpTextView.append( message + "\r\n" );
}
mTextoEditor1.setText("");
}
});
tarea1 = new MiTareaAsincrona();
tarea1.execute();
}
private class MiTareaAsincrona extends AsyncTask<Void, Integer, Boolean> {
@Override
protected Boolean doInBackground(Void... params) {
setupTimer(20);
return true;
}
@Override
protected void onPostExecute(Boolean result) {
if(result)
Toast.makeText(MainActivity.this, "Timer finalizado!", Toast.LENGTH_SHORT).show();
}
@Override
protected void onCancelled() { }
}
private void setupTimer(Integer milisegundos) {
new CountDownTimer( milisegundos, 1000 ) {
public void onTick(long millisUntilFinished) {
//mDumpTextView.setText( "seconds: " + millisUntilFinished / 1000 );
}
public void onFinish() {
mDumpTextView.setText( "done!" );
start();
}
}.start();
}
}
Aquí el error que aparece en el log:
04-26 12:17:42.316 31649-31668/com.wsalas.myicontest E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #1
Process: com.wsalas.myicontest, PID: 31649
java.lang.RuntimeException: An error occured while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:300)
at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
at java.util.concurrent.FutureTask.run(FutureTask.java:242)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at java.lang.Thread.run(Thread.java:818)
Caused by: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
at android.os.Handler.<init>(Handler.java:200)
at android.os.Handler.<init>(Handler.java:114)
at android.os.CountDownTimer$1.<init>(CountDownTimer.java:114)
at android.os.CountDownTimer.<init>(CountDownTimer.java:114)
at com.wsalas.myicontest.MainActivity$2.<init>(MainActivity.java:73)
at com.wsalas.myicontest.MainActivity.setupTimer(MainActivity.java:73)
at com.wsalas.myicontest.MainActivity.access$300(MainActivity.java:19)
at com.wsalas.myicontest.MainActivity$MiTareaAsincrona.doInBackground(MainActivity.java:58)
at com.wsalas.myicontest.MainActivity$MiTareaAsincrona.doInBackground(MainActivity.java:55)
at android.os.AsyncTask$2.call(AsyncTask.java:288)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at java.lang.Thread.run(Thread.java:818)
04-26 12:17:42.626 1441-2116/? E/ActivityManager: Invalid thumbnail dimensions: 288x288
04-26 12:17:42.906 1441-2210/? E/JavaBinder: !!! FAILED BINDER TRANSACTION !!!
04-26 12:17:42.956 31649-31649/com.wsalas.myicontest E/CliptrayUtils: hideClipTrayIfNeeded() TextView is focused!! hideClipTray()
04-26 12:17:44.036 31689-31689/? E/android_hardware_fm: FM : loading QCOMM FM-JNI
Asynctask
, en este caso necesito que eltimer corra en forma independiente en otro hilo
, solo por comodidad he colocado un timer en reemplazo de un código el cual se comporta como un timer pero su código es muy grande como para colocarlo en el foro