English 中文(简体)
显示对话框的 Async Task
原标题:AsyncTask for showing dialog
  • 时间:2012-05-23 14:49:23
  •  标签:
  • android

执行任务时, 我如何使用 AsyncTashsk 类显示对话框?

class TestAsynTask extends AsyncTask<Void, Void, Void> {

    @Override
    protected void onPreExecute() {
        ProgressDialog.show(???, null, null);
        super.onPreExecute();
    }
    @Override
    protected Void doInBackground(Void... params) {
        // TODO Auto-generated method stub
        return null;
    }

}
最佳回答

假设TestAsynctask Accessity 的内层, 那么您就可以使用活动名称 . this 来获取上下文。 如果TestAsynctask不是一个内层, 那么您会想要将您的 Assynctask 实例传递给构建者, 以便您能够将它作为 Context 提供给 < a href=> http://developer.android.com/android/app/app/regationDialog.html#show% 28android. content. Context,%20java.lang.Charsequecce,% 20java.lang.Charconcent%29" rel="\nofol_code> areDrevicol> a Revicdoog. show () <

以下是内类方法的一个例子:

class MyActivity extends Activity {
    //Activity Lifecycle methods

    class TestAsynTask extends AsyncTask<Void, Void, Void> {
        ProgressDialog dialog;

        @Override
        protected void onPreExecute() {
            dialog = ProgressDialog.show(MyActivity.this, "title", "message");
            super.onPreExecute();
        }

        @Override
        protected Void doInBackground(Void... params) {
            //very long computation...
            return null;
        }

        @Override
        protected void onPostExecute(Void void) {
           dialog.cancel();
        }
    }
}

您会注意到, 您应该将 < code> ProgressDialog 保存到 on Preecute () 方法中的一个实例变量上, 并在 on PostExecute () 方法中将 < code> 调用到该变量上, 并在 on PostExecut () 方法中调用 < code> < cancel () 。

另一种方法看起来相似:

class MyActivity extends Activity {
    //Activity Lifecycle methods


}

class TestAsynTask extends AsyncTask<Void, Void, Void> {
    ProgressDialog dialog;
    MyActivity activity;        

    TaskAsynTask(MyActivity activity) {
        this.activity = activity;
    }

    @Override
    protected void onPreExecute() {
        dialog = ProgressDialog.show(activity, "title", "message");
        super.onPreExecute();
    }

    @Override
    protected Void doInBackground(Void... params) {
        //very long computation...
        return null;
    }

    @Override
    protected void onPostExecute(Void void) {
        dialog.cancel();
    }
}
问题回答

暂无回答




相关问题
Android - ListView fling gesture triggers context menu

I m relatively new to Android development. I m developing an app with a ListView. I ve followed the info in #1338475 and have my app recognizing the fling gesture, but after the gesture is complete, ...

AsyncTask and error handling on Android

I m converting my code from using Handler to AsyncTask. The latter is great at what it does - asynchronous updates and handling of results in the main UI thread. What s unclear to me is how to handle ...

Android intent filter for a particular file extension?

I want to be able to download a file with a particular extension from the net, and have it passed to my application to deal with it, but I haven t been able to figure out the intent filter. The ...

Android & Web: What is the equivalent style for the web?

I am quite impressed by the workflow I follow when developing Android applications: Define a layout in an xml file and then write all the code in a code-behind style. Is there an equivalent style for ...

TiledLayer equivalent in Android [duplicate]

To draw landscapes, backgrounds with patterns etc, we used TiledLayer in J2ME. Is there an android counterpart for that. Does android provide an option to set such tiled patterns in the layout XML?

Using Repo with Msysgit

When following the Android Open Source Project instructions on installing repo for use with Git, after running the repo init command, I run into this error: /c/Users/Andrew Rabon/bin/repo: line ...

Android "single top" launch mode and onNewIntent method

I read in the Android documentation that by setting my Activity s launchMode property to singleTop OR by adding the FLAG_ACTIVITY_SINGLE_TOP flag to my Intent, that calling startActivity(intent) would ...

From Web Development to Android Development

I have pretty good skills in PHP , Mysql and Javascript for a junior developer. If I wanted to try my hand as Android Development do you think I might find it tough ? Also what new languages would I ...

热门标签