android: 傳送自定義廣播

yufan發表於2016-02-03

5.3.1    傳送標準廣播

 

在傳送廣播之前,我們還是需要先定義一個廣播接收器來準備接收此廣播才行,不然發 出去也是白髮。因此新建一個 MyBroadcastReceiver 繼承自 BroadcastReceiver,程式碼如下所示:

public class MyBroadcastReceiver extends BroadcastReceiver {

 

@Override

public void onReceive(Context context, Intent intent) { Toast.makeText(context, "received in MyBroadcastReceiver",

Toast.LENGTH_SHORT).show();

}

}

這裡當 MyBroadcastReceiver 收到自定義的廣播時,就會彈出 received in MyBroadcastReceiver的提示。然後在 AndroidManifest.xml 中對這個廣播接收器進行註冊:

 

<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.broadcasttest"

android:versionCode="1" android:versionName="1.0" >

……

<application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" >

……

<receiver android:name=".MyBroadcastReceiver">

<intent-filter>

<action android:name="com.example.broadcasttest. MY_BROADCAST"/>

</intent-filter>

</receiver>

</application>

</manifest>

可 以 看 到 , 這 裡 讓 MyBroadcastReceiver 接 收 一 條 值 為 com.example.broadcasttest. MY_BROADCAST 的廣播,因此待會兒在傳送廣播的時候,我們就需要發出這樣的一條廣播。

接下來修改 activity_main.xml 中的程式碼,如下所示:

 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" >

 

<Button android:id="@+id/button" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Send Broadcast"/>

</LinearLayout>

這裡在佈局檔案中定義了一個按鈕,用於作為傳送廣播的觸發點。然後修改 MainActivity中的程式碼,如下所示:

 

public class MainActivity extends Activity {

……

@Override

protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main);

Button button = (Button) findViewById(R.id.button);

button.setOnClickListener(new OnClickListener() {

@Override

public void onClick(View v) {

Intent intent = new Intent("com.example.broadcasttest.MY_BROADCAST");

}

});

……

sendBroadcast(intent);

}

……

}

可以看到,我們在按鈕的點選事件裡面加入了傳送自定義廣播的邏輯。首先構建出了一 個 Intent 物件,並把要傳送的廣播的值傳入,然後呼叫了 Context 的 sendBroadcast()方法將廣 播傳送出去,這樣所有監聽 com.example.broadcasttest.MY_BROADCAST 這條廣播的廣播接 收器就會收到訊息。此時發出去的廣播就是一條標準廣播。

重新執行程式,並點選一下 Send Broadcast 按鈕,效果如圖 5.7 所示。

 

圖   5.7

 

這樣我們就成功完成了傳送自定義廣播的功能。另外,由於廣播是使用 Intent 進行傳遞 的,因此你還可以在 Intent 中攜帶一些資料傳遞給廣播接收器。

 

5.3.2    傳送有序廣播

 

廣播是一種可以跨程式的通訊方式,這一點從前面接收系統廣播的時候就可以看出來 了。因此在我們應用程式內發出的廣播,其他的應用程式應該也是可以收到的。為了驗證這 一點,我們需要再新建一個 BroadcastTest2 專案。

將專案建立好之後,還需要在這個專案下定義一個廣播接收器,用於接收上一小節中的 自定義廣播。新建 AnotherBroadcastReceiver 繼承自 BroadcastReceiver,程式碼如下所示:

 

public class AnotherBroadcastReceiver extends BroadcastReceiver {

 

@Override

public void onReceive(Context context, Intent intent) { Toast.makeText(context, "received in AnotherBroadcastReceiver",

Toast.LENGTH_SHORT).show();

}

 

}

 

這 裡 仍 然 是 在 廣 播 接 收 器 的 onReceive() 方 法 中 彈 出 了 一 段 文 本 信 息 。 然 後 在AndroidManifest.xml 中對這個廣播接收器進行註冊,程式碼如下所示:

 

<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.broadcasttest2"

android:versionCode="1"

android:versionName="1.0" >

……

<application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" >

……

<receiver android:name=".AnotherBroadcastReceiver" >

<intent-filter>

<action android:name="com.example.broadcasttest.MY_BROADCAST" />

</intent-filter>

</receiver>

</application>

</manifest>

可 以 看 到 , AnotherBroadcastReceiver 同 樣 接 收 的 是 com.example.broadcasttest. MY_BROADCAST 這條廣播。現在執行 BroadcastTest2 專案將這個程式安裝到模擬器上,然 後重新回到 BroadcastTest 專案的主介面,並點選一下 Send Broadcast 按鈕,就會分別彈出兩 次提示資訊,如圖 5.8 所示。

圖   5.8

 

這樣就強有力地證明了,我們的應用程式發出的廣播是可以被其他的應用程式接收到的。

不過到目前為止,程式裡發出的都還是標準廣播,現在我們來嘗試一下傳送有序廣播。 關閉 BroadcastTest2 專案,然後修改 MainActivity 中的程式碼,如下所示:

 

public class MainActivity extends Activity {

……

@Override

protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main);

Button button = (Button) findViewById(R.id.button);

button.setOnClickListener(new OnClickListener() {

@Override

public void onClick(View v) {

Intent intent = new Intent("com.example.broadcasttest.MY_BROADCAST");

 

 }});

 

sendOrderedBroadcast(intent, null);

 

}

……

}

可 以 看 到 , 發 送 有 序 廣 播 只 需 要 改 動 一 行 代 碼 , 即 將 sendBroadcast() 方 法 改 成 sendOrderedBroadcast()方法。sendOrderedBroadcast()方法接收兩個引數,第一個引數仍然是 Intent,第二個引數是一個與許可權相關的字串,這裡傳入 null 就行了。現在重新執行程式, 並點選 Send Broadcast 按鈕,你會發現,兩個應用程式仍然都可以接收到這條廣播。

看上去好像和標準廣播沒什麼區別嘛,不過別忘了,這個時候的廣播接收器是有先後順 序的,而且前面的廣播接收器還可以將廣播截斷,以阻止其繼續傳播。

那麼該如何設定廣播接收器的先後順序呢?當然是在註冊的時候進行設定的了,修改AndroidManifest.xml 中的程式碼,如下所示:

 

<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.broadcasttest"

android:versionCode="1" android:versionName="1.0" >

……

<application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" >

……

<receiver android:name=".MyBroadcastReceiver">

<intent-filter android:priority="100" >

<action android:name="com.example.broadcasttest.MY_BROADCAST"/>

</intent-filter>

</receiver>

</application>

</manifest>

可以看到,我們通過 android:priority 屬性給廣播接收器設定了優先順序,優先順序比較高的 廣播接收器就可以先收到廣播。這裡將 MyBroadcastReceiver 的優先順序設成了 100,以保證它 一定會在 AnotherBroadcastReceiver 之前收到廣播。

既然已經獲得了接收廣播的優先權,那麼 MyBroadcastReceiver 就可以選擇是否允許廣 播繼續傳遞了。修改 MyBroadcastReceiver 中的程式碼,如下所示:

 

public class MyBroadcastReceiver extends BroadcastReceiver {

 

@Override

public void onReceive(Context context, Intent intent) { Toast.makeText(context, "received in MyBroadcastReceive",

Toast.LENGTH_SHORT).show();

abortBroadcast();

}

 

}

如果在 onReceive()方法中呼叫了 abortBroadcast()方法,就表示將這條廣播截斷,後面的 廣播接收器將無法再接收到這條廣播。現在重新執行程式,並點選一下 Send Broadcast 按鈕, 你 會 發 現 , 只 有 MyBroadcastReceiver 中 的 Toast 信 息 能 夠 彈 出 , 說 明 這 條 廣 播 經 過 MyBroadcastReceiver 之後確實是終止傳遞了。

相關文章