戲說江湖靜如水昌粤,游蕩江湖才有情既绕。我就是江湖中的一個(gè)戲子啄刹。
俗話說,入行先入門岸更。作為一名android學(xué)習(xí)者鸵膏,四大組件是android中的核心組件膊升,豈有不學(xué)之理怎炊。然而,本人才疏學(xué)淺廓译,敘述略有不當(dāng)之處评肆,敬請(qǐng)諒解。
BroadcastReceiver在android開發(fā)中使用的頻率也是蠻高的非区,主要的作用來接受系統(tǒng)廣播或者用戶廣播瓜挽,然后進(jìn)行相應(yīng)的處理,很像我們生活中的廣播征绸。
-
類別
- 普通廣播
對(duì)于多個(gè)接受者來說是完全異步的久橙,每個(gè)接受者無需等待就可以接受到廣播,并且接受者之間不會(huì)有任何影響管怠。這種廣播淆衷,接受者無法終止廣播,也無法阻止其他接受者的接收動(dòng)作渤弛。
- 有序廣播
有序廣播每次只能發(fā)送到優(yōu)先級(jí)較高的接接收者哪里祝拯,然后由優(yōu)先級(jí)高的接收者傳播到優(yōu)先級(jí)低的接收者那里,并且優(yōu)先級(jí)高的接收者能夠終止這個(gè)廣播她肯。
需要定義權(quán)限佳头,在必要時(shí)來終止權(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)注冊
<action android:name="andorid.intent.action.BROADCAST"/>
- 動(dòng)態(tài)注冊
IntentFilter filter = new IntentFilter(); filter.addAction("android.intent.action.BROADCAST");
相關(guān)案例使用
- 開機(jī)啟動(dòng)服務(wù)
- 注冊廣播來接收開機(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);
}
}
- 實(shí)現(xiàn)自己的服務(wù)
public class BootCompleteService extends Service{
@Override
public IBinder onBind(Intent intent) {
return null;
}
}
- 配置相關(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"/>
- 聲明使用權(quán)限
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
- 網(wǎng)絡(luò)狀態(tài)變化
- 接收網(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;
}
}
- 配置相關(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>
- 聲明使用權(quán)限
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
- 電量變化
- 接收電量信息
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
}
}
- 配置相關(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>
- 聲明使用權(quán)限
<uses-permission android:name="android.permission.BATTERY_STATS"/>
- 立即獲得電量信息
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;
上述很簡單的描述了BroadcastReceiver的使用方法和相關(guān)的案例晴氨,你是否又get到了呢康嘉?