Handler
import android. telecom. Call;
import com. fiannce. bawei. common. LogUtil;
public class ShopHandler {
public ShopMessageQueue shopMessageQueue;
public CallBack mCallBack;
public ShopHandler ( ) {
ShopLooper shopLooper = ShopLooper. getLooper ( ) ;
shopMessageQueue = shopLooper. shopMessageQueue;
}
public ShopHandler ( CallBack callBack) {
ShopLooper shopLooper = ShopLooper. getLooper ( ) ;
shopMessageQueue = shopLooper. shopMessageQueue;
mCallBack = callBack;
}
public void sendEmptyDelayMessage ( int what, int delayMills) {
ShopMessage shopMessage = new ShopMessage ( ) ;
shopMessage. what = what;
shopMessage. when = System. currentTimeMillis ( ) + delayMills;
shopMessage. target = this ;
shopMessageQueue. enqueueMessage ( shopMessage) ;
}
public void sendEmptyMessage ( int what) {
sendEmptyDelayMessage ( what, 0 ) ;
}
public void post ( Runnable runnable) {
ShopMessage shopMessage = new ShopMessage ( ) ;
shopMessage. callBack = runnable;
shopMessage. target = this ;
shopMessageQueue. enqueueMessage ( shopMessage) ;
}
public void dispatchMessage ( ShopMessage shopMessage) {
if ( shopMessage. callBack!= null) {
shopMessage. callBack. run ( ) ;
return ;
} else {
if ( mCallBack!= null) {
if ( mCallBack. handleMessage ( shopMessage) ) {
return ;
}
}
handleMessage ( shopMessage) ;
}
}
public void handleMessage ( ShopMessage shopMessage) {
LogUtil. d ( "what = " + shopMessage. what) ;
}
public interface CallBack {
boolean handleMessage ( ShopMessage shopMessage) ;
}
}
Looper
public class ShopLooper {
private static ThreadLocal< ShopLooper> shopLooperThreadLocal = new ThreadLocal < > ( ) ;
public ShopMessageQueue shopMessageQueue;
public static ShopLooper getLooper ( ) {
return shopLooperThreadLocal. get ( ) ;
}
public ShopLooper ( ) {
shopMessageQueue = new ShopMessageQueue ( ) ;
}
public static void prepare ( ) {
ShopLooper shopLooper = new ShopLooper ( ) ;
shopLooperThreadLocal. set ( shopLooper) ;
}
public static void loop ( ) {
for ( ; ; ) {
ShopLooper shopLooper = shopLooperThreadLocal. get ( ) ;
ShopMessage shopMessage = shopLooper. shopMessageQueue. next ( ) ;
shopMessage. target. dispatchMessage ( shopMessage) ;
}
}
}
Message
public class ShopMessage {
public ShopHandler target;
public Runnable callBack;
public long when;
public int what;
}
MessageQueue
import java. util. ArrayList;
import java. util. List;
import java. util. Timer;
import java. util. TimerTask;
public class ShopMessageQueue {
List< ShopMessage> shopMessageList = new ArrayList < > ( ) ;
private Timer timer = new Timer ( ) ;
public synchronized ShopMessage next ( ) {
for ( ; ; ) {
if ( shopMessageList. size ( ) > 0 ) {
ShopMessage shopMessage = shopMessageList. get ( 0 ) ;
long now = System. currentTimeMillis ( ) ;
long timeOut = shopMessage. when- now;
if ( timeOut> 0 ) {
startTimerTask ( timeOut) ;
try {
wait ( ) ;
continue ;
} catch ( InterruptedException e) {
e. printStackTrace ( ) ;
}
} else {
shopMessageList. remove ( shopMessage) ;
return shopMessage;
}
} else {
try {
wait ( ) ;
continue ;
} catch ( InterruptedException e) {
e. printStackTrace ( ) ;
}
}
}
}
private synchronized void notifyLooper ( ) {
notifyAll ( ) ;
}
private void startTimerTask ( long timeOut) {
timer. schedule ( new TimerTask ( ) {
@Override
public void run ( ) {
notifyLooper ( ) ;
}
} , timeOut) ;
}
public synchronized void enqueueMessage ( ShopMessage shopMessage) {
int i = 0 ;
if ( shopMessageList. size ( ) == 0 ) {
shopMessageList. add ( shopMessage) ;
notifyAll ( ) ;
return ;
}
for ( ; i < shopMessageList. size ( ) ; i++ ) {
if ( shopMessage. when>= shopMessageList. get ( i) . when) {
continue ;
} else {
shopMessageList. add ( i, shopMessage) ;
break ;
}
}
if ( i == shopMessageList. size ( ) ) {
shopMessageList. add ( shopMessage) ;
}
notifyAll ( ) ;
}
}