2017-05-27 30 views
-1

我無法調用廣播接收對象的活動功能。有一個運行敬酒,因此執行不流存在,但本身的活動功能不被稱爲在廣播接收機不叫活動函數對象

protected void onCreate() 
    { 
     ... 
     ... 
     this.registerReceiver(receiver, new IntentFilter("ACTION")); 

    } 

    BroadcastReceiver receiver = new BroadcastReceiver() { 

    Toast.makeText(MainActivity.this, "Printed", Toast.LENGTH_LONG).show(); // This runs 

    FunctionA(); // <-- This function is not called 
    }; 

    public void FunctionA() 
    { 
    ... // Function holds view objects 
    } 

我環顧四周,試圖像這樣的接收對象的內部沒有任何的運氣

 BroadcastReceiver receiver = new BroadcastReceiver() { 

      Toast.makeText(MainActivity.this, "Printed", Toast.LENGTH_LONG).show(); //<--- This runs fine   

      MainActivity.this.runOnUiThread(new Runnable() { 
      @Override 
      public void run() { 

       FunctionA(); // <-- Function still is not called 

      } 
     }); 

    } 

問題是爲什麼不調用函數?有沒有辦法在該廣播接收器對象中調用該函數?

+0

您確切地說,確定「函數」沒有被調用? FunctionA()'確實*做了什麼? – CommonsWare

+0

該函數負責改變UI上的視圖。我甚至有Logs中沒有打印在Logcat中的函數。 – Czar

回答

1

您的代碼不在BroadcastReceiveronReceive()方法中。 onReceive()是當廣播到達什麼被調用。

現在,您的代碼正在執行時BroadcastReceiver實例化。這不太可能是你想要的。

更普遍,這是不可能的使用BroadcastReceiver,監聽系統廣播,是什麼問題,你正在試圖解決相應的解決方案。 LocalBroadcastManager或事件總線的一些其它形式是作爲過程中的消息的更好的解決方案,例如。

+0

感謝您的建議,你的想法實際上工作。我沒有使用LocalBroadcastManager,而是使用EventBus。 – Czar