2011-02-03 22 views
13

sendBroadcast() - 是否應該在Activity內部調用? 我想從我的實用工具類的方法調用sendBroadcast()不擴展活動。我收到編譯錯誤如下sendBroadcast()的用法

的方法sendBroadcast(意向)是 未定義類型MyWrapperClass MyWrapperClass.java

這裏是代碼片段:

abstract class MyWrapperClass { 

    public static void sendData() 
     { 
      Intent intent = new Intent ("com.proj.utility.mgr",null); 

      intent.putExtra("example","Broadcasting "); 

      sendBroadcast(intent); 

     } 
    } 

是在我的課堂裏使用sendBroadcast調用的任何概念。在Activity中使用sendBroadcast()沒有問題。 這裏有人能幫我解決嗎? 或者任何其他建議都被邀請以異步方式將數據從工具類返回給應用程序。 在此先感謝。

+2

您需要訪問應用程序上下文才能調用sendbroadcast。你能否從android文檔中得到這些信息? – 2011-02-03 06:55:38

回答

20

你應該通過從活動類上下文工具類訪問像startActivity,sendBroadcast具體應用資源等

context.sendBroadcast(intent); 
0

如果擴展正被前稱爲Application對象(主入口的第一個Activity),那麼你可以爲你的應用程序保留一個共享的singletone。

相關問題