简体   繁体   English

为什么我的所有听众都没有注册?

[英]Why aren't all my listeners registered?

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");
    }

}

I am expecting to get: 我期望得到:

I onDownloaded 我在下载

You onDownloaded 您在下载

PATTERNSActivity onDownloaded PATTERNS活动已下载

But I am getting only: 但是我只得到:

PATTERNSActivity onDownloaded PATTERNS活动已下载

What could it be the problem? 可能是什么问题?

You keep registered downloaders in a single instance property: 您可以将注册的下载程序保留在单个实例属性中:

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

The last one registered is the activity's: 注册的最后一个是活动的:

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);

Edit based on your comment. 根据您的评论进行编辑。

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;
}

In your code, this gets executed by calling mDownloadListener.onDownloaded(); 在您的代码中,这可以通过调用mDownloadListener.onDownloaded();执行。 in the DownloadManager class. 在DownloadManager类中。

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

In don't see why the onDownloaded methods of the I and YOU class should be executed, they're never called. 在看不到为什么应该执行I和YOU类的onDownloaded方法的情况下,它们从未被调用。 Only the OnDownloaded method of your Listener is called. 仅调用您的侦听器的OnDownloaded方法。

For starters, I think you are not using a list. 首先,我认为您没有使用列表。 You just override the value so you will always get the last one: 您只需覆盖该值,就可以始终获得最后一个值:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM