Android之Handler有感 三

2021-09-08 03:08:55 字數 1420 閱讀 2948

主線程給主線程傳送訊息:

public class mainsendtomain extends activity  

}); 

} class myhandler extends handler 

public void handlemessage(message msg) 

其他執行緒給自己傳送訊息:

public class othersendtoits extends activity 

}); 

} class myhandler extends handler 

public void handlemessage(message msg) 

}     

class mythread extends thread 

//定義執行緒類中的訊息處理類 

class threadhandler extends handler 

public void handlemessage(message msg) 

} } 

} } 

其他執行緒給主線程傳送訊息:

public class othersendtomain extends activity

});}

class myhandler extends handler

@override

public void handlemessage(message msg)

}class mythread extends thread}}

主線程給其他執行緒傳送訊息:

public class mainsendtoother extends activity });

mythread thread = new mythread();

thread.start();

}

class mythread extends thread

}

class otherhandler extends handler

public otherhandler(looper looper)

@override

public void handlemessage(message msg)

}

mainhandler mainhandler;

class mainhandler extends handler

public mainhandler(looper looper)

@override

public void handlemessage(message msg)

}

}

android之Handler的使用

handler訊息機制 首先在主線程中建立乙個handler,然後建立乙個訊息message物件,有兩種方法,new message和message.obtain,由handler發出到訊息佇列messagequeue,messagequeue中enqueuemessage方法,對所有的msg進行排...

Android精通之Handler講解

建立乙個handler final handler handler new handler 有可能做大量耗時的操作 子執行緒 new thread new runnable catch interruptedexception e 通知ui更新 handler.sendemptymessage 11...

android學習筆記之Handler

handler handler new handler runable update new runnable publicvoid run system.out.println update handler.postdelayed update,3000 3秒後將update執行緒新增到佇列中 h...