Tuesday, June 23, 2009

Using Threads In Android wihtout Async Task

This code is without using Async Task class (Android 1.5) For (Android 1.1 its User Task)
Its not a good way of implementing threads but works
....:-)


package com.arun;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;

public class thread extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final Handler threadCallback = new Handler();
final Runnable runInUIThread = new Runnable(){

@Override
public void run() {
// TODO Auto-generated method stub
}
};
Thread t1= new Thread(){
public void run(){
sayHi();
threadCallback.post(runInUIThread);
}
};
t1.start();
System.out.println("Thread Id 1:::::"+t1.getPriority());
try {
t1.sleep(500);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Thread t2=new Thread(){
public void run(){
sayHello();
threadCallback.post(runInUIThread);
}
};
t2.start();

System.out.println("Thread Id 2:::::"+t2.getPriority());
try {
t2.sleep(10000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void sayHello(){
System.out.println("hello");
}
public void sayHi(){
System.out.println("hi");
}
}

No comments: