- public class MyThread extends Thread {
-
-
-
- private final static String TAG = "My Thread ===> ";
-
- public void run(){
- Log.d(TAG, "run");
- for(int i = 0; i<100; i++)
- {
- Log.e(TAG, Thread.currentThread().getName() + "i = " + i);
- }
- }
- }
启动线程:
2.创建一个Runnable对象
- public class MyRunnable implements Runnable{
- private final static String TAG = "My Runnable ===> ";
-
- @Override
- public void run() {
-
- Log.d(TAG, "run");
- for(int i = 0; i<1000; i++)
- {
- Log.e(TAG, Thread.currentThread().getName() + "i = " + i);
- }
- }
- }
启动线程:
-
- new Thread(new MyRunnable()).start();
3.通过Handler启动线程
- public class MainActivity extends Activity {
-
- private final static String TAG = "UOfly Android Thread ==>";
- private int count = 0;
- private Handler mHandler = new Handler();
- private Runnable mRunnable = new Runnable() {
- public void run() {
- Log.e(TAG, Thread.currentThread().getName() + " " + count);
- count++;
- setTitle("" + count);
-
- mHandler.postDelayed(mRunnable, 3000);
- }
- };
-
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
-
- mHandler.post(mRunnable);
- }
-
- @Override
- protected void onDestroy() {
-
- mHandler.removeCallbacks(mRunnable);
- super.onDestroy();
- }
-
- }
http://www.cnblogs.com/u0fly/archive/2011/01/25/1944645.html