Android如何等待线程完成并通知?
该程序的任务是从互联网上读取数据并进行处理 我在主类中使用AsyncTask从internet读取数据。但是AsyncTask将作为单独的线程运行,因为该主类在AsyncTask读取网页之前完成。现在我希望主类等待AsyncTask线程完成(即从internet读取数据)并恢复主类线程 注:Android如何等待线程完成并通知?,android,multithreading,Android,Multithreading,该程序的任务是从互联网上读取数据并进行处理 我在主类中使用AsyncTask从internet读取数据。但是AsyncTask将作为单独的线程运行,因为该主类在AsyncTask读取网页之前完成。现在我希望主类等待AsyncTask线程完成(即从internet读取数据)并恢复主类线程 注: 我不想在onPostExcecute()方法中执行该过程 我不想使用sleep() 这是密码 public class MyApp extends Activity { String da
public class MyApp extends Activity {
String data[] = null;
super.onCreate(savedInstanceState);
setContentView(R.layout.m1_pm_material_requisitions_main);
tv = (TextView)findViewById(R.id.test_textview);
GetData get = new GetData();
get.execute(new String[]{"http://192.168.92.171/erp/index.php"});
try {
this.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//i want the program to wait here till the above task is done and proceed further
/* Here the code to process the data in String[] data;....
..........
.......... */
private class GetData extends AsyncTask<String, Void, String> {
protected String doInBackground(String... params) {
// code to read webpage from internet
//im using HttpGet to read a webpage
//now the content of webpage is stored in String[] data
}
}
}
公共类MyApp扩展活动{
字符串数据[]=null;
super.onCreate(savedInstanceState);
setContentView(R.layout.m1_pm_物料_请购单_main);
tv=(TextView)findViewById(R.id.test\u TextView);
GetData get=新建GetData();
get.execute(新字符串[]{”http://192.168.92.171/erp/index.php"});
试一试{
这个。等等();
}捕捉(中断异常e){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
//我希望程序在这里等待,直到上述任务完成并继续
/*这里是处理字符串[]data;中数据的代码;。。。。
..........
.......... */
私有类GetData扩展异步任务{
受保护的字符串doInBackground(字符串…参数){
//从internet读取网页的代码
//我正在使用HttpGet阅读网页
//现在网页的内容存储在String[]数据中
}
}
}
将处理字符串的代码也放入异步任务中。在那里尽你所能。只有在更新UI时才返回到主线程。如果你真的不想在onPostExecute()
中使用任何东西,我想你可以给你的应用程序发送Intent
把处理字符串的代码也放在你的异步任务中。在那里尽你所能。只有在更新UI时才返回到主线程。如果你真的不想在onPostExecute()
中使用任何东西,我想你可以给你的应用发送一个Intent
替换
this.wait();
与
打电话
this.notify(); // wakeup someone who waiting for me
你想要的地方。替换
this.wait();
与
打电话
this.notify(); // wakeup someone who waiting for me
您想要的地方。但是,您可能会得到一个异常。“我不想在onPostExcecute()方法中执行该过程。”-为什么?这正是onPostExecute(…)
的设计目的。然而,您可能会遇到一个异常。“我不想在onPostExcecute()方法中执行该过程。”-为什么?这正是onPostExecute(…)
的设计目的。