Android中的回调?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2566852/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 06:21:06  来源:igfitidea点击:

Callback in Android?

androidandroid-emulator

提问by Praveen

In Android application development, I frequently go through the word CallBackin many places. I want to know what it means to tell us technically - and how I can manage to use the callbackin applications. I need a guide to understand it and use it.

在Android应用程序开发中,我经常CallBack在很多地方通过这个词。我想知道在技术上告诉我们意味着什么 - 以及我如何设法使用callbackin 应用程序。我需要一个指南来理解它并使用它。

采纳答案by CommonsWare

i want to know what it means, tell us technically

我想知道它是什么意思,从技术上告诉我们

http://en.wikipedia.org/wiki/Callback_%28computer_science%29

http://en.wikipedia.org/wiki/Callback_%28computer_science%29

"In object-oriented programming languages without function-valued arguments, such as Java, [callbacks] can be simulated by passing an abstract class or interface, of which the receiver will call one or more methods, while the calling end provides a concrete implementation. Such objects are effectively a bundle of callbacks, plus the data they need to manipulate. They are useful in implementing various design patterns such as Visitor, Observer, and Strategy."

“在没有函数值参数的面向对象编程语言中,如Java,[回调]可以通过传递一个抽象类或接口来模拟,接收者将调用其中的一个或多个方法,而调用端提供具体的实现. 这样的对象实际上是一组回调函数,加上它们需要操作的数据。它们在实现各种设计模式时很有用,例如访问者、观察者和策略。”

how i can manage the callback of the applications

我如何管理应用程序的回调

I have no idea what this means.

我不知道这是什么意思。

回答by JAL

Hmm. How about an example. You write a quicksort algorithm in C. The user who wants to use your algorithm must supply a compare method appropriate for what the user is sorting with your algorithm. The user must pass a function pointer to the user's compare method to your quicksort code. The quicksort code uses this address, the function pointer, to CALL BACK to the user's compare function. You provide a function prototype, no implementation, since you cannot possibly know how to determine the ordinality of what is being sorted. The user supplies the implementation of compare that makes sense for what the user is sorting. This implementation must match the function prototype. The function pointer is used by the quicksort alogorithm to reach back and touch the user's code.

唔。举个例子吧。您用 C 编写了一个快速排序算法。想要使用您的算法的用户必须提供一个适合用户使用您的算法进行排序的比较方法。用户必须将指向用户比较方法的函数指针传递给您的快速排序代码。快速排序代码使用这个地址,即函数指针,来回调用户的比较函数。您提供了一个函数原型,没有实现,因为您不可能知道如何确定正在排序的内容的序数。用户提供对用户正在排序的内容有意义的比较实现。此实现必须与函数原型相匹配。快速排序算法使用函数指针返回并触摸用户代码。

This is actually about polymorphism.

这实际上是关于多态性的。

In java, you can use an interface to do this. So for sorting, see the interface IComparer and IComparable.

在 java 中,您可以使用接口来执行此操作。所以对于排序,看接口IComparer和IComparable。

回答by huseyin

A Callable interface can be used to run a piece of code as Runnable does. However, Callable can return the result and can throw checked an exception.

Callable 接口可用于像 Runnable 一样运行一段代码。但是,Callable 可以返回结果并抛出已检查的异常。

For more detail. http://developer.android.com/reference/java/util/concurrent/Callable.html

欲知更多详情。 http://developer.android.com/reference/java/util/concurrent/Callable.html

By using Callable interfaces you can pass an argument as function I added a simple code snippet for understanding.

通过使用 Callable 接口,您可以将参数作为函数传递,我添加了一个简单的代码片段以供理解。

public class MainActivity<V> extends Activity {


    Callable<String> doLogin=null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        doLogin=new Callable<String>() {  //created but not called now.

            @Override
            public String call() throws Exception {

                //make some piece of code
                return "something"; //or false
            }
        };

        CheckSession checkSession=new CheckSession("sessionName");
        String sessionKey="";


        try {  //we are sending callable to the DAO or any class we want 
             sessionKey=checkSession.getSessionKey(doLogin);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}



public class CheckSession{


    String sessionName="";
    Callable<String> func=null;

    public CheckSession(String sessionName) {
        super();
        this.sessionName = sessionName;

    }

    public String getSessionKey(Callable<String> doLogin) throws Exception{

        func=doLogin;

        return (String) func.call();

    }

}