Android应用中的自定义事件监听器

时间:2022-07-25 20:04:00

I need to set up a simple event listener to refresh a ListView once in a while. The problem is I don't know how could I generate an event.

我需要设置一个简单的事件监听器来不时刷新ListView。问题是我不知道如何生成事件。

I know that for events like key or button pressing I just need to implement the Handler. But in this specific case I actually need to generate the event, which will be fired every time another running thread of my app wakes up and refreshes its list of news from an RSS feed.

我知道,对于像按键或按钮按下这样的事件,我只需要实现处理程序。但在这个特定的情况下,我实际上需要生成事件,每当我的应用程序的另一个运行线程醒来并刷新它的RSS提要列表时,它就会被触发。

I've done everything, but got stuck in here. Can I get any suggestion or link with some more info on how to implement this?

我做了所有的事,但是被困在这里了。我能得到任何建议或链接更多的信息如何实现这一点吗?

4 个解决方案

#1


89  

  1. Define a callback interface

    定义一个回调接口

            public interface NewsUpdateListener 
            {
                void onNewsUpdate(<News data to be passed>);
            }
    
  2. Provide a registration facility on the background thread which gets the RSS feed

    在获取RSS提要的后台线程上提供注册功能

        class <Background processing class name> 
        {
        ....
            ArrayList<NewsUpdateListener> listeners = new ArrayList<NewsUpdateListener> ();
        .... 
            public void setOnNewsUpdateListener (NewsUpdateListener listener) 
            {
                // Store the listener object
                this.listeners.add(listener);
            }
        ....
        }
    
  3. Fire the callback when news is available

    在有消息时触发回调

    ....
    for (listener : listeners) 
    {
        listener.onNewsUpdate(<News data to be passed>);
    }
    ....
    
  4. Register listener somewhere during initialization

    在初始化期间在某处注册监听器

    ....
        <class <Background processing class object>.registerListener
    (
        new OnNewsUpdateListener() {
            onNewsUpdate(<News Data>) {
                // process news data
                runOnUIThread(new Runnable() {
                    public void run() {
                        // refresh list view
                    }
                }
            }
    }
    ....
    

#2


27  

try this:

试试这个:

interface MyHandlerInterface
{
   void onHandle(Object obj)
}
class myListClass
{
   MyHandlerInterface myHandler;
   public void setHandlerListener(MyHandlerInterface listener)
   {
      myHandler=listener;
   }
   protected void myEventFired(myObj)
   {
      if(myHandler!=null)
         myHandler.onHandle(myObj);
   }
}

#3


3  

It sounds like you need a Handler - (look-up android.os.Handler for details).

听起来你需要一个处理器。细节的处理程序)。

The sendMessageDelayed method will allow you to schedule when the message is sent to your handler.

sendMessageDelayed方法将允许您在消息发送到处理程序时进行调度。

A quick search pulled up a full example that should get you started: http://www.tutorialforandroid.com/2009/01/using-handler-in-android.html

快速搜索一下,就会看到一个完整的示例:http://www.tutorialforandroid.com/2009/01/use -handler-in-android.html

#4


2  

You can use android life cycle for that.

你可以使用android的生命周期。

Create a signal interface, aka your event

创建一个信号接口,也就是你的事件

interface NewsUpdateSignal{
    void newsUpdateHandler(Mydata data);
}

Than register to it inside your activity or anywhere else you want, there could be many listeners to same Signal.

在你的活动或其他任何你想要的地方注册它,可能会有很多的侦听器发出相同的信号。

class MyActivity extends Activity implements NewsUpdateSignal{
    Signal<NewsUpateSignal> newsUpdateSignal = SignalsBag.inject(NewsUpateSignal.class);

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        newsUpdateSignal.addListener(this);        
    }

    @Override
    public void newsUpdateHandler(final Mydata data){
           //Do something here 
    }
}

And dispatch the signal when you need, from where ever you need.

