如何实现机器人块 [英] how to implement blocks in android

查看:182
本文介绍了如何实现机器人块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在IOS,我们使用块,当我们需要在特定的情况时有发生,以处理动作

有没有这样在android系统的方式来处理情况的 onCompletion ,我们可以添加在以往是。

例如 AsyncTask的方法它知道何时工作完成。它执行onPostExecute。我们怎样才能建立这种类型的方法,当一个特定的情况下达到我们处理它等等。

今天,我觉得它表现得像块在IOS的方式

  BaseAsyncTask(新回拨(){onPostResult(字符串结果)
{
//做任何你想做的事,结果从asynctaks了
}
});

它是一个代表,当它到达一个特定的情况下,它调用..

我说得对,上述code为 Android中块我们在IOS做的。或出现在android的创建块的任何其他方式。


解决方案

看来你有你的问题的两个问题,所以我会回答他们俩


  

有没有这样的android系统中处理情况的方式onCompletion,我们可以添加在以往它是什么?
  如何在达到特定的情况时,我们可以创建这种类型的方法?


是的,有一种方式。我有<一个href=\"http://stackoverflow.com/questions/18582807/java-pass-an-executable-block-as-a-parameter\">asked同样的事情曾经在这里。

在此情况下,您使用的Runnable 。您可以使用它像这样:

  //创建一个可运行的块
Runnable接口块=新的Runnable(){
    @覆盖
    公共无效的run(){
        // code在这里
    }
};//传递它的参数
DoSomething的(块); //就像一个正常的变量使用它的方法//
公共无效DoSomething的(Runnable接口模块){
    block.run();
}

您第二个问题:

  BaseAsyncTask(新回拨(){    onPostResult(字符串结果)
    {
        //做任何你想做的事,结果从asynctaks了
    }
});


  

我说得对,上述code采用的是Android块我们在做IOS。或出现在android的创建块的任何其他方式。


是在我看来,该对象回调行为就像我在previous例子显示了相同目标C块。当异步任务完成(结果是已经可用),则该回调被调用。大概像这样:

  callback.onPostResult();

只是为了您的信息,对象的Runnable 回调是的 Java接口的。更多关于接口这里。如果你没有需要额外的参数,你的回调,你可以使用的Runnable ,以避免重新发明轮子。但如果有一些特别的东西,即你要paramters传递给回调,那么你可以创建自己的界面并使用它像一个的Runnable

In IOS we use blocks when we want to handle an action when a particular situation occur.

Is there a way like this in android to handle situation onCompletion and we can add where ever it is.

Like AsyncTask methods it knows when work is finish . it executes onPostExecute. How we can create this type of method when a particular situation reach and we handle it like .

Today i find a way which behave like Blocks in IOS

BaseAsyncTask(new Callback(){

onPostResult(string result)
{
// do whatever you want to do with the result got from asynctaks
}
});

it is a delegate and it calls when it reaches a particular situation..

Am i right that the above code is block in android as we do in IOS. or there any other way of creating blocks in android.

解决方案

It seems that you have two questions in your question so I will answer them both

Is there a way like this in android to handle situation onCompletion and we can add where ever it is? How we can create this type of method when a particular situation is reached?

Yes there is a way. I have asked the same thing once here.

In this cases, you use Runnable. You use it like so:

//creating a runnable block
Runnable block = new Runnable() {
    @Override
    public void run() {
        //code here
    }
};

//passing it on arguments
doSomething(block); //just like a normal variable

//using it on the method
public void doSomething(Runnable block) {
    block.run();
}

Your 2nd question:

BaseAsyncTask(new Callback(){

    onPostResult(string result)
    {
        // do whatever you want to do with the result got from asynctaks
    }
});

Am i right that the above code is block in android as we do in IOS. or there any other way of creating blocks in android.

Yes in my opinion, the object Callback behaves the same as the blocks in Objective C as I have shown in my previous examples. When the async task is completed (the result is already available) then that Callback is called. Probably like so:

callback.onPostResult();

Just for your information, the Objects Runnable and Callback are java interfaces. More on interfaces here. If you do not need extra parameters for your callback, you can just use the Runnable so as to avoid reinventing the wheel. But if there is something special, i.e. you want to pass paramters to the callback, then you can create your own interface and use it like a Runnable.

这篇关于如何实现机器人块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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