Android:如何运行简单的后台循环
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你从未启动过这个线程。
无论如何,如果您需要后台任务,您也可以尝试服务。
You never started the thread.
Anyway if you need a background task, you could also try a service.