当你需要的时候,从你需要的地方发出信号。

Class A{
    Signal<NewsUpateSignal> newsUpdateSignal = SignalsBag.inject(NewsUpateSignal.class);

    void execute(){
        // obtain the data somehow, and dispatch it, the callback will be invoked on the same thread
        newsUpdateSignal.dispatcher.newsUpdateHandler(data);
    }        
}

Disclaimer: I am the author of android life cycle.

免责声明:我是android生命周期的作者。

#1


89  

  1. Define a callback interface

    定义一个回调接口

            public interface NewsUpdateListener 
            {
                void onNewsUpdate(<News data to be passed>);
            }
    
  2. Provide a registration facility on the background thread which gets the RSS feed

    在获取RSS提要的后台线程上提供注册功能

        class <Background processing class name> 
        {
        ....
            ArrayList<NewsUpdateListener> listeners = new ArrayList<NewsUpdateListener> ();
        .... 
            public void setOnNewsUpdateListener (NewsUpdateListener listener) 
            {
                // Store the listener object
                this.listeners.add(listener);
            }
        ....
        }
    
  3. Fire the callback when news is available

    在有消息时触发回调

    ....
    for (listener : listeners) 
    {
        listener.onNewsUpdate(<News data to be passed>);
    }
    ....
    
  4. Register listener somewhere during initialization

    在初始化期间在某处注册监听器

    ....
        <class <Background processing class object>.registerListener
    (
        new OnNewsUpdateListener() {
            onNewsUpdate(<News Data>) {
                // process news data
                runOnUIThread(new Runnable() {
                    public void run() {
                        // refresh list view
                    }
                }
            }
    }
    ....
    

#2


27  

try this:

试试这个:

interface MyHandlerInterface
{
   void onHandle(Object obj)
}
class myListClass
{
   MyHandlerInterface myHandler;
   public void setHandlerListener(MyHandlerInterface listener)
   {
      myHandler=listener;
   }
   protected void myEventFired(myObj)
   {
      if(myHandler!=null)
         myHandler.onHandle(myObj);
   }
}

#3


3  

It sounds like you need a Handler - (look-up android.os.Handler for details).

听起来你需要一个处理器。细节的处理程序)。

The sendMessageDelayed method will allow you to schedule when the message is sent to your handler.

sendMessageDelayed方法将允许您在消息发送到处理程序时进行调度。

A quick search pulled up a full example that should get you started: http://www.tutorialforandroid.com/2009/01/using-handler-in-android.html

快速搜索一下,就会看到一个完整的示例:http://www.tutorialforandroid.com/2009/01/use -handler-in-android.html

#4


2  

You can use android life cycle for that.

你可以使用android的生命周期。

Create a signal interface, aka your event

创建一个信号接口,也就是你的事件

interface NewsUpdateSignal{
    void newsUpdateHandler(Mydata data);
}

Than register to it inside your activity or anywhere else you want, there could be many listeners to same Signal.

在你的活动或其他任何你想要的地方注册它,可能会有很多的侦听器发出相同的信号。

class MyActivity extends Activity implements NewsUpdateSignal{
    Signal<NewsUpateSignal> newsUpdateSignal = SignalsBag.inject(NewsUpateSignal.class);

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        newsUpdateSignal.addListener(this);        
    }

    @Override
    public void newsUpdateHandler(final Mydata data){
           //Do something here 
    }
}

And dispatch the signal when you need, from where ever you need.

当你需要的时候,从你需要的地方发出信号。

Class A{
    Signal<NewsUpateSignal> newsUpdateSignal = SignalsBag.inject(NewsUpateSignal.class);

    void execute(){
        // obtain the data somehow, and dispatch it, the callback will be invoked on the same thread
        newsUpdateSignal.dispatcher.newsUpdateHandler(data);
    }        
}

Disclaimer: I am the author of android life cycle.

免责声明:我是android生命周期的作者。