2015-05-11 46 views
1

我已經在Android中實現了一個應用程序。有三個組件:MainActivity,MyWorker和PublishService。 MyWorker從Android的SensorManager獲取傳感器數據,然後廣播到MainActivity。當MainActivity獲取消息時,它將調用發佈功能以將其推送到我的PublishService。請看下圖: enter image description here多次執行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_72mSend_80開始,然後產生run_87run_87看起來比預期的要多。而下一次,run_87傾向於產生超過這個時間。我不知道爲什麼這會令人開心?我認爲這是因爲Runnable發生的,但我不知道如何解決。請你給我適當的建議,提前謝謝你。

回答

1

我已經自己解決了,但我想在這裏爲每個可能遇到此問題的人展示答案。原因是多線程不能有效地管理線程。事實是我們的系統不能保證多線程同時運行。舉例來說,我把代碼

private Boolean receiveResult = false; 

一些行的onReceive,receiveResult = true;

if (receiveResult) { 
    receiveResult = false; 
    Log.i("run_87", "Result"); 
    Publish(Resulty); 
} 

主題MyActivity的具有更高的優先級,因此被稱爲多的時間比MyWorkerService。