Hallo,
hab nen Problem nen Thread auf meinem Galaxy S3 laufen lassen zu können.
Hier ist der Thread :
|
Quellcode
|
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
|
public class Changer implements Runnable{
MainActivity ma;
boolean run = true;
public Changer (MainActivity ma)
{
this.ma = ma;
}
public void run() {
System.out.println("HI1");
while(run)
{
sleep(50);
Integer i = ma.getInt();
if(i!=null)
ma.setText(""+i);
}
}
private void sleep(int i) {
try{
Thread.sleep(i);
}
catch(InterruptedException e){};
}
public void stop()
{
try {
this.wait();
} catch (InterruptedException e) {
}
}
}
|
Die Funktionen getInt und setText funktionieren.
In der Main mach ich zuerst :
|
Quellcode
|
1
2
3
|
..th = new Changer(this);
th.run();
th.stop();.. // umständlich ich weiß, aber mache das zum testen erstmal so .
|
und dann mit dem toggleButton :
|
Quellcode
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
toggle.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
TextView v = ((TextView)findViewById(R.id.textView1));
v.setText(""+isChecked);
if(!isChecked)
{
th.stop();
}
else
{
th.notify();
}
}
});
|
er scheint beim th.run(); im zweiten code-block zu blockieren und nur die Thread while Schleife abzulaufen und nicht wirklich wie ein Thread zu arbeiten =/
Irgendnen Denkfehler ?
Danke für Tips