2012-01-12 73 views
3

我正在Android應用程序中通過處理程序實現IPC。 在官方文檔(這裏http://developer.android.com/reference/android/app/Service.html)有一個例子:Android中的處理程序

class IncomingHandler extends Handler { 
    @Override 
    public void handleMessage(Message msg) { 
     switch (msg.what) { 
      case MSG_REGISTER_CLIENT: 
       mClients.add(msg.replyTo); 
       break; 
      case MSG_UNREGISTER_CLIENT: 
       mClients.remove(msg.replyTo); 
       break; 
      case MSG_SET_VALUE: 
       mValue = msg.arg1; 
       for (int i=mClients.size()-1; i>=0; i--) { 
        try { 
         mClients.get(i).send(Message.obtain(null, 
           MSG_SET_VALUE, mValue, 0)); 
        } catch (RemoteException e) { 
         // The client is dead. Remove it from the list; 
         // we are going through the list from back to front 
         // so this is safe to do inside the loop. 
         mClients.remove(i); 
        } 
       } 
       break; 
      default: 
       super.handleMessage(msg); 
     } 
    } 
} 

我的問題很簡單:這是什麼線?:做

super.handleMessage(msg); 

我是否一定要調用該方法?


編輯: 我知道這會調用該方法的父的實現。但是那種方法是什麼?在發佈消息之前是否需要做特別的事情?

在此先感謝

回答

4

快速瀏覽一下source code表明這個方法的超類實現什麼也不做。儘管如此,我仍然會這麼稱呼它,因爲未來版本的Android可能會有不同的表現。

2

它調用的handleMessageHandler實施的IncomingHandler父親。

看看Java tutorial

關於您的新問題,應該沒有問題,因爲此方法在Handler類中爲空。

+0

是的,我知道這一點。但我有義務稱這種方法嗎?我正在編輯我的問題,以更具體一點。 – mneri 2012-01-12 15:42:16

+0

好的,謝謝! – mneri 2012-01-12 15:49:07

2

我看了一下android的來源,它只是顯示

/** 
* Subclasses must implement this to receive messages. 
*/ 
public void handleMessage(Message msg) { 
} 

所以,我說沒有。

但是(正如我在鍵入我的答案時敏銳地觀察到的devconsole),無論如何稱它爲好習慣。