Android的监听器/回调如何? [英] Android listener / callback How to?

查看:180
本文介绍了Android的监听器/回调如何?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个生成位图一个POJO方法。完成后,我想将位图抛到UI活动。这是一个简单的,但在这里它是:

I have a pojo method that generates a Bitmap. When done, I want to throw the Bitmap to a UI Activity. This is a sample, but here it is:

private void sendBitmap(Bitmap bitmap) {
    // TODO - I'm in the pojo where I want to send the bitmap
}

FYI:我想通知UI活动是我的项目之外。我的意思是,我的项目是一个SDK,使得其他开发人员将尽快抓住这一信息,因为它成为可用。

I've been trying to figure this out but getting somewhat confused.  

 1. Do I need a "callback" interface? 
 2. Or a "listener" interface? 
 3. Or both?

我在MyActivity创造了一个回调接口。到现在为止还挺好。这是什么样子至今:

I have created a Callback Interface in my MyActivity. So far, so good. This is what it looks like so far:

import android.graphics.Bitmap;

public class MyActivity {

//  protected void whateverOtherMethods (Bundle savedInstanceState) 
//  {
//      .
//      .
//      .
//      .
//      .
//      .
//      
//  }

    /**
     * Callback interface used to supply a bitmap.
     */
    public interface MyCallback {
        public void onBitmapReady(Bitmap bitmap);
    }
}

演示应用程序的DoSomethingActivity可以实现我创建了回调接口。这是回调的演示应用程序的DoSomethingActivity的实现:

The demo application's DoSomethingActivity can implement the Callback Interface that I have created. This is the implementation of the callback in the demo application's DoSomethingActivity:

private final MyCallback myCallback = new MyCallback() {

    @Override
    public void onBitmapReady(Bitmap bitmap) {
        // TODO - do something with the bitmap
    }
};

什么是我错过通知回调的一步?也许我几乎都有,但我不够困惑,我正在寻找一些帮助这里。

What is the step I'm missing to notify the callbacks? Maybe I almost have it, but I'm confused enough that I'm looking for a little help here.

感谢您。

更新:

OK,我已经添加了实施为我的回调BitmapCallback界面进入POJO。在这里,它是:

OK, I've added "implements" for my callback "BitmapCallback" interface into the pojo. Here it is:

public class ProcessImage implements BitmapCallback {

/**
 * pojo method to return bitmap
 */
private void sendBitmap() {
    this.onBitmapReady(bitmap);
}

@Override
public void onBitmapReady(Bitmap bitmap) {
    // we still need to send the bitmap to cusomer App.

}

我不能回调的定义从SDK活动动,因为我想在客户的应用来实现他们的code来处理位图回调,当它返回到客户的应用。

I cannot move the callback definition from the SDK Activity, because I want the customer's application to implement the callback in their code to handle the bitmap when it is returned to the customer's application.

下面是我的活动回调接口:

Here is the callback interface in my Activity:

public interface BitmapCallback {

    public void onBitmapReady(Bitmap processedBitmapy);
}

下面是在客户的应用程序,实现:

Here is the implementation in the customer's application:

private final BitmapCallback bitmapCallback = new BitmapCallback()  
{

    @Override
    public void onBitmapReady(Bitmap bitmap) {
        // TODO Auto-generated method stub          
    }
};

叫我哑巴,但我有一个很难得到我的头解决这个问题。

Call me dumb, but I'm having a hard time getting my head around this.

推荐答案

您快到了。你只需要告诉你的有关回调实例POJO。首先,我会强烈建议您将接口定义成POJO(因为在概念上它与该对象比您的活动做更多),或者使接口的独立型。该POJO不应该知道在客户端类(ES)定义的类型。

You're almost there. You just need to tell your pojo about the callback instance. First, I would strongly recommend that you move the interface definition into the pojo (since conceptually it has to do more with that object than with your activity) or make the interface an independent type. The pojo should not know about types defined in client class(es).

然后,你有一个选择:


  1. 使用侦听器模式:在您的POJO添加一个字段存储的回调(或东西,如果你想支持调用回多的听众更复杂),并设置监听的方法的一个实例。从您的活动调用的方法(或其它地方是合适的)当您创建或正在设置的POJO。

  2. 使用一个回调格局:加强与一个回调参数相关的API方法

在任一情况下,当位图是准备有布线,以获得位图回活动

In either case, when the bitmap is ready you have the wiring to get the bitmap back to the activity.

修改

这里有一个如何这可能是结构化的一个例子。您没有提供有关如何创建你的POJO或调用处理的任何信息,所以我发明的东西只是作为一个例子。它假定处理发生在一个单独的线程,在完成时将回调到活动中。您可以根据需要进行修改。 (具体而言,这可能是更简单,如果POJO正在做的工作同步。此外,它会更好地使用的 AsyncTaskLoader ,而不是一个普通的线程,使其与活动正常进行交互的生命周期。)

Here's an example of how this might be structured. You haven't provided any information about how you create your pojo or invoke the processing, so I invented something just as an example. It assumes that the processing is taking place in a separate thread, which will call back to the activity when done. You can modify this as needed. (In particular, this can be much simpler if the pojo is doing the work synchronously. Also, it would be better to use an AsyncTaskLoader rather than a plain vanilla thread so that it interacts properly with the Activity lifecycle.)

BitmapCallback.java

public interface BitmapCallback {
    public void onBitmapReady(Bitmap processedBitmap);
}

MyActivity.java

public Activity MyActivity extends Activity implements BitmapCallback {
    Handler mHandler; // initialize this in onCreate
    . . .

    public void onRequestImage() {
        ProcessImage processor = new ProcessImage();
        getImage(this /* , other actual args */);
    }

    @Override
    public void onBitmapReady(final Bitmap processedBitmap) {
        // since this may be called from a non-UI thread, we can't update
        // the UI directly.
        mHandler.post(new Runnable() {
            @Override
            public void run() {
                // handle the bitmap (update the UI, etc.)
            }
        });
    }
}

ProcessImage.java

public class ProcessImage {
    public void getAnImage(final BitmapCallback callback /* , other formal args */) {
        new Thread() {
            @Override
            public void run() {
                Bitmap bitmap;
                . . . // do the work
                // then deliver the results
                callback.onBitmapReady(bitmap);
            }
        }.start();
    }
}

这篇关于Android的监听器/回调如何?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