我正在使用下面的函数创建一个新线程。
public void toCallAsynchronous() {
mThread = new Thread() {
public void run() {
while (true) {
try {
// do something here
if(mLoggedIn)
{
boolean syncresult = mDownload.syncData();
}
Log.d(TAG, "local Thread sleeping");
Thread.sleep(10000);
//mHandler.postDelayed(this, 1000);
} catch (InterruptedException e) {
Log.e(TAG, "local Thread error", e);
}
}
}
};
mThread.start();
}我在onResume中调用这个,因为我希望这个线程只在用户登录到他的帐户时启动。synData函数访问服务器以获取新文件,下载它们并更新数据库中的条目。你能指出我在这里做错了什么吗?
发布于 2012-11-01 14:12:04
while(true)很奇怪,因为您可能不希望这个线程永远运行。至少应该是while(!isFinishing())
此外,您还需要确保mDownload.syncData不会尝试访问任何UI,如果访问,将会出现异常。
https://stackoverflow.com/questions/13171749
复制相似问题