2012-03-18 13 views
1

プログラムの仕事はインターネットからデータを読んでそれを処理することです。Androidがスレッドを終了して通知する方法をお待ちしていますか?

メインクラス内でAsyncTaskを使用して、インターネットからデータを読み込みます。AsyncTaskはAsyncTaskがWebページを読み取る前にメインクラスが終了するため別のスレッドとして実行されます。次に、メインクラスがAsyncTaskスレッドが完了するまで待ちます(すなわち、インターネットからデータを読み込み)、メインクラスのスレッドを再開します。

注:

  1. 私はonPostExcecute()メソッドでそのプロセスを行うにはしたくありません。
  2. 私は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 

     } 
    } 
    } 
+0

[AsyncTask.get()](http://developer.android.com/reference/android/os/AsyncTask.html)、しかし、あなたはおそらくANR例外が発生します。 – yorkw

+0

"私はonPostExcecute()メソッドでその処理をしたくありません。" - なぜ?それはまさに 'onPostExecute(...)'が設計されたものです。 – Squonk

答えて

0

です。できるだけ多くのことをしてください。 UIを更新するときにのみ、メインスレッドに戻ります。 onPostExecute()に何も欲しくない場合は、Intentのアプリを送信することができます。

1

get.wait(); // wait until get calls notify() 

this.wait(); 

を交換し、あなたが望む

this.notify(); // wakeup someone who waiting for me 

どこかを呼び出します。

関連する問題