簡體   English   中英

我希望Android進度對話框一直顯示在屏幕上,直到新活動的功能完成但不起作用

[英]I want Android progress dialog to stay on screen until function on new activity is complete but doesn't work

在我的Android應用中,一種形式具有一個按鈕,單擊該按鈕可打開另一種形式。 新表格執行的活動可能需要一段時間。 我希望第一個表單保持打開狀態,並讓進度對話框在這些活動結束時保持旋轉狀態。

我在下面嘗試過此操作,但它行不通。 進度對話框將完成,並打開下一個窗口(在完成對新表單的描述之前)

在下面的代碼SecondForm中-

子例程“ Calculations ”是需要一段時間才能完成的

碼:

主要活動:

final ProgressDialog ringProgressDialog = new ProgressDialog(
            MainActivity.this);
    ringProgressDialog.setTitle("Loading");
    ringProgressDialog.show();
    ringProgressDialog.setCancelable(false);
    new Thread(new Runnable() {

    @Override
    public void run() {
        try {

            runOnUiThread(new Runnable() {

                @Override
                public void run() {

                        Intent intent = new Intent(
                                getApplicationContext(),
                                SecondForm.class);
                        startActivity(intent);


                }
            });
        } catch (Exception e) {

        }
        ringProgressDialog.dismiss();

    }
}).start();

SecondForm:

 public class CategoryTabs extends Fragment {

        static Context mContext;
        View rootView;

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
                gotstatdata = false;
            rootView = inflater.inflate(R.layout.fragment_abc, container, false);

            mContext = rootView.getContext();

            new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        ((Activity) mContext).runOnUiThread(new Runnable() {

                            @Override
                            public void run() {
                                gotstatdata = false;
                                Calculations(128);
                                gotstatdata = true;
                            }
                        });
                    } catch (Exception e) {
                    }
                }
            }).start();

使用ringProgressDialog.dismiss();運行對話框后,您將立即將其ringProgressDialog.dismiss(); 該行應刪除,完成關閉進度對話框后,您應該執行類似發送廣播的操作。

看起來您可能也只想要一個后台線程而不是第二個Activity因為不需要用戶交互。

查看AsyncTask將是您從后台線程開始的最簡單方法,它的“ onPostExecute”方法可讓您關閉對話框。

編輯

您想要的基本結構是添加以下內容

new AsyncTask<Void, Void, Void>() {

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            //TODO: show your dialog from here
        }

        @Override
        protected Void doInBackground(Void... params) {
            //TODO: call Calculations(128); from here
            //Calculations(128); should live within this async task instead of a new activity
            return null;
        }

        @Override
        protected void onPostExecute(Void aVoid) {
            super.onPostExecute(aVoid);
            //TODO: call dismiss on your dialog from here
        }
    }.execute();

而不是new Thread(new Runnable() {

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM