Method1:
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.widget.Toast;
public class Method1 extends Activity {
class PrinterTask extends AsyncTask<String, Void, Void> {
protected Void doInBackground(String... x) {
while (true) {
System.out.println(x[0]);
test();
try {
Thread.sleep(1000);
test2();
} catch (InterruptedException ie) {
ie.printStackTrace();
}
}
}
};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
new PrinterTask().execute("---------------------");
//new PrinterTask().execute("foo foo foo");
System.out.println("onCreate() is done.");
}
public void test2() {
// TODO Auto-generated method stub
System.out.println("test2 is done.");
}
public void test() {
// TODO Auto-generated method stub
System.out.println("test is done.");
}
}
Method2:
import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.KeyEvent;
import android.widget.TextView;
public class Method2 extends Activity implements Runnable {
private String pi_string;
private TextView tv;
private ProgressDialog pd;
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main2);
tv = (TextView) this.findViewById(R.id.tv);
tv.setText("Press any key to start calculation");
}
public boolean onKeyDown(int keyCode, KeyEvent event) {
pd = ProgressDialog.show(this, "Working..", "Calculating Pi", true,
false);
Thread thread = new Thread(this);
thread.start();
return super.onKeyDown(keyCode, event);
}
public void run() {
pi_string = "gfdsgs";
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
handler.sendEmptyMessage(0);
}
private Handler handler = new Handler() {
public void handleMessage(Message msg) {
pd.dismiss();
tv.setText(pi_string);
}
};
}
Friday, July 15, 2011
Thread Concepts:
By Magesh Kumar
Posted at 9:59 PM
Android
No comments
About the Author
Nulla sagittis convallis arcu. Sed sed nunc. Curabitur consequat. Quisque metus enim, venenatis fermentum, mollis in, porta et, nibh. Duis vulputate elit in elit. Mauris dictum libero id justo.
View all posts by: BT9
- Draw 9-patch Tutorial
- Cracking apk to get java code
- Handling Button clicks in a ListView Row
- Android Menu and Submenu Example
- Larry Page: Android powers Google Glass
- Android GPS How to calculate the distance between two GPS coordinates?
- SlidingDrawer
- Gradient dividers in Android
- Thread Concepts:
- How to Crack a Software
Popular Posts
Total Pageviews
Powered by Blogger.
Contributors
About Me
Trending Stories
- Android (72)
- Blackberry (1)
- CSS (6)
- How to Crack a Software (1)
- HTML (14)
- Java (1)
- Java Scripts (6)
- Photoshop (11)
0 comments: