Bonjour,
J'ai une application qui doit lire et afficher des valeurs de capteurs à intervalles réguliers. Dans une première approche "pédagogique", cette tâche est déléguée à un thread qui va devoir accéder à des widgets texte du thread UI.
Je tourne donc depuis un moment autour de runOnUiThread en m'inspirant de moult articles du web et pour l'instant, j'en suis là.
Mon thread UI
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
 
public class MainActivity extends AppCompatActivity {
 
    private TextView tv_temperature;
    private TextView tv_hygro;
    private Button btn_start, btn_stop;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
        tv_temperature = (TextView) findViewById(R.id.textView_temperature);
        tv_hygro = (TextView) findViewById(R.id.textView_hygro);
        btn_start = (Button) findViewById(R.id.button_start);
        btn_stop = (Button) findViewById(R.id.button_stop);
 
        btn_start.setOnClickListener(
                new View.OnClickListener() {
                    public void onClick(View v) {
                        WorkingThread th = new WorkingThread();
                        th.run();
                    }
                }
        );
 
        btn_stop.setOnClickListener(
                new View.OnClickListener() {
                    public void onClick(View v) {
                        //to write
                    }
                }
        );
    }
 
    //mise à jour du TextView
    public void updateResults(String results) {
        tv_temperature.setText(results);
    }
}
mon Working thread où je ne fais que générer une valeur aléatoire pour l'afficher dans le threadUI
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
 
public class WorkingThread extends Thread {
    private static final String TAG = "WorkingThread";
    private static final int DELAY = 5000; // 5 seconds
    private static final int RANDOM_MULTIPLIER = 10;
 
    //constructeur du thread
    public void WorkingThread() {
        Log.v(TAG, "construct WorkingThread");
    }
 
    @Override
    public void run() {
        Log.v(TAG, "doing work in WorkingThread");
        while (true) {
            int randNum = (int) (Math.random() * RANDOM_MULTIPLIER);
            // need to publish the random number back on the UI at this point in the code through the publishProgress(randNum) call
            publishProgress(randNum);
            try {
                Thread.sleep(DELAY);
            } catch (InterruptedException e) {
                Log.v(TAG, "Interrupting and stopping the WorkingThread");
                return;
            }
        }
    }
 
    private void publishProgress(int randNum) {
        Log.v(TAG, "reporting back from the WorkingThread");
        final String text = String.format(String.valueOf(R.string.service_msg), randNum);
        ((Activity)activity).runOnUiThread(new Runnable() {
            @Override
            public void run() {
                updateResults(text);
            }
        });
    }
}
Mon problème: runOnUiThread() et updateResults() sont tagués "cannot resolve".
J'en ai exploré des syntaxes mais j'en suis toujours là. J'ai le sentiment qu'il faudrait lier ça avec MainActivity mais je ne vois pas comment.
Merci pour votre aide.