1
在我的Android應用程序,我有一小段代碼,類似於Run code for x seconds in Java?不斷傳遞數據給AsyncTask?
long t= System.currentTimeMillis();
long end = t+15000;
while(System.currentTimeMillis() < end) {
//useful calculation
}
而這個代碼將凍結UI線程,所以我不知道是否有在數據不斷傳遞給我的AsyncTask方式那會在後臺線程中進行計算以避免凍結?或者有更好的方法,或者繼續在我的while循環中調用AsyncTask?
聽起來像是你想有一個主題,而不是一個的AsyncTask。如果某些事情應該在後臺繼續進行,則Thread更合適。 AsyncTasks在默認情況下在一個線程上運行,所以你不應該使用一個來處理超過幾秒的處理。除此之外 - 你想通過什麼樣的數據,以什麼頻率?這樣做的一種正常方式是通過同步消息隊列。將數據傳遞給線程或任務是可能的,但容易出錯。 –
查看Android中的處理程序或服務類。 –
@GabeSechan我正在以非常高的頻率(至少對眼睛)從傳感器讀取數據(雙倍),而不是手機上的傳感器,我不確定是什麼,但它看起來像10Hz ish。你能指給我一份文件嗎? –