8

私はAndroid開発に不慣れです。私はSwingとSWTに数年間取り組んできました。SwingとSWTはどちらも、UIスレッドの同期と非同期でコードを実行するための戦略を持っています。典型的な使用法は、時間をかけて1つのスレッドでスタッフを消費し、結果をUIスレッド非同期で表示することです。

だから私の質問は、Androidに同様の戦略がありますか?これが私のコードです。実行可能なパラメーターは、時間のかかるコードです。このメソッドは、実行中に待機ダイアログを表示し、終了後にトーストを表示することを期待します。ただし、トーストはUIスレッドで表示する必要があります。それで、それをどのように行うのですか?

    public static void showWaitingDialog(final Activity parent, final Runnable runnable, String msg) {

    if (StringUtils.isEmpty(msg)) {
        msg = "processing...";
    }

    final ProgressDialog waitingDialog = ProgressDialog.show(parent, "Please Wait...", msg, true);

    // execute in a new thread instead of UI thread
    ThreadPoolUtil.execute(new Runnable() {

        public void run() {
            try {
                // some time-consume operation
                runnable.run();
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                waitingDialog.dismiss();
            }
            // TODO: How to display a Toast message here? execute some code in UI Thread.

        }

    });

}

そして、Android UIシステムについていくつかの言葉はありますか?スレッドセーフ、スレッドがどのように連携するかなどです。どうもありがとう!

4

6 に答える 6

7

このようにAsyncTaskを使用できます。

AsyncTaskを呼び出すには

new getAsynctask().execute("");

これが結果を得るためのクラスです。

class getAsynctask extends AsyncTask<String, Long, Integer> {

    protected void onPreExecute() {
        super.onPreExecute();
        loading = ProgressDialog.show(Pass.this, null, "Please wait...");
    }
    protected Integer doInBackground(String... params) {
        try {
            // do your coding
            return null;
        } catch (Exception e) {
            return null;
        }

    }

    protected void onPostExecute(Integer result) {
        super.onPostExecute(result);
        try {
            if (loading != null && loading.isShowing())
                loading.dismiss();
        } catch (Throwable t) {
            Log.v("this is praki", "loading.dismiss() problem", t);
        }
    }
}
于 2012-02-07T09:01:37.030 に答える
7

それを行うにはいくつかの方法があります、

  • AsyncTask- _

AsyncTaskを使用すると、UIスレッドを適切かつ簡単に使用できます。このクラスを使用すると、スレッドやハンドラーを操作しなくても、バックグラウンド操作を実行してUIスレッドで結果を公開できます。Example for using AsyncTask

  • サービス-

サービスは、ユーザーと対話せずに長時間実行される操作を実行したい、または他のアプリケーションが使用する機能を提供したいというアプリケーションの要望を表すアプリケーションコンポーネントです。Example for Using Service.

  • IntentService- _

IntentServiceは、非同期要求(Intentsとして表される)をオンデマンドで処理するサービスの基本クラスです。クライアントはstartService(Intent)呼び出しを介してリクエストを送信します。サービスは必要に応じて開始され、ワー​​カースレッドを使用して各インテントを順番に処理し、作業がなくなると自動的に停止します。Example for using IntentService.

于 2012-02-07T09:01:48.753 に答える
2

UIスレッドではない個別のスレッドで作業している場合は常に、ハンドラーを使用するのが最善の方法です。スレッドからユーザーを親密にしたいときはいつでも、進行状況を想定してから、ハンドラーにメッセージを送信してください。ハンドラー内では、メッセージを処理し、UIで何かを変更するためのコードスニペットを記述できます。これはAndroidに推奨される方法です。これらのlink1link2 、 link3を参照してください

于 2012-02-07T08:57:59.950 に答える
2

このAsynTaskをアクティビティの内部クラスとして使用します。バックグラウンドで実行し、実行したい時間のかかるタスクを実行してから、実行後にテキストメッセージを表示できます。あなたの主な活動からこれを呼んでください

initTask = new InitTask();
initTask.execute(this);


 protected class InitTask extends AsyncTask<Context, Integer, String> {     
        @Override
        protected String doInBackground(Context... params) {
            // Do the time comsuming task here 
            return "COMPLETE!";
        }

        // -- gets called just before thread begins
        @Override
        protected void onPreExecute() {         
            super.onPreExecute();

        }

        // -- called from the publish progress
        // -- notice that the datatype of the second param gets passed to this
        // method
        @Override
        protected void onProgressUpdate(Integer... values) {

        }

        // -- called if the cancel button is pressed
        @Override
        protected void onCancelled() {
            super.onCancelled();            
        }

        // -- called as soon as doInBackground method completes
        // -- notice that the third param gets passed to this method
        @Override
        protected void onPostExecute(String result) {
            super.onPostExecute(result);
            // Show the toast message here
        }
    }
于 2012-02-07T08:59:05.783 に答える
2

ハンドラーを使用します。

static final int SHOW_TOAST = 0;
public static void showWaitingDialog(final Activity parent, final Runnable runnable, String msg) {

    if (StringUtils.isEmpty(msg)) {
        msg = "processing...";
    }

    final ProgressDialog waitingDialog = ProgressDialog.show(parent, "Please Wait...", msg, true);

    // execute in a new thread instead of UI thread
    ThreadPoolUtil.execute(new Runnable() {

        public void run() {
            try {
                // some time-consume operation
                runnable.run();
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                waitingDialog.dismiss();
            }
           handler.sendMessage(handler.obtainMessage(SHOW_TOAST));

        }

    });

}

public Handler handler = new Handler() {
    @Override
    public void handleMessage(Message msg) {
        switch (msg.what) {
            case SHOW_TOAST:
                //Toast here
                break;
        }
    }
};
于 2012-02-07T08:59:52.370 に答える
1

android開発者リソースの痛みのないスレッドの記事は、特定のSDKバージョンに応じてさまざまな代替手段を提供します。

于 2012-02-07T08:58:51.277 に答える