Created
June 11, 2012 21:41
-
-
Save condesa/2912922 to your computer and use it in GitHub Desktop.
Prueba de un thread en Android
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.app.Activity; | |
import android.os.Bundle; | |
import android.os.Handler; | |
import android.os.Message; | |
import android.widget.ProgressBar; | |
public class ThreadTestActivity extends Activity { | |
ProgressBar bar; | |
Handler handler = new Handler(){ | |
@Override | |
public void handleMessage(Message msg){ | |
bar.incrementProgressBy(5); | |
} | |
}; | |
boolean isRunning = false; | |
/** Called when the activity is first created. */ | |
@Override | |
public void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.main); | |
bar = (ProgressBar)findViewById(R.id.progress); | |
} | |
public void onStart(){ | |
super.onStart(); | |
bar.setProgress(0); | |
Thread background = new Thread(new Runnable(){ | |
public void run(){ | |
try{ | |
for(int i=0; i<20; i++){ | |
Thread.sleep(1000); | |
handler.sendMessage(handler.obtainMessage()); | |
} | |
}catch(Throwable t){ | |
//Termina el thread en background | |
} | |
} | |
}); | |
isRunning = true; | |
background.start(); | |
} | |
public void stop(){ | |
super.onStop(); | |
isRunning = false; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment