Cours6 les exemples du direct

Modérateurs : Douin, F. Lemoine, Farinone

Douin
Messages : 167
Inscription : 18 mars 2009 15:46

Cours6 les exemples du direct

Message par Douin » 03 avr. 2017 15:07

Bonjour, à la demande générale

Code : Tout sélectionner

package thread.smb116.cnam.fr.coursthread;

import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Handler;
import android.os.Message;
import android.os.SystemClock;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    private TextView tv;
    //private Handler handler;
    //ProgressDialog mProgressDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout);
        tv = (TextView) findViewById(R.id.textViewId);
//        this.handler = new Handler() {
//            public void handleMessage(Message msg) {
//                if (msg.what == 1) {
//                    mProgressDialog.setMessage((String) msg.obj);
//                } else {
//                    Toast.makeText(MainActivity.this,
//                            (String) msg.obj, Toast.LENGTH_LONG).show();
//                    mProgressDialog.dismiss();
//                }
//            }
//        };
    }

//    public void onClickButton(View v){
//
//            mProgressDialog = ProgressDialog.show(this, "Patience",
//                    "de longs calculs commencent...", true);
//            Runnable r = new Runnable(){
//                public void run(){
//
//                    String progressBarData = "le premier calcul débute...";
//                    Message msg = handler.obtainMessage(1, (Object) progressBarData);
//                    handler.sendMessage(msg);
//                    //doLongOperation1();
//                    SystemClock.sleep(5000);
//
//                    progressBarData = "et maintenant le deuxième calcul...";
//                    msg = handler.obtainMessage(1, (Object) progressBarData);
//                    handler.sendMessage(msg);
//                    //doLongOperation2();
//                    SystemClock.sleep(7000);
//
//
//                    progressBarData = "voilà c'est fini...";
//                    msg = handler.obtainMessage(2, (Object) progressBarData);
//                    handler.sendMessage(msg);
//                }};
//            new Thread(r).start();
//        }

    public void onClickButton(View v) {
        //TaskDialog task = new TaskDialog();
        Task task = new Task();
        task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR,2000);
    }

    private class TaskDialog extends AsyncTask<Void, String, String> {

        private ProgressDialog mProgressDialog;
        @Override
        protected void onPreExecute() {
            mProgressDialog =
                    ProgressDialog.show(MainActivity.this,
                            "Patience",
                            "de longs calculs commencent...", true);
        }

        @Override
        protected void onPostExecute(String msg) {
            Toast.makeText(MainActivity.this,
                    msg, Toast.LENGTH_LONG).show();
            mProgressDialog.dismiss();
        }

        @Override
        protected String doInBackground(Void... voids) {
            String progressBarData = "le premier calcul débute...";
            publishProgress(progressBarData);
            SystemClock.sleep(15000);
            progressBarData = "et maintenant le deuxième calcul...";
             //doLongOperation2();
            publishProgress(progressBarData);
            SystemClock.sleep(27000);
            return "voilà c'est fini...";

        }

        @Override
        protected void onProgressUpdate(String... values) {
            mProgressDialog.setMessage(values[0]);
        }
    }

    private class Task extends AsyncTask<Integer, Integer, Void> {
        @Override
        protected void onPreExecute() {
            tv.setText("demarrage");
        }

        @Override
        protected void onPostExecute(Void aVoid) {
            tv.setText("fin");
        }

        @Override
        protected Void doInBackground(Integer... values) {
            int delai = values[0];
            for (int i = 1; i <= 5; i++) {
                SystemClock.sleep(delai);
                publishProgress(i, delai);
            }
            SystemClock.sleep(300);
            return null;
        }

        @Override
        protected void onProgressUpdate(Integer... values) {
            int duree = values[0];
            int delai = values[1];
            tv.setText(duree + " " + delai + "ms");
        }
    }

}

Code : Tout sélectionner

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">


    <TextView
        android:id="@+id/textViewId"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"

        android:text="TextView"
        android:textSize="64dp" />

    <Button
        android:id="@+id/buttonId"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="onClickButton"
        android:text="Button" />
</LinearLayout>

Répondre

Qui est en ligne ?

Utilisateurs parcourant ce forum : Aucun utilisateur inscrit et 1 invité