apostila 20140414

download apostila 20140414

of 5

description

apostila 20140414

Transcript of apostila 20140414

  • 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: