Android中發(fā)送有序廣播案例代碼
Android系統(tǒng)提供了兩種廣播類(lèi)型,一種是有序廣播,一種是有序廣播。
(1)無(wú)序廣播是完全異步執(zhí)行,發(fā)送廣播時(shí)所有監(jiān)聽(tīng)這個(gè)廣播的廣播接收者都會(huì)收到此消息,但接收的順序不確定。
(2)有序廣播是按照接收者的優(yōu)先級(jí)接收,只有一個(gè)廣播接收者能接收信息,在此廣播接收者中邏輯執(zhí)行完畢后,才會(huì)繼續(xù)傳遞。
實(shí)驗(yàn)要求:通過(guò)sendOrderedBroadeast()發(fā)送一條有序廣播
1.在activity-main.xml布局文件中代碼如下:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:background="@drawable/stitch_one"
tools:context="cn.edu.bzu.orderedbroadcast.MainActivity">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:onClick="send"
android:layout_marginTop="50dp"
android:text="發(fā)送有序廣播"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:background="#FFD202"
android:textSize="20sp"
/>
</RelativeLayout>
2.在MainActivity中代碼如下:
package cn.edu.bzu.orderedbroadcast;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void send(View view){
Intent intent=new Intent();
//定義廣播事件類(lèi)型
intent.setAction("Intercept_Stitch");
//發(fā)送廣播
sendOrderedBroadcast(intent,null);
}
}
3.創(chuàng)建三個(gè)廣播接收者
(1)MyBroadcastReceiverOne
(2)MyBroadcastReceiverTwo
(3)MyBroadcastReceiverThree
(1)在MyBroadcastReceiverOne中代碼如下:
package cn.edu.bzu.orderedbroadcast;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
public class MyBroadcastReceiverOne extends BroadcastReceiver {
public MyBroadcastReceiverOne() {
}
@Override
public void onReceive(Context context, Intent intent) {
Log.i("MyBroadcastReceiverOne","自定義的廣播接收者one,接收到了廣播事件");
}
}
(2)在MyBroadcastReceiverTwo中代碼如下:
package cn.edu.bzu.orderedbroadcast;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
public class MyBroadcastReceiverTwo extends BroadcastReceiver {
public MyBroadcastReceiverTwo() {
}
@Override
public void onReceive(Context context, Intent intent) {
Log.i("MyBroadcastReceiverTwo","自定義的廣播接收者Two,接收到了廣播事件");
}
}
(3)在MyBroadcastReceiverThree中代碼如下:
package cn.edu.bzu.orderedbroadcast;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
public class MyBroadcastReceiverThree extends BroadcastReceiver {
public MyBroadcastReceiverThree() {
}
@Override
public void onReceive(Context context, Intent intent) {
Log.i("MyBroadcastReceiverThree","自定義的廣播接收者Three,接收到了廣播事件");
}
}
4.在配置文件AndroidManifest中代碼如下:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="cn.edu.bzu.orderedbroadcast">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver
android:name=".MyBroadcastReceiverOne">
<intent-filter android:priority="1000">
<action android:name="Intercept_Stitch"/>
</intent-filter>
</receiver>
<receiver
android:name=".MyBroadcastReceiverTwo">
<intent-filter android:priority="200">
<action android:name="Intercept_Stitch"/>
</intent-filter>
</receiver>
<receiver
android:name=".MyBroadcastReceiverThree">
<intent-filter android:priority="600">
<action android:name="Intercept_Stitch"/>
</intent-filter>
</receiver>
</application>
</manifest>
5.實(shí)驗(yàn)效果圖:

運(yùn)行程序后解決如下問(wèn)題:
問(wèn)題1:程序啟動(dòng)后,單擊“發(fā)送有序廣播”按鈕,發(fā)出一條廣播事件,此時(shí)觀察LogCat窗口下的提示信息,輸出什么?為什么?
點(diǎn)擊按鈕后出現(xiàn)如下圖所示提示信息,原因是有序廣播根據(jù)優(yōu)先級(jí)接收廣播信息的,在配置文件中廣播接收者One的priority值最大,所以它先接收到廣播,其次是Three,最后是Two。其中android:priority="值"是用來(lái)設(shè)置廣播接收者的優(yōu)先級(jí)的,值越大,優(yōu)先級(jí)別越高。

