Skip to content

Instantly share code, notes, and snippets.

@ericksprengel
Created September 28, 2016 18:18
Show Gist options
  • Save ericksprengel/3216767549f3cd6f3cd82eedd8ea99d9 to your computer and use it in GitHub Desktop.
Save ericksprengel/3216767549f3cd6f3cd82eedd8ea99d9 to your computer and use it in GitHub Desktop.
AndroidA03T03 - AsyncTask
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_async_task"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="br.com.ericksprengel.androidaula03.AsyncTaskActivity">
<Button
android:text="Conte!"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="51dp"
android:id="@+id/async_task_activity_start_button" />
<TextView
android:text="--//--"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:id="@+id/async_task_activity_progress_text_view" />
</RelativeLayout>
package br.com.ericksprengel.androidaula03;
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
public class AsyncTaskActivity extends Activity {
TextView mProgress;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_async_task);
// guardar uma referência para o TextView usado para exibir o progresso da AsynxTask
mProgress = (TextView) findViewById(R.id.async_task_activity_progress_text_view);
// adicionar ação de contar versos para o botão.
findViewById(R.id.async_task_activity_start_button).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
contarVersos();
}
});
}
private void contarVersos() {
// Carlos Drummond de Andrade
// Quadrilha
String[] versos = {
"João amava Teresa que amava Raimundo",
"que amava Maria que amava Joaquim que amava Lili",
"que não amava ninguém.",
"João foi para o Estados Unidos, Teresa para o",
"convento,",
"Raimundo morreu de desastre, Maria ficou para tia,",
"Joaquim suicidou-se e Lili casou com J. Pinto",
"Fernandes",
"que não tinha entrado na história." };
ContadorDeVersosAsynctTask atask = new ContadorDeVersosAsynctTask();
atask.execute(versos);
}
class ContadorDeVersosAsynctTask extends AsyncTask<String, Integer, String>{
@Override
protected void onPreExecute() {
mProgress.setText("Iniciando...");
}
@Override
protected String doInBackground(String... params) {
int totalDeLetras = 0;
try {
for(int i = 0; i < params.length; i++) {
Thread.sleep(2000);
totalDeLetras += params[i].length();
publishProgress(i, totalDeLetras);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
return totalDeLetras + " caracteres em " + params.length + " Strings fornecidas.";
}
@Override
protected void onProgressUpdate(Integer... values) {
mProgress.setText("String " + values[0] + ". Total de caracteres: " + values[1]);
}
@Override
protected void onPostExecute(String result) {
mProgress.setText("Resultado: " + result);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment