gpt4 book ai didi

android - 启动不同任务的最佳方式

转载 作者:行者123 更新时间:2023-11-30 04:46:28 27 4
gpt4 key购买 nike

我正在为盲人开发一个应用程序。我必须一直使用 TextToSpeech 模块、GPS 和网络连接。

我需要执行这样的查询:咨询 GPS,执行 JSON 调用并调用 TextToSpeech(TTS) 模块。

我想知道处理与 UI 主线程通信的不同任务的最佳方法。到目前为止我已经看到:处理程序对象和 AsyncTask 类。

我必须按顺序启动每个任务,所以我想在从网络检索数据后调用 TTS。所以我使用了“mHandler.post(Runnable)”并在该 runnable 中调用另一个,依此类推。

但是我发现建议使用 AsynTask 类。但在这种情况下,我认为我必须为每个任务实现一个不同的类,而我不知道这些任务是否会按顺序执行。像这样的东西:

AsyntaskClass1 at1;
AsyntaskClass2 at2;
AsyntaskClass3 at3;

at1.execute();
at2.execute();
at3.execute();

这些任务会按顺序执行吗?导致 TTS 模块必须等待网络任务完成...

谢谢你的帮助,

BR.大卫。

最佳答案

你可以用一个 AsyncTask 类做所有事情:

private class AsyncTask1 extends AsyncTask<Object, Void, Object> {

@Override
protected String doInBackground(Object... args) {
// Get a result from the GPS.

// Make a network call

// Communicate with the TTS
}

@Override
protected void onPostExecute(Object result) {
// Return a result to the main thread
// You are only allowed to touch the UI here
}

}

关于android - 启动不同任务的最佳方式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4849312/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com