Android 學習之《第一行程式碼》第二版 筆記(十一)詳解廣播機制(一)
阿新 • • 發佈:2018-11-10
一、廣播機制簡介
1. 四大元件之一
2. Android 提供了一套完整的API,允許應用程式自由地傳送和接收廣播。
A. 傳送廣播藉助Intent
B. 接收廣播藉助廣播接收器(Broadcast Receiver)
3. 廣播型別:
A. 標準廣播: 完全非同步執行,廣播發出後,所有的廣播接收器幾乎會同時接收到該廣播訊息。效率高但無法被截斷。
B. 有序廣播: 同步執行,廣播發出後,同一時刻僅有一個廣播接收器接收到該廣播訊息,相應邏輯執行完畢後,廣播才會繼續傳遞。優先順序高的廣播接收器優先接收,可被截斷。
二、接收系統廣播
廣播接收器對感興趣的的廣播進行註冊,方可在目標廣播發出後接收。註冊廣播的方式有兩種:動態註冊
動態註冊的廣播接收器一定要取消註冊。
靜態註冊可讓程式未啟動時便接收廣播訊息。
1. 動態註冊監聽網路變化 && 靜態註冊實現開機啟動
如何建立一個廣播接收器?新建一個類繼承BroadcastReceiver,並重寫父類的onReceive()方法,邏輯可在該方法中處理。
A. 介面效果:(效果僅展示網路變化的)
B. 程式碼:
AndroidMainfest.xml
<?xml version="1.0" encoding="utf-8"?>
< manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.thinkpad.broadcasttest">
<!--訪問網路狀態的許可權需要在AndroidManifest.xml宣告一下-->
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<!--監聽系統開機廣播也需要許可權-->
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
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>
<!--靜態的廣播接收器一定要在AndroidManifest.xml中註冊-->
<receiver
android:name=".BootCompleteReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<!--Android系統在啟動完成後會發出一條值為:android.intent.action.BOOT_COMPLETED的廣播-->
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
</application>
</manifest>
MainActivity.java(監聽網路變化)
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
private IntentFilter mIntentFilter;
private NetworkChangeReceiver mNetworkChangeReceiver; //廣播接收器物件
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mIntentFilter = new IntentFilter(); //建立一個IntentFilter例項
//當網路狀態發生變化時,系統發出的正是值為此的廣播
mIntentFilter.addAction("android.net.conn.CONNECTIVITY_CHANGE");
mNetworkChangeReceiver = new NetworkChangeReceiver();//建立一個廣播接收器的例項
registerReceiver(mNetworkChangeReceiver,mIntentFilter);//註冊廣播,傳入廣播接收器例項和IntentFilter
}
@Override
protected void onDestroy() {
super.onDestroy();
//動態註冊的廣播接收器一定要取消註冊。
unregisterReceiver(mNetworkChangeReceiver);
}
//內部類NetworkChangeReceiver繼承自BroadcastReceiver,用於建立廣播接收器
class NetworkChangeReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
//通過getSystemService()獲得ConnectivityManager,這個類是一個系統服務類,專門用於管理網路連線的。
ConnectivityManager connectivityManager = (ConnectivityManager)
getSystemService(Context.CONNECTIVITY_SERVICE);
//通過getActiveNetworkInfo()可以得到NetworkInfo的例項。
//訪問網路狀態的許可權需要在AndroidManifest.xml宣告一下。
NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
//通過NetworkInfo的isAvailable()判斷當前是否有網路
if(networkInfo != null && networkInfo.isAvailable()){
Toast.makeText(context,"network is available",Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(context,"network is unavailable",Toast.LENGTH_SHORT).show();
}
}
}
}
BootCompleteReceiver.java(監聽開機啟動)
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;
public class BootCompleteReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context,"Boot Complete",Toast.LENGTH_SHORT).show();
}
}
三、傳送自定義廣播
書上使用的安卓版本過低,目前的高版本安卓會出現一定的bug
解決方法:BUG解決參考部落格
1. 傳送標準廣播
A. 效果圖
B. 廣播接收器(MyBroadcastReceiver.java)
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;
public class MyBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context,"接收到了自定義的廣播",Toast.LENGTH_SHORT).show();
}
}
C. AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.thinkpad.broadcasttest2">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
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=".MyBroadcastReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="com.example.broadcasttest.MY_BROADCAST"/>
</intent-filter>
</receiver>
</application>
</manifest>
D. 按鈕佈局(activity_main.xml)
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.thinkpad.broadcasttest2.MainActivity">
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="傳送廣播訊息"/>
</android.support.constraint.ConstraintLayout>
E. 主活動(MainActivity.java)
import android.content.ComponentName;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = (Button)findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent("com.example.broadcasttest.MY_BROADCAST");
intent.setComponent(new ComponentName("com.example.thinkpad.broadcasttest2",
"com.example.thinkpad.broadcasttest2.MyBroadcastReceiver"));
//該方法將廣播發送出去
sendBroadcast(intent);
}
});
}
}
2. 傳送有序廣播
在專案一中點選按鈕傳送廣播,專案二接收並截斷,使專案三接收不到廣播。
僅展示專案二程式碼:
A. 廣播接收器(MyBroadcastReceiver.java)
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;
public class MyBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context,"BroadcastReceiver接收到了廣播!",Toast.LENGTH_SHORT).show();
//呼叫abortBroadcast();方法將廣播截斷
abortBroadcast();
}
}
B. AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.thinkpad.broadcastordertest">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
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>
<!--靜態註冊廣播接收器,並設定其action-->
<receiver
android:name=".MyBroadcastReceiver"
android:enabled="true"
android:exported="true">
<!--給廣播接收器設定優先順序,優先順序高的先接收到廣播-->
<intent-filter android:priority="100">
<action android:name="com.example.broadcastordertest.MY_BROADCAST"/>
</intent-filter>
</receiver>
</application>
</manifest>
C. 傳送廣播的專案一(MainActivity.java)
將sendBroadcast(intent);方法改成
sendOrderBroadcast(intent,null);//該方法帶有兩個引數,第一個為Intent,第二個為與許可權有關的字串。
四、使用本地廣播
1. 使用本地廣播的優勢:
A. 正在傳送的廣播不會離開本程式,安全。
B. 其他程式的廣播送不到本程式內,安全。
C. 比系統全域性廣播更加高效。
2. 本地廣播是無法通過靜態註冊的方式來接收的
3. 程式碼(MainActivity.java)
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.support.v4.content.LocalBroadcastManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
//使用LocalBroadcastManager來對廣播進行管理,該類提供了傳送廣播和註冊廣播接收器的方法。
private IntentFilter mIntentFilter;
private LocalBroadcastManager mLocalBroadcastManager;
private LocalReceiver mLocalReceiver;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mLocalBroadcastManager = LocalBroadcastManager.getInstance(this);//獲取例項
Button button = (Button)findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent("com.example.thinkpad.localbroadcast.LOCAL_BROADCAST");
mLocalBroadcastManager.sendBroadcast(intent);//傳送本地廣播
}
});
mIntentFilter = new IntentFilter();
mIntentFilter.addAction("com.example.thinkpad.localbroadcast.LOCAL_BROADCAST");
mLocalReceiver = new LocalReceiver();
mLocalBroadcastManager.registerReceiver(mLocalReceiver,mIntentFilter);//註冊本地廣播監聽器
}
@Override
protected void onDestroy() {
super.onDestroy();
mLocalBroadcastManager.unregisterReceiver(mLocalReceiver);//動態註冊的廣播接收器一定要取消註冊
}
//內部類 廣播接收器
class LocalReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context,"接收到了本地廣播",Toast.LENGTH_SHORT).show();
}
}
}
整理學習自郭霖大佬的《第一行程式碼》
目前小白一名,持續學習Android中,如有錯誤請批評指正!