我已經在Android中實現了一個應用程序。有三個組件:MainActivity,MyWorker和PublishService。 MyWorker從Android的SensorManager獲取傳感器數據,然後廣播到MainActivity。當MainActivity獲取消息時,它將調用發佈功能以將其推送到我的PublishService。請看下圖: 多次執行Runnable帖子到Looper的活動
下面我已經表明我的代碼演示:
public class MyActivity extends Activity implements Runnable {
public class PushReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent i){
Result = i.getStringExtra("Result");
Log.d("onReceive_72", Result);
mSend();
}
}
private void mSend(){
Log.d("mSend_80","sent");
handler.post(this);
}
@Override
public void run() {
Log.d("run_87", Result);
handler.postDelayed(this, 500);
Publish(Result); // publish function
}
// more code here <--->
}
我的問題是,當應用程序運行時,logcat的顯示"run_87": Result
很多次,但"onReceive_72": Result
和"mSend_80":"sent"
出現一次。例如:
05-11 11:42:01.286 11631-11631/example I/onReceive_72﹕ Result
05-11 11:42:01.286 11631-11631/example I/mSend_80﹕ sent
05-11 11:42:01.286 11631-11631/example I/run_87﹕ Result
05-11 11:42:01.786 11631-11631/example I/run_87﹕ Result
05-11 11:42:01.946 11631-11631/example I/onReceive_72﹕ Result
05-11 11:42:01.946 11631-11631/example I/mSend_80﹕ sent
05-11 11:42:01.946 11631-11631/example I/run_87﹕ Result
05-11 11:42:02.286 11631-11631/example I/run_87﹕ Result
05-11 11:42:02.446 11631-11631/example I/run_87﹕ Result
05-11 11:42:02.556 11631-11631/example I/onReceive_72﹕ Result
05-11 11:42:02.556 11631-11631/example I/mSend_80﹕ sent
05-11 11:42:02.556 11631-11631/example I/run_87﹕ Result
05-11 11:42:02.786 11631-11631/example I/run_87﹕ Result
05-11 11:42:02.946 11631-11631/example I/run_87﹕ Result
05-11 11:42:03.056 11631-11631/example I/run_87﹕ Result
05-11 11:42:03.221 11631-11631/example I/onReceive_72﹕ Result
05-11 11:42:03.221 11631-11631/example I/mSend_80﹕ sent
05-11 11:42:03.221 11631-11631/example I/run_87﹕ Result
05-11 11:42:03.286 11631-11631/example I/run_87﹕ Result
05-11 11:42:03.461 11631-11631/example I/run_87﹕ Result
05-11 11:42:03.556 11631-11631/example I/run_87﹕ Result
05-11 11:42:03.721 11631-11631/example I/run_87﹕ Result
05-11 11:42:03.786 11631-11631/example I/run_87﹕ Result
05-11 11:42:03.836 11631-11631/example I/onReceive_72﹕ Result
05-11 11:42:03.836 11631-11631/example I/mSend_80﹕ sent
05-11 11:42:03.836 11631-11631/example I/run_87﹕ Result
05-11 11:42:03.961 11631-11631/example I/run_87﹕ Result
05-11 11:42:04.056 11631-11631/example I/run_87﹕ Result
05-11 11:42:04.221 11631-11631/example I/run_87﹕ Result
05-11 11:42:04.286 11631-11631/example I/run_87﹕ Result
05-11 11:42:04.336 11631-11631/example I/run_87﹕ Result
05-11 11:42:04.461 11631-11631/example I/run_87﹕ Result
05-11 11:42:04.486 11631-11631/example I/onReceive_72﹕ Result
05-11 11:42:04.486 11631-11631/example I/mSend_80﹕ sent
05-11 11:42:04.486 11631-11631/example I/run_87﹕ Result
05-11 11:42:04.556 11631-11631/example I/run_87﹕ Result
05-11 11:42:04.721 11631-11631/example I/run_87﹕ Result
05-11 11:42:04.786 11631-11631/example I/run_87﹕ Result
05-11 11:42:04.836 11631-11631/example I/run_87﹕ Result
05-11 11:42:04.961 11631-11631/example I/run_87﹕ Result
05-11 11:42:04.986 11631-11631/example I/run_87﹕ Result
05-11 11:42:05.056 11631-11631/example I/run_87﹕ Result
正如你所看到的,對於每一個圓圈,它onReceive_72
和mSend_80
開始,然後產生run_87
。 run_87
看起來比預期的要多。而下一次,run_87
傾向於產生超過這個時間。我不知道爲什麼這會令人開心?我認爲這是因爲Runnable發生的,但我不知道如何解決。請你給我適當的建議,提前謝謝你。