![[安卓開發(fā)] Broadcast 三種廣播的使用總結(jié).doc_第1頁](http://file.renrendoc.com/FileRoot1/2020-1/11/0e63751e-644e-48e3-a887-cb4ec501b619/0e63751e-644e-48e3-a887-cb4ec501b6191.gif)
![[安卓開發(fā)] Broadcast 三種廣播的使用總結(jié).doc_第2頁](http://file.renrendoc.com/FileRoot1/2020-1/11/0e63751e-644e-48e3-a887-cb4ec501b619/0e63751e-644e-48e3-a887-cb4ec501b6192.gif)
![[安卓開發(fā)] Broadcast 三種廣播的使用總結(jié).doc_第3頁](http://file.renrendoc.com/FileRoot1/2020-1/11/0e63751e-644e-48e3-a887-cb4ec501b619/0e63751e-644e-48e3-a887-cb4ec501b6193.gif)
![[安卓開發(fā)] Broadcast 三種廣播的使用總結(jié).doc_第4頁](http://file.renrendoc.com/FileRoot1/2020-1/11/0e63751e-644e-48e3-a887-cb4ec501b619/0e63751e-644e-48e3-a887-cb4ec501b6194.gif)
![[安卓開發(fā)] Broadcast 三種廣播的使用總結(jié).doc_第5頁](http://file.renrendoc.com/FileRoot1/2020-1/11/0e63751e-644e-48e3-a887-cb4ec501b619/0e63751e-644e-48e3-a887-cb4ec501b6195.gif)
已閱讀5頁,還剩5頁未讀, 繼續(xù)免費閱讀
版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)
文檔簡介
安卓開發(fā) Broadcast 三種廣播的使用總結(jié)1、什么是安卓的Broadcast?安卓的四大組件之一,是一種廣泛應(yīng)用在應(yīng)用程序之間傳輸信息的機制。2、什么是安卓的BroadcastReceiver?是對發(fā)送出來的廣播進行過濾接收并響應(yīng)的一類組件,它就是用來接收來自系統(tǒng)和應(yīng)用中的廣播。例如系統(tǒng)的廣播有開機廣播: 系統(tǒng)在開機時候會發(fā)送開機廣播,程序接收到之后,能進行開機自啟動。 網(wǎng)絡(luò)狀態(tài)改變廣播: 3g變wifi、網(wǎng)絡(luò)斷開等。電量改變廣播等等。3、Anroid為什么要這樣設(shè)計?大大減少開發(fā)工作量和開發(fā)周期 作為開發(fā)者,只需要掌握BroadcastReceiver4、怎么理解Broadcast和BroadcastReceiver ?Broadcast就像現(xiàn)實中的廣播電臺,他發(fā)廣播信號來,然后我們用收音機來接收,然后處理,并且播放出聲音, BroadcastReceiver就相當于那臺收音機。5、使用方法發(fā)送:把信息裝入一個Intent對象(如:Action、Category),通過調(diào)相應(yīng)的方法將Intent對象以廣播的方式發(fā)送出去: sendBroadcast(); sendOrederBroadcast(); sendStickyBroadcast();接收:當Intent發(fā)送之后,所有已經(jīng)注冊receivedBroadcastReceiver會檢查注冊時的IntentFilter是否與發(fā)送的Intent相匹配,若匹配則就會調(diào)用BroadcastReceiver的onReceiver()方法。所以當我們定義一個BroadcastReceiver的時候,都需要實現(xiàn)onReceiver()方法。注意:BroadcastReceiver需要注冊 靜態(tài)注冊 代碼動態(tài)注冊6、注意!BroadReceiver生命周期只有十秒左右,不能直接執(zhí)行耗時操作,不然會出現(xiàn)ANR(應(yīng)用程序無響應(yīng)),也不能用子線程來做,因為每次廣播來的時候都會創(chuàng)建一個Reveiver對象,并且調(diào)用onReceiver,執(zhí)行完之后 ,對象會立刻被銷毀,子線程也沒了 要做耗時操作的話,應(yīng)該通過發(fā)送Intent給Service,由Service來完成。 動態(tài)注冊廣播接受者的話要在Destory回調(diào)事件進行unregister7、廣播的分類普通廣播 (Normal broadcast)所有監(jiān)聽該廣播接受者都可以監(jiān)聽到該廣播同級別接收先后順序是隨機的(無序)級別低的后收到廣播接收器不能截斷廣播的繼續(xù)傳播,也不能處理廣播同級別動態(tài)注冊高于靜態(tài)注冊有序廣播 (Oredered broadcast)按照接收者的優(yōu)先順序來接收廣播,優(yōu)先級別在intent-filter中的priority中聲明,-1000到1000之間,值越大優(yōu)先級越高,可以終止廣播的繼續(xù)傳播,接受者可以修改intent的內(nèi)容。同級別接收順序是隨機的級別低的后收到能截斷廣播的繼續(xù)傳播,高級別的廣播接收器接收廣播后能決定時候截斷。能處理廣播同級別動態(tài)注冊高于靜態(tài)注冊異步廣播 (粘滯性滯留廣播) ps:已被棄用不能處理結(jié)果給下一個接收者,無法終止廣播。一直存在可以先發(fā)送廣播,再注冊接收器需要在清單文件添加android.permission.BROADCAST_STICKY權(quán)限8、Demo布局actibity_main三個按鈕: MainActivity.Javapublic class MainActivity extends AppCompatActivity implements View.OnClickListener private Button btOne; private Button btTwo; private Button btThree; MyReiceiverThree myReiceiver = new MyReiceiverThree(); Override protected void onCreate(Bundle savedInstanceState) super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btOne = (Button) findViewById(R.id.bt_one); btTwo = (Button) findViewById(R.id.bt_two); btThree = (Button) findViewById(R.id.bt_three); btOne.setOnClickListener(this); btTwo.setOnClickListener(this); btThree.setOnClickListener(this); /動態(tài)注冊,在當前activity的生命周期內(nèi)運行 /*IntentFilter filter= new IntentFilter(Config.BC_ONE_ACTION); MyReiceiver myReiceiver = new MyReiceiver(); registerReceiver(myReiceiver,filter);*/ Override public void onClick(View view) Intent intent = new Intent(); switch (view.getId() case R.id.bt_one: /發(fā)送普通廣播 intent.setAction(Config.BC_ONE_ACTION); intent.putExtra(msg,這是普通廣播); sendBroadcast(intent); break; case R.id.bt_two: /有序廣播 intent.setAction(Config.BC_TWO_ACTION); intent.putExtra(msg,這是有序廣播); sendOrderedBroadcast(intent,null); /其中第二個參數(shù)是設(shè)置權(quán)限,即接收器必須具有相應(yīng)的權(quán)限才能正常接收到廣播。 break; case R.id.bt_three: /異步廣播 intent.setAction(Config.BC_THREE_ACTION); intent.putExtra(msg,這是異步廣播); sendStickyBroadcast(intent); /可以先發(fā)送 后注冊 IntentFilter filter = new IntentFilter(Config.BC_THREE_ACTION); registerReceiver(myReiceiver, filter); break; Override protected void onDestroy() super.onDestroy(); unregisterReceiver(myReiceiver); MyReceiver.javapublic class MyReiceiver extends BroadcastReceiver Override public void onReceive(Context context, Intent intent) /獲取處理的的廣播,普通廣播不能獲取處理 /true代表如果前面的接收器沒有存放數(shù)據(jù),則自動創(chuàng)建一個空的Bundle對象,false則表示如果前面的接收器如果沒有存放任何數(shù)據(jù)則返回null。 Bundle bundle= getResultExtras(true); System.out.println(接收器1接收到處理的值:+bundle.getString(msg); System.out.println(接收器1:+intent.getStringExtra(msg); MyReceiverTwo.javapublic class MyReiceiverTwo extends BroadcastReceiver Override public void onReceive(Context context, Intent intent) /Toast.makeText(context,intent.getStringExtra(msg),Toast.LENGTH_SHORT).show(); System.out.println(接收器2:+intent.getStringExtra(msg); abortBroadcast(); /截斷廣播,不讓別的接收器繼續(xù)接收,有序廣播才能成功攔截 /處理廣播 Bundle bundle = new Bundle(); bundle.putString(msg,處理過后的廣播); setResultExtras(bundle); / MyReceiverThree.javapublic class MyReiceiverThree extends BroadcastReceiver Override public void onReceive(Context context, Intent intent) /Toast.makeText(context,intent.getStringExtra(msg),Toast.LENGTH_SHORT).show(); System.out.println(接收器3:+intent.getStringExtra(msg); Config.javapublic class Config public static final String BC_ONE_ACTION = com.example.testbroadcasetwo.bcone; public static final String BC_TWO_ACTION = com.example.testbroadcasetwo.bctwo; public static final String BC_THREE_ACTION = com.example.testbroadcasetwo.bcthree;Androidmanifest.xml /異步廣播需要 一個權(quán)限 /靜態(tài)注冊,全局有效 /第一個接收器 /添加級別 /第二個接收器 /添加級別 Confiug.java 定義一個常量作為actionpublic class Config public static String BC_ONE = com.example.testbroadcast.bcone;TYPE.java 枚舉類,集合,沒用了,用來判斷是什么廣播的public enum TYPE NORMALHandleBroadcas.java p層的處理數(shù)據(jù)類public class HandleBroadcast private IShowView iShowView; private Context context; public HandleBroadcast(final IShowView iShowView, Context context) this.iShowView = iShowView; this.context = context; /必須動態(tài)注冊才能實現(xiàn)回調(diào) MyBroadcastReceiver broadcast = new MyBroadcastReceiver(); IntentFilter intentFilter = ew IntentFilter(); intentFilter.addAction(Config.BC_ONE); context.registerReceiver(broadcast, intentFilter); broadcast.setiShowView(new IShowView() Override public void updateText(String msg) iShowView.updateText(msg); ); public void sendMyBroadcast(TYPE type) Intent intent = new Intent(); switch (type) case NORMAL: /普通廣播 intent.putExtra(msg, 普通廣播發(fā)送成功); intent.setAction(Config.BC_ONE); context.sendBroadcast(intent); break; MyBroadcast.java 廣播接收器public class MyBroadcastReceiver extends BroadcastReceiver private IShowView iShowView; Override public void onReceive(Context context, Intent intent) String on = intent.getAction(); String msg = intent.getStringExtra(msg); iShowView = (MainActivity) context; if (action.equals(Config.BC_ONE) /接收到普通廣播 iShowView.updateText(msg); /回調(diào)給HandleBroadcast public void setiShowView(IShowView iShowView) this.iShowView = iShowView; IShowView.java 回調(diào)到activity更新ui的接口public interface IShowView void updateText(String msg);MainActivity.javapublicclassMainActivityextendsAppCompatActivityimplements View.OnClickListener,IShowView private Button btOne; private TextView mTvResult; /p層,處理數(shù)據(jù) private HandleBroadcast handleBroadcast; Override p
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
- 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 買賣房屋交接合同協(xié)議書
- 帶擔保的借款合同
- 水產(chǎn)品購銷合同
- 疾控中心報廢物品回收處置協(xié)議書(2篇)
- 2024-2025學(xué)年四年級語文上冊第七單元25倔強的小紅軍作業(yè)設(shè)計無答案語文S版
- 湘教版數(shù)學(xué)八年級上冊《4.3 一元一次不等式的解法》聽評課記錄2
- 初二班主任學(xué)期總結(jié)
- 項目工程師個人工作總結(jié)
- 委托放貸款協(xié)議
- 駐場獸醫(yī)聘用協(xié)議書范本
- 自愿斷絕父子關(guān)系協(xié)議書電子版
- 2023年4月自考00504藝術(shù)概論試題及答案含解析
- 美麗的大自然(教案)2023-2024學(xué)年美術(shù)一年級下冊
- 2024年低壓電工考試題庫(試題含答案)
- 成都特色民俗課件
- 花城版音樂四下-第四課-認知音樂節(jié)奏(教案)
- 寵物醫(yī)院員工手冊
- 2024年高考英語讀后續(xù)寫高分寶典專題08讀后續(xù)寫肢體動作描寫積累1(詞-句-文)講義
- 商業(yè)與公積金貸款政策
- 時政述評培訓(xùn)課件
- 2022屆高三體育特長生家長會
評論
0/150
提交評論