問(wèn)題2:若將廣播接收者M(jìn)yBroadcastReceiverTwo優(yōu)先級(jí)同樣設(shè)置為1000,并將MyBroadcastReceiverTwo注冊(cè)在MyBroadcastReceiverOne前面,再來(lái)運(yùn)行程序,觀察結(jié)果,你能得出什么結(jié)論?
修改配置文件AndroidManifest中代碼如下:
<receiver
android:name=".MyBroadcastReceiverTwo">
<intent-filter android:priority="1000">
<action android:name="Intercept_Stitch"/>
</intent-filter>
</receiver>
<receiver android:name=".MyBroadcastReceiverOne">
<intent-filter android:priority="1000">
<action android:name="Intercept_Stitch"/>
</intent-filter>
</receiver>
<receiver
android:name=".MyBroadcastReceiverThree">
<intent-filter android:priority="600">
<action android:name="Intercept_Stitch"/>
</intent-filter>
</receiver>
運(yùn)行程序出現(xiàn)如下圖所示提示信息,原因是當(dāng)廣播接收者的優(yōu)先級(jí)別相同時(shí),先注冊(cè)的廣播接收者先接收到廣播。

問(wèn)題3:修改MyBroadcastReceiverTwo如下,觀察結(jié)果,你又可以得出什么結(jié)論?
public class MyBroadcastReceiverTwo extends BroadcastReceiver {
public MyBroadcastReceiverTwo() {
}
@Override
public void onReceive(Context context, Intent intent) {
Log.i("MyBroadcastReceiverTwo","自定義的廣播接收者Two,接收到了廣播事件");
abortBroadcast();//有序廣播攔截器
Log.i("MyBroadcastReceiverTwo","我是廣播接收者Two,廣播被我終止了");
}
}
運(yùn)行程序出現(xiàn)如下圖所示提示信息,原因是廣播接收者Two的優(yōu)先級(jí)最高,所以在Two中寫(xiě)一個(gè)攔截器,優(yōu)先級(jí)低的廣播接收者將接收不到廣播信息,所以O(shè)ne和Three沒(méi)有接收到廣播事件,也就沒(méi)有打印關(guān)于它們的信息。

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android內(nèi)置SQLite的使用詳細(xì)介紹
這篇文章主要介紹了Android內(nèi)置SQLite的使用詳細(xì)介紹,文章通過(guò)文章展開(kāi)詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下2022-09-09
Android實(shí)現(xiàn)懸浮對(duì)話框代碼
這篇文章主要介紹了Android實(shí)現(xiàn)懸浮對(duì)話框代碼的相關(guān)資料,需要的朋友可以參考下2016-03-03
在Android里完美實(shí)現(xiàn)基站和WIFI定位
眾所周知的,在OPhone和大部分國(guó)產(chǎn)的Android定制機(jī)里不支持最簡(jiǎn)單實(shí)用的基站和WIFI定位,只能使用速度慢而耗電的GPS定位,但OPhone和華為/中興生產(chǎn)的一些Android定制機(jī)卻占據(jù)了一定的市場(chǎng),因此導(dǎo)致了很多使用了定位技術(shù)的Andorid應(yīng)用挺尷尬的。2014-07-07
Android調(diào)用google地圖生成路線圖實(shí)現(xiàn)代碼
Android程序調(diào)用本機(jī)google地圖并且傳遞起始和終點(diǎn)位置生成路線圖,有需要的朋有可以參考下,或許本文對(duì)你有所幫助,好了話不多說(shuō),看代碼2013-02-02
Android中EditText光標(biāo)的顯示與隱藏方法
這篇文章主要給大家介紹了關(guān)于Android中EditText光標(biāo)的顯示與隱藏以及Android之第一次不顯示EditText光標(biāo)的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考下2021-11-11
Android共享元素動(dòng)畫(huà)效果顯示問(wèn)題解決
什么是共享元素呢?可以理解為當(dāng)頁(yè)面跳轉(zhuǎn)是,看起來(lái)一個(gè)View屬于界面A又屬于界面B,下面這篇文章主要給大家介紹了關(guān)于Android共享元素動(dòng)畫(huà)效果顯示問(wèn)題的相關(guān)資料,需要的朋友可以參考下2022-02-02
android開(kāi)發(fā)之蜂鳴提示音和震動(dòng)提示的實(shí)現(xiàn)原理與參考代碼
蜂鳴提示音和震動(dòng)提示此功能在手機(jī)使用中很實(shí)用,最近在讀zxing項(xiàng)目,學(xué)到了不少東西;我們一起來(lái)看看他是怎么做的,感興趣的朋友可以了解下哦2013-01-01
Android 實(shí)現(xiàn)不依賴(lài)焦點(diǎn)和選中的TextView跑馬燈
本文主要介紹Android 跑馬燈的實(shí)現(xiàn),這里提供實(shí)現(xiàn)詳細(xì)實(shí)現(xiàn)代碼供大家參考,有需要的小伙伴可以看下2016-07-07
Android?獲取實(shí)時(shí)網(wǎng)速實(shí)現(xiàn)詳解
這篇文章主要為大家介紹了Android?獲取實(shí)時(shí)網(wǎng)速實(shí)現(xiàn)詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-11-11

