0% found this document useful (0 votes)
56 views5 pages

Apostila 20140414

The document describes how to create a simple example using threads in Android. It involves creating a Process class that implements Runnable and updates a button's text value every second. A new thread is started from the MainActivity onCreate method by passing a Process instance. The button is added to a relative layout and its text continuously increments from the background thread.

Uploaded by

LOlzito
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
56 views5 pages

Apostila 20140414

The document describes how to create a simple example using threads in Android. It involves creating a Process class that implements Runnable and updates a button's text value every second. A new thread is started from the MainActivity onCreate method by passing a Process instance. The button is added to a relative layout and its text continuously increments from the background thread.

Uploaded by

LOlzito
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Threads em Android

Iremos construir outro exemplo bem simples utilizando thread.

Criar projeto no ADT: prjThread02

Crie uma classe chamada Processo

import android.util.Log;
import android.widget.Button;

public class Processo implements Runnable {


public Processo(MainActivity MainActivity){
this.MainActivity = MainActivity;
button = null;
rodando = true;
i = 0;
}

private int i;

private MainActivity MainActivity;

private Button button;

private static boolean rodando;

public synchronized static boolean getRodando() {


return rodando;
}

public synchronized static void setRodando(boolean _rodando) {


rodando = _rodando;
}

@Override
public void run() {
while (rodando) {

try {
if (button == null) {
MainActivity.getHandler().post(new Runnable() {

@Override
public void run() {
button = MainActivity.getButton();
button.setText(Integer.toString(i));
button = null;
}
});
}
i++;
Thread.sleep(1000);
} catch (Exception e) {
Log.e("ERRO!!!", "Erro no mtodo RUN:\n" + e.getMessage());
}
}
}
}

Deixar o MainActivity.java da seguinte forma:

private Handler handler;

private Thread thread;

private Button button;

private RelativeLayout relativeLayout;

public synchronized Button getButton(){


return this.button;
}

public synchronized Handler getHandler(){


return this.handler;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

try {

button = new Button(this);


button.setText(Integer.toString(0));
button.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View arg0) {
if (thread != null) {
Processo.setRodando(false);
}
}
});

relativeLayout = (RelativeLayout)findViewById(R.id.relativeLayout1);
relativeLayout.addView(button);

handler = new Handler();

thread = new Thread(new Processo(this));

thread.start();

} catch (Exception e) {
Log.e("ERRO!!!", "Erro no mtodo onCreate:\n" + e.getMessage());
}
}

@Override
protected void finalize() throws Throwable {

Thread.sleep(5000);
super.finalize();
}

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

try {

button = new Button(this);


button.setText(Integer.toString(0));
button.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View arg0) {
if (thread != null) {
Processo.setRodando(false);
}
}
});

relativeLayout = (RelativeLayout)findViewById(R.id.relativeLayout1);
relativeLayout.addView(button);

handler = new Handler();

thread = new Thread(new Processo(this));

thread.start();

} catch (Exception e) {
Log.e("ERRO!!!", "Erro no mtodo onCreate:\n" + e.getMessage());
}
}

@Override
protected void finalize() throws Throwable {
Thread.sleep(5000);
super.finalize();
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {

// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}

Deixar o activity_main.xml conforme a seguir:

<?xml version="1.0" encoding="utf-8"?>


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" android:weightSum="1">
<RelativeLayout
android:layout_width="fill_parent"
android:id="@+id/relativeLayout1"
android:layout_height="fill_parent">
</RelativeLayout>
</LinearLayout>

You might also like