我想在两个活动之间添加进度条

发布于 2024-10-29 10:05:30 字数 118 浏览 2 评论 0原文

在android应用程序中,当从第一个活动调用第二个活动时,我想在两个活动之间放置进度条,以便该应用程序看起来处于运行状态,并且我认为这样的事情是通过线程完成的,如果有人知道这一点,请帮助我..

提前致谢。

In android application when second activity is being called from first one i want to put progressbar between two activities, so that application seems that it is in running state, and i think such thing is done through thread,help me if anyone knows this..

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

羁客 2024-11-05 10:05:30

Kartik 是对的,但是让我添加更多内容以显示活动内的进度条。

当您想要显示进度条并在后台获取数据时,您应该在第二个活动中实现 AsyncTask。

下面是一个示例:

 private class performBackgroundTask extends AsyncTask <Void, Void, Void>  
      {
               private ProgressDialog Dialog = new ProgressDialog(ClassName.this);

               protected void onPreExecute()
               {
                   Dialog.setMessage("Please wait...");
                   Dialog.show();
               }

               protected void onPostExecute(Void unused)    
               {
                   try
                   {
                       if(Dialog.isShowing())
                       {
                           Dialog.dismiss();
                       }
                               // do your Display and data setting operation here
                   }
                   catch(Exception e)
                   {

                   }

            @Override
        protected Void doInBackground(Void... params) 
            {
           // Do your background data fetching here 
               return null;   
        }
      }

添加此 PerformBackgroundTask 类后,无论何时要执行,都需要调用 execute() 来执行相同的操作。

Kartik is right, But let me add more to show Progressbar inside activity.

As you want to show Progressbar along with fetching data in background,you should implement AsyncTask in your second activity.

Here is an example:

 private class performBackgroundTask extends AsyncTask <Void, Void, Void>  
      {
               private ProgressDialog Dialog = new ProgressDialog(ClassName.this);

               protected void onPreExecute()
               {
                   Dialog.setMessage("Please wait...");
                   Dialog.show();
               }

               protected void onPostExecute(Void unused)    
               {
                   try
                   {
                       if(Dialog.isShowing())
                       {
                           Dialog.dismiss();
                       }
                               // do your Display and data setting operation here
                   }
                   catch(Exception e)
                   {

                   }

            @Override
        protected Void doInBackground(Void... params) 
            {
           // Do your background data fetching here 
               return null;   
        }
      }

After adding this performBackgroundTask class, you need to call execute() for the same whenever you want to execute.

风吹短裙飘 2024-11-05 10:05:30

您必须使用 AsyncTask 或 Thread 来获取所选数据的详细信息。通过使用它,您可以解决在两个活动之间显示进度条的问题。

AsyncTask 基本示例: AsyncTask

private class LongOperation extends AsyncTask<String, Void, String> {

@Override
protected String doInBackground(String... params) {
    // perform long running operation operation
    return null;
}

/* (non-Javadoc)
 * @see android.os.AsyncTask#onPostExecute(java.lang.Object)
 */
@Override
protected void onPostExecute(String result) {
    // execution of result of Long time consuming operation
}

/* (non-Javadoc)
 * @see android.os.AsyncTask#onPreExecute()
 */
@Override
protected void onPreExecute() {
// Things to be done before execution of long running operation. For example showing ProgessDialog
}

/* (non-Javadoc)
 * @see android.os.AsyncTask#onProgressUpdate(Progress[])
 */
@Override
protected void onProgressUpdate(Void... values) {
  // Things to be done while execution of long running operation is in progress. For example updating ProgessDialog
 }
}

You must use AsyncTask or Thread for getting the details of selected data. By using this you can solve your problem of displaying progressbar between two activities..

AsyncTask basic Example : AsyncTask

private class LongOperation extends AsyncTask<String, Void, String> {

@Override
protected String doInBackground(String... params) {
    // perform long running operation operation
    return null;
}

/* (non-Javadoc)
 * @see android.os.AsyncTask#onPostExecute(java.lang.Object)
 */
@Override
protected void onPostExecute(String result) {
    // execution of result of Long time consuming operation
}

/* (non-Javadoc)
 * @see android.os.AsyncTask#onPreExecute()
 */
@Override
protected void onPreExecute() {
// Things to be done before execution of long running operation. For example showing ProgessDialog
}

/* (non-Javadoc)
 * @see android.os.AsyncTask#onProgressUpdate(Progress[])
 */
@Override
protected void onProgressUpdate(Void... values) {
  // Things to be done while execution of long running operation is in progress. For example updating ProgessDialog
 }
}
恬淡成诗 2024-11-05 10:05:30

在android中只有主线程,也称为UI线程,可以进行UI操作。因此,您无法显示新线程的进度条。
我认为问题的解决方案可能会显示您的第二个活动(可能为空),显示 onCreate() 的进度对话框,然后在活动中执行任何繁重的工作。
希望这有帮助。

In android only the main thread, also called the UI thread, can to UI operations. So, you cannot show a progress bar from a new thread.
I think the solution to your problem may to show your second activity (possibly empty), show progress dialog from onCreate() then do any heavy lifting in the activity.
Hope this helps.

满天都是小星星 2024-11-05 10:05:30

只需在进行计算的活动中创建 ProgressDialog 即可。

ProgressDialog progressDialog = ProgressDialog.show(context, title, text);

当你的任务完成后,关闭它,

if(progressDialog != null && progressDialog.isShowing())
      progressDialog.dismiss();

但一定要从 UI 线程中显示和关闭对话框。 AsyncTask 可能是这种情况的最佳解决方案

Just make ProgressDialog in the activity that make your calculations.

ProgressDialog progressDialog = ProgressDialog.show(context, title, text);

and when your task is done close it

if(progressDialog != null && progressDialog.isShowing())
      progressDialog.dismiss();

But be sure to show and dismiss dialog from UI thread. AsyncTask is probably the best sollution for such case

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文