简体   繁体   English

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

[英]Android: how run simple background loop

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? 我只想在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();
       }
      }
      };

      }

    }

But again...not working? 但是再次...不起作用?

Thanx in advance 提前感谢

You never started the thread. 您从未启动线程。 Anyway if you need a background task, you could also try a service. 无论如何,如果您需要后台任务,也可以尝试服务。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM