Skip to content

Instantly share code, notes, and snippets.

@onlyangel
Created May 21, 2011 01:07
Show Gist options
  • Save onlyangel/984106 to your computer and use it in GitHub Desktop.
Save onlyangel/984106 to your computer and use it in GitHub Desktop.
Codigo de ejemplo de AsynkTask
package com.asynctaskdemo;
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.SystemClock;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity {
MainActivity yo;
TextView tv;
Button but;
Integer ejecuciones = new Integer(20);
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
yo = this;
this.tv = (TextView) this.findViewById(R.id.texto);
this.but = (Button) this.findViewById(R.id.but);
this.but.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
DownloadFilesTask d = new DownloadFilesTask();
d.activityPadre = yo;
d.execute(ejecuciones);
}
});
/*
this.but.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
new Thread(new Runnable() {
public void run() {
for (int i = 0; i < ejecuciones.intValue(); i++) {
SystemClock.sleep(250);
}
}
}).start();
}
});
*/
}
private class DownloadFilesTask extends AsyncTask<Integer, Integer, Long> {
public MainActivity activityPadre;
protected Long doInBackground(Integer... inte) {
for (int i = 0; i < inte[0]; i++) {
SystemClock.sleep(250);
publishProgress((int) ((i / (float) inte[0]) * 100));
}
return new Long(0);
}
protected void onProgressUpdate(Integer... progress) {
tv.setText(""+(progress[0]));
}
protected void onPostExecute(Long result) {
Log.i("MyLog","Downloaded " + result + " bytes");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment