2012-03-20 63 views
5

我在ICS得到這個問題,但不是在以前的版本:廣播接收器的onReceive()從來沒有所謂的

從應用1,我發送廣播,並試圖獲得在app 2活動。但是,onReceive從未被稱爲App 2的活動。

我無法理解該塊的onReceive是否被調用,儘管我已經正確地指定了一切。

我先BroadcastSend

任何幫助,這將有助於我解決這個是非常讚賞運行BroadcastReceive。

App1的發送活動

public class BroadcastSend extends Activity { 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    Intent i = new Intent(); 
    i.setAction("edu.ius.rwisman.custom.intent.action.TEST"); 
    i.putExtra("url","ww.ius.edu"); 
    sendBroadcast(i); 
} 

應用2接收活動

public class BroadcastReceive extends BroadcastReceiver{ 
// Display an alert that we've received a message.  
@Override 
public void onReceive(Context context, Intent intent){ 
    System.out.println("Inside onReceive"); 
    String url = intent.getExtras().getString("url"); 
    Toast.makeText(context, "BroadcastReceive:"+url, Toast.LENGTH_SHORT).show(); 
    } 

應用2的清單

<?xml version="1.0" encoding="utf-8"?> 

<application android:icon="@drawable/icon" android:label="@string/app_name"> 
    <receiver android:name="edu.ius.rwisman.BroadcastReceive.BroadcastReceive" android:enabled="true" android:exported="true"> 
     <intent-filter> 
      <action android:name="edu.ius.rwisman.custom.intent.action.TEST"/> 
     </intent-filter> 
    </receiver> 
</application> 

回答

10

在ICS中,您至少要手動啓動應用程序才能接收廣播。在Android 3.1+中,如果應用程序從未運行或已被強制停止,它們處於停止狀態。系統從廣播意圖中排除這些應用程序。他們可以通過使用Intent.FLAG_INCLUDE_STOPPED_PACKAGES標誌像這樣被列入..

i.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES); 
+0

感謝這個固定的問題。但我們首先運行接收活動,然後運行發送活動。那麼我們爲什麼面臨這個問題? – user264953 2012-03-20 09:31:50

+0

應用程序在第一次安裝但尚未啓動時以及用戶手動停止時(處於管理應用程序中)時處於停止狀態......請參閱http://developer.android.com/sdk/android- 3.1.html#launchcontrols – 5hssba 2012-03-20 09:38:21

+2

但在註冊廣播時如何在清單中包含此標誌 – 2014-01-29 07:35:26

相關問題