Jason_gang 发表于 2013-1-28 18:48:17

java

package com.test;

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

public class TestHandler extends Activity {
    /** Called when the activity is first created. */


//myHandler tt=null;
    @Override
    public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.main);
//      myHandlertt=new myHandler();
      
      Worker mWork = new Worker("test here");
      
    }

   
}

   
    class myHandler extends Handler{
    private static final int OVERRIDE_URL = 0;
   private final Object mLock = new Object();
@Override
public void handleMessage(Message msg) {
// TODO Auto-generated method stub
super.handleMessage(msg);
System.out.println("==========msg is "+msg);
          synchronized (mLock) {
          mLock.notifyAll();
            }
      
}

public myHandler(){
    Message msg = obtainMessage(OVERRIDE_URL);
   System.out.println("-----------------one");
      synchronized (mLock) {
            sendMessage(msg);
            try {
                System.out.println("-----------------two");
                mLock.wait();
            } catch (InterruptedException e) {
            }
      }
      System.out.println("----------------three");

}

    }

   
   
   
   class Worker implements Runnable {
      private final Object mLock = new Object();
      private Looper mLooper;
      
      /**
         * Creates a worker thread with the given name. The thread
         * then runs a {@link android.os.Looper}.
         * @param name A name for the new thread

         */
      Worker(String name) {
            Thread t = new Thread(null, this);
            t.setPriority(Thread.MIN_PRIORITY);
            t.start();
            synchronized (mLock) {
//                while (mLooper == null) {
                  try {
                  System.out.println("------------here wait1 ------------");
                        mLock.wait();
                  } catch (InterruptedException ex) {
//                  }
                }
            }
            
          System.out.println("------------here wait 3 ------------");
      }
      
//      public Looper getLooper() {
//            return mLooper;
//      }
      
      public void run() {
            synchronized (mLock) {
//                Looper.prepare();
//                mLooper = Looper.myLooper();
                mLock.notifyAll();
            System.out.println("------------here wait 2 ------------");
            }
//            Looper.loop();
      
      }
      
//      public void quit() {
//            mLooper.quit();
//      }
    }
页: [1]
查看完整版本: java