で。
これの使い道っていうと、重い処理を別スレッドで動かして、その間に表示。
処理が終了したら閉じる。 ってなる。
なので、重い処理の開始前にshow()して、処理が終わったらdismiss()を呼ぶ。
でも、その「重い処理」ってーのは、別スレッドなので、別スレッドからshow()すると、RuntimeExceptionだしてくれるんだよね。コイツ。(Toastでも同じ現象発生)
んで、Handlerを使えとかなんとか言われるわけなんだけど。
なんとかクラスでまとめられないかなーと思って、作ってみました。
- package mosaic.dividebyzero.net;
- import android.app.ProgressDialog;
- import android.content.Context;
- import android.os.Handler;
- class MyProgressDialog implements Runnable {
- private String title, message;
- private static ProgressDialog progressDialog;
- private static Handler handle = new Handler();
- public static void init(Context context) {
- if (progressDialog == null) progressDialog = new ProgressDialog(context);
- }
- public static void createDialog(String title, String message) {
- if (progressDialog == null)
- throw new RuntimeException("MyProgressDialog is not initialize\nplease call init Method.");
- handle.post(new MyProgressDialog(title, message));
- }
- public static void close() {
- if (progressDialog != null) progressDialog.dismiss();
- }
- public MyProgressDialog(String title, String message) {
- this.title = title;
- this.message = message;
- }
- @Override
- public void run() {
- // TODO 自動生成されたメソッド・スタブ
- progressDialog.setTitle(title);
- progressDialog.setMessage(message);
- progressDialog.setIndeterminate(false);
- progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
- progressDialog.setCancelable(true);
- progressDialog.show();
- }
- }
肝は、createDialogより前にinitを呼ぶということ。
さらに言えば、initはviewのスレッドから呼ぶ必要があるっぽいです(てか、無きゃinitとcreateに分ける必要が無い)
正直、ここらへんは行き当たりばったりというか、やってみたら出来た。というか。
理論に基づいた作りじゃないのでなにか間違ってるかもですが。とりあえず、今のところ問題無しです。
0 件のコメント:
コメントを投稿