等功能到结束执行和使用的结果 [英] Waiting for a function to finish execution and using the results

查看:119
本文介绍了等功能到结束执行和使用的结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面的情景

我有有一个按钮和TextView的活动(A)。我有另一个类(B)和用于执行各种功能的方法。建立B类的一个实例后,它的一个公共方法是从点击按钮时调用。该方法需要一段时间来执行(它调用另一个耗时类中的私有方法)并返回B类的私有成员

I have an activity(A) which has a button and textview. I have another class(B) with methods for performing various functions. After creating an instance of class B, one of it's public method is called from A when the button is clicked. The method takes a while to execute(it invokes another time consuming private methods in the class) and returns the value of one of the private members of class B.

麻烦的是,该方法返回构件的初始值,而不是计算后的值。有没有办法强制的功能等待一段时间,并返回计算成员的值?

The trouble is that the method returns the initial value of the member and not the values after computation. Is there way to force the function to wait for some time and return the value of member of computation?

public String getItem(){
        startFunction(); //Time consuming Function
        generateItem(); //Function which uses results of startFunction() to generate item and set values to mItem
        return mItem; //mItem is the private member of class B
    }

中的值总是返回在构造函数中设置MITEM即值的默认值。由startFunction(无线扫描)所花费的时间是任意的。任何帮助将是非常美联社preciated。

The value returned always is the default value of mItem i.e value set in the constructor. The time taken by startFunction(WiFi Scanning) is arbitrary. Any help would be much appreciated.

推荐答案

在创建B类的实例,确保它采用上下文作为其构造函数的参数。然后你可以使用的AsyncTask (假设你知道)试试。再放入消费功能的时间doInBackground()并在斗智斗勇其值函数postExecute()

When you create an instance of class B make sure it takes Context as an argument in its constructor.. Then you can try by using AsyncTask (Assuming you know it). Then put the time consuming function in doInBackground() and the function which wits for its values in postExecute()

这篇关于等功能到结束执行和使用的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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