Android入門之動態(tài)BroadCast的使用教程
BroadCast是什么
BroadcastReceiver就是應(yīng)用程序間的全局大喇叭,即通信的一個手段, 系統(tǒng)自己在很多時候都會發(fā)送廣播,比如電量低或者充足,剛啟動完,插入耳機,你有一條新的微信消息。。。這種都是使用BroadCast機制去實現(xiàn)的。
BroadCast分為靜態(tài)和動態(tài)BroadCast兩種。它們的區(qū)別是:
- 動態(tài)BroadCast是運行時發(fā)生的,它只有發(fā)生在APP運行后;
- 那么有一種消息如:開機監(jiān)聽、全局監(jiān)聽、無activity承載彈窗在開機時APP沒有運行前告知你天氣要變化了這種消息,這種消息就是用的“靜態(tài)BroadCast“;
我們今天先從動態(tài)BroadCast入手。從今天開始我們后面很多教程都涉及到有一樣?xùn)|西即:Android權(quán)限。
對于Android權(quán)限,這一塊知識比較零碎。而且在不同的Android版本間還有巨大的差異。主要分為:
- Android 6以前版本
- Android 6-10
- Android 10以后
每個sdk版本對權(quán)限的調(diào)用、配置還各不相同,我們就見招折招吧。
動態(tài)Broad Cast演示例子
今天我們來做的例子是一個在應(yīng)用啟動后如果網(wǎng)絡(luò)信號發(fā)生斷/連后及時廣播相應(yīng)的消息給到應(yīng)用的Activity。如下圖:
- 在模擬器里用鼠標拖著屏幕的頂部黑框下一點然后按住鼠標不動往下拖動,就會出現(xiàn)一些“常用設(shè)置”。
- 在設(shè)置里里對著網(wǎng)絡(luò)信號開關(guān)作開/關(guān)操作
- 你可以看到下方有浮動的Toast顯示“網(wǎng)絡(luò)狀態(tài)發(fā)生改變”
圍繞例子進行設(shè)計
1.我們寫動態(tài)BroadCast,需要讓一個Java類繼承自:android.content.BroadcastReceiver;
2.覆寫public void onReceive(Context context, Intent intent)方法,這個方法就是收到BroadCast后的處理邏輯所在了;
3.一定要記得把這個自己寫的receiver注冊到:AndroidManifest.xml文件中去。當(dāng)然,你可以使用Android Studio的new->other->BroadCast Receiver來生成這個Receiver,它會自動幫你把這個Receiver的類注冊到AndroidManifest.xml文件中去;
4.然后在Activity啟動使IntentFilter.addAction("android.net.conn.CONNECTIVITY_CHANGE")添加應(yīng)用對網(wǎng)絡(luò)狀態(tài)監(jiān)聽情況;
來看全代碼吧。
全代碼
Receiver在AndroidManifest中的注冊
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools"> <application android:allowBackup="true" android:dataExtractionRules="@xml/data_extraction_rules" android:fullBackupContent="@xml/backup_rules" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/Theme.DemoDynamicBroadCast" tools:targetApi="31"> <receiver android:name=".SimpleBroadCast" android:enabled="true" android:exported="true"></receiver> <activity android:name=".MainActivity" android:exported="true"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> <meta-data android:name="android.app.lib_name" android:value="" /> </activity> </application> </manifest>
Receiver-SimpleBroadCast
package org.mk.android.demo.broadcast; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.util.Log; import android.widget.Toast; public class SimpleBroadCast extends BroadcastReceiver { private final static String TAG="DemoDynamicBroadCast"; @Override public void onReceive(Context context, Intent intent) { Toast.makeText(context,"網(wǎng)絡(luò)狀態(tài)發(fā)生改變~",Toast.LENGTH_SHORT).show(); Log.i(TAG,">>>>>>網(wǎng)絡(luò)狀態(tài)發(fā)生改變"); } }
這個Receiver很簡單,就是在onReceive方法中輸出一個Toast。
再來看activity端。
MainActivity
package org.mk.android.demo.broadcast; import androidx.appcompat.app.AppCompatActivity; import android.content.IntentFilter; import android.os.Bundle; import android.view.View; import android.widget.Button; public class MainActivity extends AppCompatActivity { SimpleBroadCast simpleBroadCast; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //核心部分代碼: simpleBroadCast = new SimpleBroadCast(); IntentFilter itFilter = new IntentFilter(); itFilter.addAction("android.net.conn.CONNECTIVITY_CHANGE"); registerReceiver(simpleBroadCast, itFilter); } //別忘了將廣播取消掉哦~ @Override protected void onDestroy() { super.onDestroy(); unregisterReceiver(simpleBroadCast); } }
運行后的效果
以下是運行后的效果
到此這篇關(guān)于Android入門之動態(tài)BroadCast的使用教程的文章就介紹到這了,更多相關(guān)Android動態(tài)BroadCast內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Adapter實現(xiàn)ListView帶多選框等狀態(tài)的自定義控件的注意事項
Android本身為ListView提供了幾個方便的Adapter,比如ArrayAdapter、SimpleCurrentAdapter等等,接下來介紹自定義Adapter實現(xiàn)ListView帶多選框等狀態(tài)控件的注意事項,感興趣的朋友可以詳細了解下,或許對你有所幫助2013-01-01android 獲取手機內(nèi)存及 內(nèi)存可用空間的方法
下面小編就為大家?guī)硪黄猘ndroid 獲取手機內(nèi)存及SD卡內(nèi)存可用空間的方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-03-03