Android:如何运行简单的后台循环

发布于 2024-09-30 18:12:57 字数 615 浏览 1 评论 0原文

Hej,我正在尝试创建一个在主线程上运行的应用程序,但也有一个后台循环运行(以检查连接)。

我只想调用某个函数 onCreate,并且该函数应该在后台运行...我已尝试使用下面的代码,但似乎不起作用...有什么建议吗?

    void doStuffBackground()
    { 
     Thread testingForBluetooth = new Thread()
     {
      public void run()
      {
       try
       {
        for(int i = 0; i < 100; i++)
        {

         writeTerminal('x');
         sleep(100);

        }

       }
       catch(Exception e)
       {
        Log.e("Threading", e.toString());
       }
       finally
       {
        finish();
       }
      }
      };

      }

    }

但又...不工作?

提前感谢

Hej, I'm trying to create an app that runs on a main thread, but also has a background loop running (to check for a connection).

I just want to call a certain function onCreate, and that function should run in the background...I've tried with the code below, but doesn't seem to work...any suggestions?

    void doStuffBackground()
    { 
     Thread testingForBluetooth = new Thread()
     {
      public void run()
      {
       try
       {
        for(int i = 0; i < 100; i++)
        {

         writeTerminal('x');
         sleep(100);

        }

       }
       catch(Exception e)
       {
        Log.e("Threading", e.toString());
       }
       finally
       {
        finish();
       }
      }
      };

      }

    }

But again...not working?

Thanx in advance

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

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

发布评论

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

评论(1

负佳期 2024-10-07 18:12:57

你从未启动过这个线程。
无论如何,如果您需要后台任务,您也可以尝试服务。

You never started the thread.
Anyway if you need a background task, you could also try a service.

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