2012-01-04 71 views
0
public interface DownloadListener { 
    public void onDownloaded(); 
} 

public class DownloadManager { 

    private static DownloadManager instance; 

    private DownloadListener mDownloadListener; 

    public static synchronized DownloadManager getInstance(){ 
     if(instance == null) 
      instance = new DownloadManager(); 
     return instance; 
    } 


    private DownloadManager() { 
     myHandler.sendEmptyMessageDelayed(29, 3 * 1000); 
    } 

    public void registerDownloadListener(DownloadListener downloadListener) { 
     mDownloadListener = downloadListener; 
    } 

    Handler myHandler = new Handler(new Handler.Callback() { 
     @Override 
     public boolean handleMessage(Message msg) { 
      if (msg.what == 29) { 
       mDownloadListener.onDownloaded(); 
       return true; 
      } 
      return false; 
     } 
    }); 
} 

public class I implements DownloadListener { 

    public I() { 
     DownloadManager.getInstance().registerDownloadListener(this); 
    } 

    @Override 
    public void onDownloaded() { 
     Log.e("TAG", "I onDownloaded"); 
    } 
} 

public class You implements DownloadListener { 

    public You() { 
     DownloadManager.getInstance().registerDownloadListener(this); 
    } 

    @Override 
    public void onDownloaded() { 
     Log.e("TAG", "You onDownloaded"); 
    } 

} 

public class PATTERNSActivity extends Activity implements DownloadListener { 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     new I(); 
     new You(); 

     DownloadManager.getInstance().registerDownloadListener(this); 
    } 

    @Override 
    public void onDownloaded() { 
     Log.e("TAG","PATTERNSActivity onDownloaded"); 
    } 

} 

我期待着得到:爲什麼我的聽衆沒有註冊?

我onDownloaded

您onDownloaded

PATTERNSActivity onDownloaded

但我只得到:

PATTERNSActivity onDownloaded

這是什麼問題?

回答

4

你讓註冊下載者在單一實例屬性:

// Last call's downloadListener wins. 
public void registerDownloadListener(DownloadListener downloadListener) { 
    mDownloadListener = downloadListener; 
} 

最後一個登記的是活動的:

new I(); // First set singleton's property to an instance of I... 
new You(); // ...then to an instance of You... 
// ...then to the current instance. 
DownloadManager.getInstance().registerDownloadListener(this); 

編輯根據您的評論。

public void registerDownloadListener(DownloadListener downloadListener) { 
    mDownloadListeners.add(downloadListener); 
} 

... 

public boolean handleMessage(Message msg) { 
    if (msg.what != 29) { 
     return false; 
    } 

    for (DownloadListener listener : mDownloadListeners) { 
     listener.onDownloaded(); 
    } 

    return true; 
} 
+0

我知道,如果我創建3個不同的DowmloadManager的每3個對象正在偵聽下載狀態 - 一切正常。但我想只有一個DownloadManager。有沒有可能? – drifter 2012-01-04 19:36:49

+0

@drifter當然;通過將每個監聽器保存在列表中來註冊。在處理程序中迭代列表,調用每個監聽器的'onDownloaded'。請參閱編輯。 – 2012-01-04 19:38:44

+0

所以我需要使用觀察者模式和聽衆列表來解決這個問題。這只是這種目的的方法嗎? – drifter 2012-01-04 19:42:18

1

在你的代碼中,這通過調用mDownloadListener.onDownloaded()來執行。在DownloadManager類中。

@Override 
public void onDownloaded() { 
    Log.e("TAG","PATTERNSActivity onDownloaded"); 
} 

在不明白爲什麼我和你類的onDownloaded方法應該被執行,他們從來不叫。只調用Listener的OnDownloaded方法。

0

對於初學者,我認爲你沒有使用一個列表。你只覆蓋該值,所以你總是會得到最後一個:

public void registerDownloadListener(DownloadListener downloadListener) { 
     mDownloadListener = downloadListener; 
    }