戲說(shuō)Android四大組件之BroadcastReceiver

戲說(shuō)江湖靜如水,游蕩江湖才有情。我就是江湖中的一個(gè)戲子。

俗話說(shuō),入行先入門。作為一名android學(xué)習(xí)者,四大組件是android中的核心組件,豈有不學(xué)之理。然而,本人才疏學(xué)淺,敘述略有不當(dāng)之處,敬請(qǐng)諒解。


BroadcastReceiver在android開(kāi)發(fā)中使用的頻率也是蠻高的,主要的作用來(lái)接受系統(tǒng)廣播或者用戶廣播,然后進(jìn)行相應(yīng)的處理,很像我們生活中的廣播。

  • 類別

    • 普通廣播

    對(duì)于多個(gè)接受者來(lái)說(shuō)是完全異步的,每個(gè)接受者無(wú)需等待就可以接受到廣播,并且接受者之間不會(huì)有任何影響。這種廣播,接受者無(wú)法終止廣播,也無(wú)法阻止其他接受者的接收動(dòng)作。

    • 有序廣播

    有序廣播每次只能發(fā)送到優(yōu)先級(jí)較高的接接收者哪里,然后由優(yōu)先級(jí)高的接收者傳播到優(yōu)先級(jí)低的接收者那里,并且優(yōu)先級(jí)高的接收者能夠終止這個(gè)廣播。

    需要定義權(quán)限,在必要時(shí)來(lái)終止權(quán)限

    <permission android:protectionLevel="normal"
             android:name="scott.permission.BROADCAST_PERMISSION"/>
    

    然后聲明使用了該權(quán)限

    <uses-permission android:name="scott.permission.BROADCAST_PERMISSION"
    
  • 使用方法

    • 繼承android.content.BroadcastReceiver
    • 實(shí)現(xiàn)其onReceiver方法
    • 靜態(tài)注冊(cè)
    <action android:name="andorid.intent.action.BROADCAST"/>
    
    • 動(dòng)態(tài)注冊(cè)
    IntentFilter filter = new IntentFilter();
    filter.addAction("android.intent.action.BROADCAST");
    

相關(guān)案例使用

  • 開(kāi)機(jī)啟動(dòng)服務(wù)
  1. 注冊(cè)廣播來(lái)接收開(kāi)機(jī)啟動(dòng)廣播,并啟動(dòng)自己的服務(wù)
public class BootCompleteReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        Intent bootIntent = new Intent(context,BootCompleteService.class);
        context.startService(bootIntent);
    }
}
  1. 實(shí)現(xiàn)自己的服務(wù)
public class BootCompleteService extends Service{
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }
}
  1. 配置相關(guān)信息
 <receiver android:name=".BootCompleteReceiver">
    <intent-filter>
        <action android:name="android.intent.action.BOOT_COMPLETED"/>
        <category android:name="android.intent.category.DEFAULT"/>
    </intent-filter>
</receiver>
<service android:name=".BootCompleteService"/>
  1. 聲明使用權(quán)限
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
  • 網(wǎng)絡(luò)狀態(tài)變化
  1. 接收網(wǎng)絡(luò)改變廣播
public class NetworkStateReciver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        if(isNetworkAvailable(context)){
            Toast.makeText(context,"network is disconnect",Toast.LENGTH_SHORT);
        }
    }

    public static boolean isNetworkAvailable(Context context){
        ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo[] info = cm.getAllNetworkInfo();
        if (info != null) {
            for (int i = 0;i < info.length;i++){
                if(info[i].getState() == NetworkInfo.State.CONNECTED) {
                    return true;
                }
            }
        }
        return false;
    }
}
  1. 配置相關(guān)信息
<receiver android:name=".NetworkStateReciver">
    <intent-filter>
        <action android:name="android.net.conn.CONNECTIVITY_CHANGE"/>
        <category android:name="android.intent.category.DEFAULT"/>
    </intent-filter>
</receiver>
  1. 聲明使用權(quán)限
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
  • 電量變化
  1. 接收電量信息
public class BatteryChangeReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        int currentLevel = intent.getIntExtra(BatteryManager.EXTRA_LEVEL,0);
        int total = intent.getIntExtra(BatteryManager.EXTRA_SCALE,1);
        int precent = currentLevel * 100 / total;
        //TODO:you want to do
    }
}
  1. 配置相關(guān)信息
<receiver android:name=".BatteryChangeReceiver">
    <intent-filter>
        <action android:name="android.intent.action.BATTERY_CHANGED"/>
        <category android:name="android.intent.category.DEFAULT"/>
    </intent-filter>
</receiver>
  1. 聲明使用權(quán)限
<uses-permission android:name="android.permission.BATTERY_STATS"/>
  1. 立即獲得電量信息
Intent batteryIntent = getApplicationContext().registerReceiver(null,new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
int currentLevel = batteryIntent.getIntExtra(BatteryManager.EXTRA_LEVEL,0);
int totalLevel = batteryIntent.getIntExtra(BatteryManager.EXTRA_SCALE,1);
int precent = currentLevel * 100 / totalLevel;

上述很簡(jiǎn)單的描述了BroadcastReceiver的使用方法和相關(guān)的案例,你是否又get到了呢?

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容