Hello, I recently started learning development for android (Game
development to be specific). And I came across the question, to which I
couldn't find an answer.
So my question would be why does this work?:
public class MainActivity extends Activity{
OurView v;
TextView textView;
Random random;
int num = 0;
public void onCreate(Bundle bundle){
super.onCreate(bundle);
v = new OurView(this);
setContentView(v);
textView = (TextView) findViewById(R.id.textView);
random = new Random();
}
protected void onResume(){
super.onResume();
v.resume();
}
public class OurView extends SurfaceView implements Runnable{
Thread t = null;
SurfaceHolder holder;
boolean isItOK = false;
public OurView(Context context) {
super(context);
holder = getHolder();
}
public void run() {
while(isItOK == true){
if(!holder.getSurface().isValid())
continue;
Canvas canvas = holder.lockCanvas();
canvas.drawARGB(random.nextInt(256), random.nextInt(256),
random.nextInt(256), random.nextInt(256));
holder.unlockCanvasAndPost(canvas);
}
}
public void resume(){
isItOK = true;
t = new Thread(this);
t.start();
}
public void pause(){
isItOK = false;
while(true){
try{
t.join();
}catch(Exception e){
e.printStackTrace();
}
break;
}
}
}
}
but this doesn't:
public class MainActivity extends Activity{
OurView v;
TextView textView;
Random random;
int num = 0;
public void onCreate(Bundle bundle){
super.onCreate(bundle);
setContentView(R.layout.activity_main);
textView = (TextView) findViewById(R.id.textView);
random = new Random();
v = new OurView(this);
}
protected void onResume(){
super.onResume();
v.resume();
}
public class OurView extends SurfaceView implements Runnable{
Thread t = null;
SurfaceHolder holder;
boolean isItOK = false;
public OurView(Context context) {
super(context);
holder = getHolder();
}
public void run() {
while(isItOK == true){
num++;
textView.setText(num);
}
}
public void resume(){
isItOK = true;
t = new Thread(this);
t.start();
}
public void pause(){
isItOK = false;
while(true){
try{
t.join();
}catch(Exception e){
e.printStackTrace();
}
break;
}
}
}
}
All I did is changed content view. And in threads run() method I changed
textView's text to "num" variable which is incremented in each while
cycle...
I don't understand why this doesn't work. Why this doesn't use AsyncTask
but uses Just plain old thread. I heard that android doesn't support plain
old Java threads. I'm totally confused, and don't understand anything.
Could someone please explain what's happening here?
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en