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

查看:308
本文介绍了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. 我需要一个回调界面吗?

  2. 还是监听器接口?

  3. 或两者都有?



我在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.

strong> UPDATE:

UPDATE:

好,我已经为我的回调BitmapCallback接口添加了implements到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活动中移动回调定义,因为我想让客户的应用程序在代码中实现回调,以便在返回到客户应用程序时处理位图。

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.

这是我的Activity中的回调接口: / p>

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不应该知道客户类中定义的类型。

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中添加一个字段来存储回调的实例(或者如果您想支持回调多个侦听器,则更复杂)和一个设置侦听器的方法。

  2. 使用回调模式:使用回调参数增强相关的API方法。
  3. li>
  1. use a listener pattern: in your pojo add a field to store an instance of the callback (or something more complicated if you want to support calling back to multiple listeners) and a method for setting the listener. Call the method from your activity (or wherever is appropriate) when you create or are setting up the pojo.
  2. use a callback pattern: enhance the relevant API methods with a callback argument.

在任何一种情况下,当位图准备就绪后,您就可以将位图返回到活动。

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

EDIT

下面是一个如何结构化的示例。你没有提供任何关于如何创建pojo或调用处理的信息,所以我发明了一个例子。它假定处理正在单独的线程中进行,这将在完成后回调到活动。您可以根据需要修改它。 (特别是,如果pojo同步执行工作,这可能会更简单。此外,最好使用 AsyncTaskLoader ,而不是普通的vanilla线程,以便与 Activity 生命周期。)

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
$ b

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 / p>

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天全站免登陆