1.androidä¸looperçå®ç°åçï¼ä¸ºä»ä¹è°ç¨looper.prepareå°±å¨å½å线ç¨å
³èäºä¸ä¸ªlo
androidä¸looperçå®ç°åçï¼ä¸ºä»ä¹è°ç¨looper.prepareå°±å¨å½å线ç¨å ³èäºä¸ä¸ªlo
å®é ä¸ï¼æ¶æ¯åéå计åä»»å¡æ交ä¹åï¼å®ä»¬é½ä¼è¿å ¥æ线ç¨çæ¶æ¯éåä¸ï¼æ们å¯ä»¥æè¿ä¸ªçº¿ç¨ç§°ä¹ä¸ºç®æ 线ç¨ãä¸è®ºæ¯ä¸»çº¿ç¨è¿æ¯å线ç¨é½å¯ä»¥æ为ç®æ 线ç¨ãä¸ä¾ä¸ä¹æ以å¨ä¸»çº¿ç¨ä¸å¤çæ¶æ¯ï¼æ¯å 为æ们è¦æ´æ°UIï¼æç §androidä¸çè§å®æä»¬å¿ é¡»ç±ä¸»çº¿ç¨æ´æ°UIãæ以æ们让主线ç¨æ为äºç®æ 线ç¨ã
é£ä¹å¦ä½æ§å¶è®©æ个线ç¨æ为ç®æ 线ç¨å¢ï¼
è¿å°±å¼åºäºLooperçæ¦å¿µãAndroidç³»ç»ä¸å®ç°äºæ¶æ¯å¾ªç¯æºå¶ï¼Androidçæ¶æ¯å¾ªç¯æ¯é对线ç¨çï¼æ¯ä¸ªçº¿ç¨é½å¯ä»¥æèªå·±çæ¶æ¯éååæ¶æ¯å¾ªç¯ãAndroidç³»ç»ä¸çéè¿Looper帮å©çº¿ç¨ç»´æ¤çä¸ä¸ªæ¶æ¯éååæ¶æ¯å¾ªç¯ãéè¿Looper.myLooper()å¾å°å½å线ç¨çLooper对象ï¼éè¿Looper.getMainLooper()å¾å°å½åè¿ç¨ç主线ç¨çLooper对象ã
åé¢æå°æ¯ä¸ªçº¿ç¨é½å¯ä»¥æèªå·±çæ¶æ¯éååæ¶æ¯å¾ªç¯ï¼ç¶èæ们èªå·±å建ç线ç¨é»è®¤æ¯æ²¡ææ¶æ¯éååæ¶æ¯å¾ªç¯çï¼åLooperï¼ï¼è¦æ³è®©ä¸ä¸ªçº¿ç¨å ·ææ¶æ¯å¤çæºå¶æ们åºè¯¥å¨çº¿ç¨ä¸å è°ç¨Looper.prepare()æ¥å建ä¸ä¸ªLooper对象ï¼ç¶åè°ç¨Looper.loop()è¿å ¥æ¶æ¯å¾ªç¯ãå¦ä¸é¢çæºç æ示ã
å½æ们ç¨Handlerçæé æ¹æ³å建Handler对象æ¶ï¼æå®handler对象ä¸åªä¸ªå ·ææ¶æ¯å¤çæºå¶ç线ç¨ï¼å ·æLooperç线ç¨ï¼ç¸å ³èï¼è¿ä¸ªçº¿ç¨å°±æäºç®æ 线ç¨ï¼å¯ä»¥æ¥åæ¶æ¯å计åä»»å¡äºãHandlerä¸çæé æ¹æ³å¦ä¸ï¼
[java] view
plaincopyprint?计计语录网站 模板 源码
public Handler() {
if (FIND_POTENTIAL_LEAKS) {
final Class<? extends Handler> klass = getClass();
if ((klass.isAnonymousClass() || klass.isMemberClass() || klass.isLocalClass()) &&
(klass.getModifiers() & Modifier.STATIC) == 0) {
Log.w(TAG, "The following Handler class should be static or leaks might occur: " +
klass.getCanonicalName());
}
}
mLooper = Looper.myLooper();
if (mLooper == null) {
throw new RuntimeException(
"Can't create handler inside thread that has not called Looper.prepare()");
}
mQueue = mLooper.mQueue;
mCallback = null;
}
public Handler(Looper looper) {
mLooper = looper;
mQueue = looper.mQueue;
mCallback = null;
}
public Handler() {
if (FIND_POTENTIAL_LEAKS) {
final Class<? extends Handler> klass = getClass();
if ((klass.isAnonymousClass() || klass.isMemberClass() || klass.isLocalClass()) &&
(klass.getModifiers() & Modifier.STATIC) == 0) {
Log.w(TAG, "The following Handler class should be static or leaks might occur: " +
klass.getCanonicalName());
}
}
mLooper = Looper.myLooper();
if (mLooper == null) {
throw new RuntimeException(
"Can't create handler inside thread that has not called Looper.prepare()");
}
mQueue = mLooper.mQueue;
mCallback = null;
}
public Handler(Looper looper) {
mLooper = looper;
mQueue = looper.mQueue;
mCallback = null;
}
å¨ä¸è¿°ç计æ¶å¨çä¾åä¸ï¼ä¹æ以å¯ä»¥å¨ä¸»çº¿ç¨ä¸å¤çæ¶æ¯èæ们èªå·±å¹¶æ²¡æè°ç¨Looper.prepare()çæ¹æ³ï¼æ¯å 为Androidç³»ç»å¨Activityå¯å¨æ¶ä¸ºå ¶å建ä¸ä¸ªæ¶æ¯éååæ¶æ¯å¾ªç¯ï¼å½æ们ç¨æ åçHandleræé æ¹æ³å建对象æ¶åç¨äºå½å线ç¨çLooper对象ï¼åå°handlerä¸ä¸»çº¿ç¨ä¸çLooper对象è¿è¡äºå ³èã
androidä¸æ¯ä½¿ç¨Looperæºå¶æ¥å®ææ¶æ¯å¾ªç¯çï¼ä½æ¯æ¬¡å建线ç¨æ¶é½å åå§åLooperæ¯è¾éº»ç¦ï¼å æ¤Android为æ们æä¾äºä¸ä¸ªHandlerThreadç±»ï¼ä»å°è£ äºLooper对象ï¼æ¯æ们ä¸ç¨å ³å¿Looperçå¼å¯åéæ¾é®é¢ã
ä¸ç®¡æ¯ä¸»çº¿ç¨è¿æ¯å ¶ä»çº¿ç¨åªè¦æLooperç线ç¨ï¼å«ç线ç¨å°±å¯ä»¥åè¿ä¸ªçº¿ç¨çæ¶æ¯éåä¸åéæ¶æ¯åä»»å¡ã
æ们使ç¨HandlerThread类代æ¿ä¸ä¸ç¯æç« ä¸çå线ç¨ï¼å¹¶ç¨HandlerThreadç±»ä¸çLooper对象æé Handlerï¼åæ¥åæ¶æ¯çç®æ 线ç¨å°±ä¸æ¯ä¸»çº¿ç¨äºï¼èæ¯HandlerThread线ç¨ã代ç å¦ä¸ï¼
[java] view
plaincopyprint?
public class clockActivity extends Activity {
/** Called when the activity is first created. */
private String TAG="clockActivity";
private Button endButton;
private TextView textView;
private int timer=0;
private boolean isRunning=true;
private Handler handler;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
endButton=(Button)findViewById(R.id.endBtn);
textView=(TextView)findViewById(R.id.textview);
endButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
isRunning=false;
}
});
HandlerThread thread=new HandlerThread("myThread");
handler=new Handler(thread.getLooper());//ä¸HandlerThreadä¸çLooperå¯¹è±¡å ³è
thread.start();
Runnable r=new Runnable(){
@Override
public void run() {
// TODO Auto-generated method stub
if(isRunning){
textView.setText("èµ°äº"+timer+"ç§");
timer++;
handler.postDelayed(this, );//æ交任å¡rï¼å»¶æ¶1ç§æ§è¡
}
}
};
handler.postDelayed(r, );
}
}
ããpublic class clockActivity extends Activity {
/** Called when the activity is first created. */
private String TAG="clockActivity";
private Button endButton;
private TextView textView;
private int timer=0;
private boolean isRunning=true;
private Handler handler;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
endButton=(Button)findViewById(R.id.endBtn);
textView=(TextView)findViewById(R.id.textview);
endButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
isRunning=false;
}
});
HandlerThread thread=new HandlerThread("myThread");
handler=new Handler(thread.getLooper());//ä¸HandlerThreadä¸çLooperå¯¹è±¡å ³è
thread.start();
Runnable r=new Runnable(){
@Override
public void run() {
// TODO Auto-generated method stub
if(isRunning){
textView.setText("èµ°äº"+timer+"ç§");
timer++;
handler.postDelayed(this, );//æ交任å¡rï¼å»¶æ¶1ç§æ§è¡
}
}
};
handler.postDelayed(r, );
}
}
ããæ¤æ¶å¤çä»»å¡ä¼å¨handlerThread线ç¨ä¸å®æãå½ç¶è¿ä¸ªä¾åä¼åºçº¿å¼å¸¸ï¼ä¾ç¶æ¯å 为å¨é主线ç¨ä¸æ´æ°äºUIãè¿æ ·ååªæ¯ä¸ºäºå¤§å®¶è½å¤ç解è¿ç§æºå¶ã
ããæ·±å ¥ç解Androidæ¶æ¯å¤çæºå¶å¯¹äºåºç¨ç¨åºå¼åé常éè¦ï¼ä¹å¯ä»¥è®©æ们对线ç¨åæ¥ææ´å æ·±å»ç认è¯ï¼å¸æè¿ç¯æç« å¯ä»¥å¯¹æå们ææ帮å©ã