Android 偏好活动

发布于 2024-11-08 01:39:14 字数 403 浏览 4 评论 0原文

我有一个偏好活动,并且希望如果单击其中一个项目,它将启动后台工作并在前台显示一个漂亮的进度条,直到后台任务完成。怎么办???

编写的代码:

 public boolean onPreferenceClick(Preference preference) {
  showProgressDialog();
  new Thread(new Runnable() {
   public void run() {
    doSomething();
    hideProgressDialog();
   }       //Runnable.run()
  }).start();
  return false;
 }
});

但是上面的代码没有显示进度对话框。并出现ANR错误。

谢谢。

I have a preference activity and want that if one of its items is clicked it will start a background work and show a nice progress bar in foreground until the background task finishes. how to do it???

Code writtenis:

 public boolean onPreferenceClick(Preference preference) {
  showProgressDialog();
  new Thread(new Runnable() {
   public void run() {
    doSomething();
    hideProgressDialog();
   }       //Runnable.run()
  }).start();
  return false;
 }
});

But the above code is not showing progress dialog. and ANR error occurs.

Thanks.

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

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

发布评论

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

评论(2

碍人泪离人颜 2024-11-15 01:39:14

在您的活动类中添加以下代码:

// Need handler for callbacks to UI Threads
    // For background operations
    final Handler mHandler = new Handler();

    // Create Runnable for posting results
    final Runnable mUpdateDone = new Runnable() {
        public void run() {
            progDialog.hide();      
            // Do your task here what you want after the background task is finished.           
        }
    };

在 onPreferenceClick 中编写以下代码:

progDialog = ProgressDialog.show(AddPhoto.this, "", "Uploading Photo...");
            new Thread() {
                public void run() {
                    // Start your task here.....                                                        
                    mHandler.post(mUpdateDone);
                }
                }.start();

如果不适合您,请告诉我!

Add following code in your activity class:

// Need handler for callbacks to UI Threads
    // For background operations
    final Handler mHandler = new Handler();

    // Create Runnable for posting results
    final Runnable mUpdateDone = new Runnable() {
        public void run() {
            progDialog.hide();      
            // Do your task here what you want after the background task is finished.           
        }
    };

Write following code in onPreferenceClick:

progDialog = ProgressDialog.show(AddPhoto.this, "", "Uploading Photo...");
            new Thread() {
                public void run() {
                    // Start your task here.....                                                        
                    mHandler.post(mUpdateDone);
                }
                }.start();

Let me know if doesn't work for you!

戒ㄋ 2024-11-15 01:39:14

我还没有实现进度对话框(计划今天晚些时候),但是这个 示例 看起来不错。我打算自己用它。我注意到它做了很多你的代码没有做的事情。

I have not implemented a progress dialog yet (plan to later today), but this example looks like a good one. I plan to use it myself. I note that it does a number of things that your code does not.

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