2010-05-31 112 views
7

我正在嘗試在我的Android應用中使用sendOrderedBroadcastAndroid - sendOrderedBroadcast幫助

我希望能夠將Intent從我的一個應用程序發送到另一個應用程序,然後我想從接收Intent的應用程序中獲取數據,在這種情況下,它是一個布爾值true或false。

下面是當前的代碼:

Intent i = new Intent(); 
    i.setAction(GlobalData.PROPOSE_IN_DOMAIN_ROAM_INTENT); 
    i.putExtra("com.testnetworks.QCLEVEL", aProposedTheoreticalQoSLevel); 
    sendOrderedBroadcast(i, null, null, null, Activity.RESULT_OK, null, null); 

這是實現我想要什麼的正確方法?

如果是這樣,我用什麼作爲resultReceiver *參數? (第三參數)

然後如何從廣播中接收數據?

我已經做了一個快速谷歌,並沒有拿出任何例子,任何幫助或例子非常感謝。

更新的代碼:


sendOrderedBroadcast(i, null, domainBroadcast, null, Activity.RESULT_OK, null, null); 

class DomainBroadcast extends BroadcastReceiver{ 

    @Override 
    public void onReceive(Context arg0, Intent intent) { 
     String action = intent.getAction(); 

     if(GlobalData.PROPOSE_IN_DOMAIN_ROAM_INTENT.equals(action)){ 
      Log.d("BROADCAST", "Returning broadcast"); 

      Bundle b = intent.getExtras(); 
      Log.d("BROADCAST", "Returning broadcast " + 
        b.getInt("com.testnetworks.INT_TEST")); 
     }  
    } 

@Override 
    public void onReceive(Context context, Intent intent) { 
      String action = intent.getAction(); 

      if(GlobalData.PROPOSE_IN_DOMAIN_ROAM_INTENT.equals(action)){ 

       Bundle b = intent.getExtras(); 
       int testQCLevel = b.getInt("com.testnetworks.QCLEVEL"); 
       switch(testQCLevel){ 
       case 1: 
        Log.d("QCLevel ", "QCLevel = UNAVAILABLE"); 
        break; 
       case 2: 
        Log.d("QCLevel ", "QCLevel = BELOWUSABILITY"); 
        break; 
       case 3: 
        Log.d("QCLevel ", "QCLevel = VOICE"); 
        break; 
       } 

       intent.putExtra("com.testnetworks.INT_TEST", 100); 

      } 

因此根據文檔的我應該在我的DomainBroadcast接收器中收到100回,但總是回到0.

任何人都可以看到爲什麼?

** resultReceiver - 你自己的BroadcastReceiver治療作爲廣播的最終接受*

+1

你有以上兩個'的onReceive()'方法。 – JRL 2010-05-31 16:57:18

回答

13

您需要get the extra data results Bundle和您的數據添加到它。

嘗試是這樣的:

public class DemoOrderedBroadcast extends Activity { 
    private static String SOMETHING_HAPPENED = "com.example.somethinghappened"; 
    private static String EXTRA_INTEGER = "extra integer"; 

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

     IntentFilter filter = new IntentFilter(SOMETHING_HAPPENED); 

     registerReceiver(new BroadcastReceiver() { 
      @Override 
      public void onReceive(Context context, Intent intent) { 
       Bundle results = getResultExtras(true); 
       results.putInt(EXTRA_INTEGER, 100); 
       Log.d("DemoOrderedBroadcast", 
         "In Initial Receiver: Put 'extra integer' = 100"); 
      } 
     }, filter); 

     Intent intent = new Intent(SOMETHING_HAPPENED); 
     sendOrderedBroadcast(intent, null, new BroadcastReceiver() { 
      @Override 
      public void onReceive(Context context, Intent intent) { 
       Bundle results = getResultExtras(true); 
       Log.d("DemoOrderedBroadcast", 
         "In Result Receiver: Got 'extra integer' = " 
           + results.getInt(EXTRA_INTEGER, -1)); 
      } 
     }, null, Activity.RESULT_OK, null, null); 
    } 
} 

產生所需的輸出:

$ adb -e shell am start -n com.example.DemoOrderedBroadcast/.DemoOrderedBroadcast 
Starting: Intent { cmp=com.example.DemoOrderedBroadcast/.DemoOrderedBroadcast } 
$ adb -e shell logcat | grep D/DemoOrderedBroadcast 
D/DemoOrderedBroadcast(1343): In Initial Receiver: Put 'extra integer' = 100 
D/DemoOrderedBroadcast(1343): In Result Receiver: Got 'extra integer' = 100 
+1

如果您有兩臺廣播接收機,這將如何工作。在我運行的一個例子中,傳入的接收器只被觸發一次。 – 2010-06-15 17:51:16