这是我经常使用Asyntask时遇到的一个问题。联系UI线程的方法是调用publishProgress()&此方法接受只有一个参数类型的数组。如何让Asynctask以不同的方式更新UI
在后台运行,这样做复杂的计算可能需要更新使用不同类型对象在不同的点UI线程。
让我说明一个例子:
...do some processing...
// Send UI thread the integer values of the width & height of the image
...do some more processing...
// Send UI thread a String with custom message.
...do some more processing...
// Send UI thread an instance of MyObject so it can extract & display certain values
...do some cleanup job & finish...
然而,onProgressUpdate()接受仅一种类型的阵列。
那么,我做出一个包罗万象的对象类型?我怎么知道如何下调它,因为这个方法可以从第1,2或3行调用,所以这是什么时间?
当然必须有一个很好的方法来实现这一目标?
编辑:我真的很想看看,如果在Android中是可能的,将是界定publishProgress1(用户定义args1),publishProgress2(用户定义args2),publishProgress3的一些方法(用户定义的args3) ...
我相信,你所要求的类似的变体结构http://en.wikipedia.org/wiki/Variant_type – JAL 2011-04-15 01:52:50
@JAL:感谢您分享有趣的链接。使用运行时类型确定的Variant_type通过运行时向下转换提醒了我非常多的Java对象类型。我真的很希望看到,如果它可能在Android中,将会是定义* publishProgress1(用户定义的args1)*,* publishProgress2(用户定义的args2)*,* publishProgress3(用户定义的args3) * ... – OceanBlue 2011-04-15 14:27:14
@OceanBlue VisualBasisVARIANT是一个旧的解决方案,但我宁愿看到更安全的解决方案,可能会解决使用消息和单个处理程序来启动三个顺序线程的问题。因此,启动返回类型A的线程返回消息(0),处理程序中的陷阱消息,启动带消息的线程(1)返回类型B,带消息(2)的陷阱和启动线程等。 – JAL 2011-04-16 00:25:35