
Ayuda!! Tarea en segundo Plano(Thread,Handler,Runnable)
Publicado por Wagner (2 intervenciones) el 15/03/2015 02:01:37
Hola soy nuevo en programacion android y las tareas en segundo plano me estan volviendo loco y agradeceria su ayuda
Este programa lo que hace o inteto que haga es que al presionar el boton espere 4 segundos y luego me cambie el texto del mismo boton por uno nuevo pero cuando lo ejecuto el programa espera los 4 segundos y despues se cae la aplicacion y no se porque e intentado de Todo desde utilizar Timer y TimerTask mover el Runnable de lugar etc etc y no funciona agradeceria su ayuda.
he aqui el codigo
package com.gecolabs.wagner.juego;
import android.os.Handler;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class Prueba extends ActionBarActivity {
private static final String TAG="GAME";
final Handler hd= null;
//metodo donde cree un subproceso que espera 4 segundos y luego ejecuta el Runnable
public void hilo(){
Thread th= new Thread(){
public void run(){
try {
Log.d(TAG,"Entramos al hilo antes del sleep");
Thread.sleep(4000);
Log.i(TAG,"PASAMOS EL SLEEP");
Log.i(TAG,"PASAMOS EL RUNNABLE");
} catch (Exception e) {
e.printStackTrace();
}
hd.post(cor);
}
};
th.start();
}
///Runnable que ejecuta el subproceso despues de los 4 segundos
final Runnable cor=new Runnable() {
@Override
public void run() {
Log.i(TAG,"ENTRAMOS AL RUNABLE");
Button btn=(Button)findViewById(R.id.btnPrueba);
Log.i(TAG,"IDENTIFICAMOS ELBOTON");
btn.setText("cambieAgain");
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_prueba);
Button btn=(Button)findViewById(R.id.btnPrueba);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
hilo();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_prueba, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
Este programa lo que hace o inteto que haga es que al presionar el boton espere 4 segundos y luego me cambie el texto del mismo boton por uno nuevo pero cuando lo ejecuto el programa espera los 4 segundos y despues se cae la aplicacion y no se porque e intentado de Todo desde utilizar Timer y TimerTask mover el Runnable de lugar etc etc y no funciona agradeceria su ayuda.
he aqui el codigo
package com.gecolabs.wagner.juego;
import android.os.Handler;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class Prueba extends ActionBarActivity {
private static final String TAG="GAME";
final Handler hd= null;
//metodo donde cree un subproceso que espera 4 segundos y luego ejecuta el Runnable
public void hilo(){
Thread th= new Thread(){
public void run(){
try {
Log.d(TAG,"Entramos al hilo antes del sleep");
Thread.sleep(4000);
Log.i(TAG,"PASAMOS EL SLEEP");
Log.i(TAG,"PASAMOS EL RUNNABLE");
} catch (Exception e) {
e.printStackTrace();
}
hd.post(cor);
}
};
th.start();
}
///Runnable que ejecuta el subproceso despues de los 4 segundos
final Runnable cor=new Runnable() {
@Override
public void run() {
Log.i(TAG,"ENTRAMOS AL RUNABLE");
Button btn=(Button)findViewById(R.id.btnPrueba);
Log.i(TAG,"IDENTIFICAMOS ELBOTON");
btn.setText("cambieAgain");
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_prueba);
Button btn=(Button)findViewById(R.id.btnPrueba);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
hilo();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_prueba, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
Valora esta pregunta


0