Android:Singleton 类中的后台线程

发布于 2024-12-17 18:34:25 字数 2220 浏览 2 评论 0原文

我有 Singleton 类,它保存所有数据(某些站点的访问者),并且数据由服务更新。我有一个接口,它是由(列表)活动(显示访问者)实现的,所以现在当我更新数据时,我只需调用接口方法,以便列表活动可以刷新它。

现在我需要维护访问者在现场的时间(在客户端)。我想在 Singleton 类中创建一个线程,该线程每秒运行一个循环,但我无法使用处理程序在主线程上调用任何方法。 这是线程的代码:

void startHeavyDutyStuff() {
          Thread t = new Thread() {
              public void run() {
                  try {
                      while(true) {
                          sleep(1000);
                          ArrayList<VisitorMC> data =  SharedAppManager.appManager().visitorsData;

                          boolean doReload = false;
                          for (VisitorMC item: data) {
                              item.secsOnSite++;
                              if(item.secsOnSite == 60) {
                                  item.secsOnSite = 0;
                                  item.minsOnSite++;
                                  doReload = true;  
                              }
                          }
                          if(doReload) {
                              messageHandler.sendEmptyMessage(1);
                          } else {
                              messageHandler.sendEmptyMessage(0);
                          }
                      }
                  } catch (InterruptedException e) {
                      e.printStackTrace();
                  }
              }
          };
          t.start();
      }

这是我在主线程上创建处理程序的代码(在 Singleton 类中):

private SharedAppManager() {

            //Initialization of the data.

        Looper.prepare();
        messageHandler = new Handler() {
              public void handleMessage(Message msg) {  
                  switch(msg.what) {
                  case 0:
                      Log.d("THREAD", "after every second");
                      break;
                  case 1:
                      if(visitorsDelegate != null) {
                            visitorsDelegate.updateVisitorsTime();
                        }
                      break;
                  default:
                  }
              }
          };
        startHeavyDutyStuff();
    }

我在这里做错了什么?

编辑: 我需要每秒更新 UI,这就是为什么我运行一个单独的线程,它可以更改数据并调用 UI 上的更新。

I have Singleton class which holds all the data (visitors of some site) and the data is updated by a service. I have an interface, which is implemented by an (list) activity (which shows visitors), so now as I get the data updated, I simply call the interface method so that the list activity can refresh it.

Now I need to maintain the time visitors are on site (at client end). I want to make a thread in Singleton class which will run a loop after every second, but I am not able to call any method on Main thread, using Handlers.
Here's the code of thread:

void startHeavyDutyStuff() {
          Thread t = new Thread() {
              public void run() {
                  try {
                      while(true) {
                          sleep(1000);
                          ArrayList<VisitorMC> data =  SharedAppManager.appManager().visitorsData;

                          boolean doReload = false;
                          for (VisitorMC item: data) {
                              item.secsOnSite++;
                              if(item.secsOnSite == 60) {
                                  item.secsOnSite = 0;
                                  item.minsOnSite++;
                                  doReload = true;  
                              }
                          }
                          if(doReload) {
                              messageHandler.sendEmptyMessage(1);
                          } else {
                              messageHandler.sendEmptyMessage(0);
                          }
                      }
                  } catch (InterruptedException e) {
                      e.printStackTrace();
                  }
              }
          };
          t.start();
      }

And here's the code where I am making the Handler on Main Thread (in Singleton class):

private SharedAppManager() {

            //Initialization of the data.

        Looper.prepare();
        messageHandler = new Handler() {
              public void handleMessage(Message msg) {  
                  switch(msg.what) {
                  case 0:
                      Log.d("THREAD", "after every second");
                      break;
                  case 1:
                      if(visitorsDelegate != null) {
                            visitorsDelegate.updateVisitorsTime();
                        }
                      break;
                  default:
                  }
              }
          };
        startHeavyDutyStuff();
    }

What am I doing wrong here?

Edit:
I need to update the UI after each second, that's why I am running a separate thread which could change the Data and call update on UI.

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文