我添加了AsyncTask来将网络操作卸载到后台线程。我需要确保UI操作位于UI线程上,因此我想在Activity中使用runOnUiThread()。
啊! 没有!!!除以外, 每种 方法都
AsyncTask可以运行。您也可以通过从进行调用来进行网络操作并更新in 或in 。
UIThread``doInBackground()``doInBackground()``UI``onPostExecute()``onProgressUpdate()``publishProgress()``doInBackground()
请勿
runonUiThread()与一起使用
AsyncTask。至少对于我来说,没有理由使用它,
AsyncTask因为它具有已经在上运行的方法
UIThread。我从未见过它会做任何事情,但会带来麻烦。
您可以致电
publishProgress()您
loop,并更新您
TextView在
onProgressUpdate()或添加值的
ArrayList中和更新
onProgressUpdate()。
请多次阅读文档。
AsyncTask刚开始时有点棘手,但是一旦您了解了它的功能,那么它可能就是一件美丽的事情。
编辑
创建您的实例
AsyncTask并将其传递
Activity Context给它
Scan myScan = new scan(this); // pass the context to the constructormyScan.execute();
然后在中创建一个构造函数,
AsyncTask并接受
Context。
public class scan extends AsyncTask<String, Integer, String> { public Object WIFI_SERVICE; public Context context; // Context variable public scan(Context c) // constructor to take Context { context = c; // intialize your Context variable }现在使用该变量
@Override protected String doInBackground(String... params) { wifiApManager = new WifiApManager(context); // use the variable here return null; }另一个编辑
class scan extends AsyncTask<String, Void, Void> { ArrayList<ClientScanResult> clients; Context context;...then initialize your `clients` in `doInBackground()`clients = wifiApManager.getClientList(false);改变
onPostExecute()为不接受任何东西
protected void onPostExecute(Void result){然后将更新其中的代码
TextView放入其中。



