2010-11-27 21 views
2

我有一個實現Observer的類。我可以通過不擴展活動的課程發送廣播嗎?

當Obervered狀態改變時,我有一個在這個類中觸發的方法,我想用這個方法發出一個廣播。這意味着我的活動可以偵聽這些廣播消息,並對數據進行處理。

這是我迄今爲止

public class DoStuff extends BroadcastReceiver implements Observer 
{ 
    public void observe(String message) 
    { 
     Log.d("TEST", "Inside observe() on DoStuff " + message); 

     // Here I want to send out a broadcast to MyActivity with message 
     // in the extras 
    } 

} 

我不能使用context.sendBroadcast,因爲我沒有訪問此應用程序上下文,我怎麼能做到這一點?

在此先感謝

+0

爲什麼不使用BroadcastReciever的`onRecieve`中的`Context` ...對不起,我還沒試過,但值得一試。 – st0le 2010-11-27 17:12:39

回答

0

爲什麼使用BroadcastReciever實現Observer接口?如果您真的需要訪問Context實例,我想ServiceApplication是更好的選擇。 BroadcastRecievers只在onRecieve()方法調用期間有效,所以從我的角度來看,使用它們實現Observer接口是沒有意義的。

相關問題