如何在不冻结UI的情况下在Android中运行无限循环? [英] How to run an infinite loop in Android without freezing the UI?

查看:77
本文介绍了如何在不冻结UI的情况下在Android中运行无限循环?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建和android程序,现在我需要使用android这样的程序来持续不断地通过蓝牙发送数据:

I'm creating and android program which needs to to continuously keep sending data over the bluetooth now I use something like this:

for(;;)
{
//send message
}

尽管此方法有效,但它冻结了我的UI和应用程序,如何在不冻结我的UI的情况下实现相同的内容?

though this works it freezes my UI and app how can I implement the same without freezing my UI?

我确定应用程序正在监视数据时正在发送数据.

I am sure that the app is sending the data as I monitor the data.

推荐答案

将循环放入 AsyncTask Service 单独的线程或仅在您的Activity旁边的另一个线程中.永远不要在主(UI)线程中进行繁重的工作,无限循环或阻塞调用.

Put your loop in an AsyncTask, Service with separate Thread or just in another Thread beside your Activity. Never do heavy work, infinte loops, or blocking calls in your main (UI) Thread.

这篇关于如何在不冻结UI的情况下在Android中运行无限循环?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