プログラムの仕事はインターネットからデータを読んでそれを処理することです。Androidがスレッドを終了して通知する方法をお待ちしていますか?
メインクラス内でAsyncTaskを使用して、インターネットからデータを読み込みます。AsyncTaskはAsyncTaskがWebページを読み取る前にメインクラスが終了するため別のスレッドとして実行されます。次に、メインクラスがAsyncTaskスレッドが完了するまで待ちます(すなわち、インターネットからデータを読み込み)、メインクラスのスレッドを再開します。
注:
- 私はonPostExcecute()メソッドでそのプロセスを行うにはしたくありません。
- 私はsleep()を使いたくありません。ここで
あまりにもあなたAsyncTask
内の文字列を処理するためのコードを入れてコード
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
}
}
}
[AsyncTask.get()](http://developer.android.com/reference/android/os/AsyncTask.html)、しかし、あなたはおそらくANR例外が発生します。 – yorkw
"私はonPostExcecute()メソッドでその処理をしたくありません。" - なぜ?それはまさに 'onPostExecute(...)'が設計されたものです。 – Squonk