Android学习随笔(11)------广播BroadcastReceiver

淡淡的烟草味﹌ 2022-06-06 14:30 559阅读 1赞

学习流程来自《第一行代码》(第二版)
为了便于进行系统级别的消息通知,Android引入了一套广播消息机制。Android中的每个应用程序都可以对自己感兴趣的广播进行注册,可以是系统的广播消息,也可以是其他应用的。
广播接收器中不允许开启线程,扮演一种打开其他程序组件的角色。(启动服务或创建状态栏通知)
广播类型 :

  1. 标准广播(Normal broadcasts) 完全异步执行的广播。在广播发出后,所有广播接收器几乎会在同一时刻接受到这条广播消息,没有先后顺序,无法被截断。
  2. 有序广播(Ordered broadcasts) 同步执行的广播。在广播发出后,同一个时刻只有一个广播接收器能收到,这个广播接收器执行完后,广播才会继续传递,接受器有优先级,若有一个广播接收器截断了此广播,之后的接收器无法收到此广播。

接收系统广播

有一些应用会在手机网络关闭时,对用户发出一条反馈网络断开的消息。根据这个场景我们实现一个广播接收器来监听网络变化。

动态注册监听

在代码中注册广播被称为动态注册。

  1. public class MainActivity extends AppCompatActivity {
  2. private IntentFilter intentFilter;
  3. private NetworkChangeReceiver networkChangeReceiver; // 动态注册监听
  4. @Override
  5. protected void onCreate(Bundle savedInstanceState) {
  6. super.onCreate(savedInstanceState);
  7. setContentView(R.layout.activity_main);
  8. intentFilter = new IntentFilter();
  9. intentFilter.addAction("android.net.conn.CONNECTIVITY_CHANGE"); //广播器想要监听什么广播,就添加相应的action
  10. networkChangeReceiver = new NetworkChangeReceiver();
  11. registerReceiver(networkChangeReceiver,intentFilter);
  12. }
  13. @Override
  14. protected void onDestroy() {
  15. super.onDestroy();
  16. unregisterReceiver(networkChangeReceiver); // 动态注册的广播接收器一定要取消注册, 只能在程序启动后才能接受到广播
  17. }
  18. class NetworkChangeReceiver extends BroadcastReceiver {
  19. @Override
  20. public void onReceive(Context context, Intent intent) { // 每当网络发生变化,得到执行
  21. Toast.makeText(context, "network changes", Toast.LENGTH_SHORT).show();
  22. }
  23. }
  24. }
  1. 创建一个内部类NetworkChangeReceiver继承BroadcastReceiver。
  2. 重写父类的onReceiver()方法,添加一个Toast提醒我们网络发生了变化。
  3. 创建一个IntentFilter,添加我们想要监听的活动”android.net.conn.CONNECTIVITY_CHANGE”网络变化。
  4. 注册广播接收器registerReceiver()传入我们自定义的广播以及intentFilter。
    添加访问系统网络的权限 :

动态注册的广播一定要取消,不然这个广播一直被引用,Activity不能被释放,占用手机内存。

运行程序会在注册完成时收到一条广播,关闭/打开流量开关,有Toast消息提醒。
Exler
Exler
Exler
还可以判断网络是打开还是关闭:

  1. class NetworkChangeReceiver extends BroadcastReceiver {
  2. @Override
  3. public void onReceive(Context context, Intent intent) { // 每当网络发生变化,得到执行
  4. ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(context.CONNECTIVITY_SERVICE); // 系统服务类,专门用于管理网络连接的
  5. NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
  6. if (networkInfo != null && networkInfo.isAvailable()) { // 判断当前是否有网络
  7. Toast.makeText(context, "network is available", Toast.LENGTH_SHORT).show();
  8. } else {
  9. Toast.makeText(context, "network is unavailable", Toast.LENGTH_SHORT).show();
  10. }
  11. }
  12. }

利用ConnectivityManager来获取网络状态,并且进行判断。
Exler
Exler
Exler

静态注册

利用静态注册来实现对开机启动的监听。
利用Android Studio提供的快捷方式来创建广播接收器。在包名上右击 New-Other-Broadcast Receiver。
Exler
Exler
Exported表示是否允许这个广播接收器接收本程序以外的广播,
Enabled表示是否启用这个广播接收器。

  1. public class BootCompleteReceiver extends BroadcastReceiver {
  2. @Override
  3. public void onReceive(Context context, Intent intent) {
  4. Toast.makeText(context, "Boot Complete", Toast.LENGTH_LONG).show();
  5. }
  6. }

在接收器的onReceiver()方法中完成对开机成功的提示。
Android Studio会自动帮我们在AndroidManifest.xml中注册静态监听器。但还是要手动添加权限

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.yezhou.com.broadcasttest">
  3. <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
  4. <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
  5. <!-- 静态广播接收器注册 -->
  6. <!-- 添加权限 -->
  7. <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">
  8. <activity android:name=".MainActivity">
  9. <intent-filter>
  10. <action android:name="android.intent.action.MAIN" />
  11. <category android:name="android.intent.category.LAUNCHER" />
  12. </intent-filter>
  13. </activity>
  14. <receiver android:name=".BootCompleteReceiver" android:enabled="true" android:exported="true">
  15. <intent-filter>
  16. <action android:name="android.intent.action.BOOT_COMPLETED" /> <!-- 系统启动完成后会发出一条android.intent.action.BOOT_COMPLETED的广播,监听系统开机广播 -->
  17. </intent-filter>
  18. </receiver>
  19. </application>
  20. </manifest>

Exler
在onReceiver()方法中不宜写太复杂的代码。

自定义广播

主要体验一下标准广播与有序广播的区别。

标准广播

在一个应用里面实现对广播的发送和接收。
新建一个MyBroadcastReceiver

  1. public class MainActivity extends AppCompatActivity {
  2. @Override
  3. protected void onCreate(Bundle savedInstanceState) {
  4. super.onCreate(savedInstanceState);
  5. setContentView(R.layout.activity_main);
  6. Button button = (Button) findViewById(R.id.button);
  7. button.setOnClickListener(new View.OnClickListener() {
  8. @Override
  9. public void onClick(View view) {
  10. Intent intent = new Intent("com.example.boardcasttest.MY_BROADCAST");
  11. sendBroadcast(intent);
  12. }
  13. });
  14. }
  15. }

AndroidManifest.xml :

  1. <receiver android:name=".MyBoardcastReceiver" android:enabled="true" android:exported="true">
  2. <intent-filter>
  3. <action android:name="com.example.broadcasttest.MY_BROADCAST" />
  4. </intent-filter>
  5. </receiver>

添加要监听的广播名。
添加一个按钮用于发送广播 :

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical">
  3. <Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Send Broadcast" />
  4. </LinearLayout>
  5. public class MainActivity extends AppCompatActivity {
  6. @Override
  7. protected void onCreate(Bundle savedInstanceState) {
  8. super.onCreate(savedInstanceState);
  9. setContentView(R.layout.activity_main);
  10. Button button = (Button) findViewById(R.id.button);
  11. button.setOnClickListener(new View.OnClickListener() {
  12. @Override
  13. public void onClick(View view) {
  14. Intent intent = new Intent("com.example.broadcasttest.MY_BROADCAST");
  15. sendBroadcast(intent);
  16. }
  17. });
  18. }
  19. }

点击按钮,发送一个”com.example.broadcasttest.MY_BROADCAST”的广播。
Exler

不同应用接收同一个标准广播

新建一个BroadcastTest2项目,在其中创建一个广播接收器用于接收上一个项目发出的”com.example.broadcasttest.MY_BROADCAST”广播。
新建一个BroadcastRecevier :

  1. public class AnotherBroadcastReceiver extends BroadcastReceiver {
  2. @Override
  3. public void onReceive(Context context, Intent intent) {
  4. Toast.makeText(context, "received in AnotherBroadcastReceiver", Toast.LENGTH_SHORT).show();
  5. }
  6. }

添加要监听的action :

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.administrator.mybroadcasttest2">
  3. <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">
  4. <activity android:name=".MainActivity">
  5. <intent-filter>
  6. <action android:name="android.intent.action.MAIN" />
  7. <category android:name="android.intent.category.LAUNCHER" />
  8. </intent-filter>
  9. </activity>
  10. <receiver android:name=".AnotherBroadcastReceiver" android:enabled="true" android:exported="true">
  11. <intent-filter>
  12. <action android:name="com.example.broadcasttest.MY_BROADCAST" />
  13. </intent-filter>
  14. </receiver>
  15. </application>
  16. </manifest>

运行将此项目安装在设备上,再返回上一个项目中点击button。
Exler
Exler

发送有序广播

在发送广播的项目BroadcastTest中修改发送广播的方式 :

  1. Button button = (Button) findViewById(R.id.button);
  2. button.setOnClickListener(new View.OnClickListener() {
  3. @Override
  4. public void onClick(View view) {
  5. Intent intent = new Intent("com.example.broadcasttest.MY_BROADCAST");
  6. sendOrderedBroadcast(intent, null); // 与权限相关字符串
  7. }
  8. });

sendOrderedBroadcast()方法发送有序广播。
在AndroidManifest.xml利用android:priority属性来设置广播接收器的优先级 :

  1. <receiver android:name=".MyBoardcastReceiver" android:enabled="true" android:exported="true">
  2. <intent-filter android:priority="100">
  3. <action android:name="com.example.broadcasttest.MY_BROADCAST" />
  4. </intent-filter>
  5. </receiver>

既然是有序广播,那么就可以设置是否允许广播继续传递

  1. public class MyBoardcastReceiver extends BroadcastReceiver {
  2. @Override
  3. public void onReceive(Context context, Intent intent) {
  4. Toast.makeText(context, "received in MyBroadcastReceiver", Toast.LENGTH_SHORT).show();
  5. abortBroadcast();
  6. }
  7. }

利用abortBroadcast()方法设置截断这条广播。

运行,只有MyBroadcastRecevier接收器有提示。

本地广播

利用本地广播机制,使得广播只能够在应用程序内部进行传递,并且只能接收来自本应用的广播。

  1. public class MainActivity extends AppCompatActivity {
  2. private LocalReceiver localReceiver;
  3. private LocalBroadcastManager localBroadcastManager;
  4. @Override
  5. protected void onCreate(Bundle savedInstanceState) {
  6. super.onCreate(savedInstanceState);
  7. setContentView(R.layout.activity_main);
  8. localBroadcastManager = LocalBroadcastManager.getInstance(this); // 获取实例
  9. Button button = (Button) findViewById(R.id.button);
  10. button.setOnClickListener(new View.OnClickListener() {
  11. @Override
  12. public void onClick(View view) {
  13. Intent intent = new Intent("com.example.broadcasttest.MY_BROADCAST");
  14. localBroadcastManager.sendBroadcast(intent); // 发送本地广播
  15. }
  16. });
  17. IntentFilter intentFilter = new IntentFilter();
  18. intentFilter = new IntentFilter();
  19. intentFilter.addAction("com.example.broadcasttest.MY_BROADCAST");
  20. localReceiver = new LocalReceiver();
  21. localBroadcastManager.registerReceiver(localReceiver,intentFilter); // 本地广播管理器 注册接收器 接收器 接收内容
  22. }
  23. @Override
  24. protected void onDestroy() {
  25. super.onDestroy();
  26. localBroadcastManager.unregisterReceiver(localReceiver);
  27. }
  28. class LocalReceiver extends BroadcastReceiver { // 本地广播
  29. @Override
  30. public void onReceive(Context context, Intent intent) {
  31. Toast.makeText(context, "received local broadcast", Toast.LENGTH_SHORT).show();
  32. }
  33. }
  34. }

代码其实与动态注册相似。
Exler
本地广播无法通过静态注册来接收。

实践 :强制下线功能

强制下线需要关闭所有活动,创建ActivityCollector :

  1. public class ActivityCollector { // 活动管理
  2. public static List<Activity> activities = new ArrayList<>();
  3. public static void removeActivity(Activity activity) {
  4. activities.remove(activity);
  5. }
  6. public static void addActivity(Activity activity) {
  7. activities.add(activity);
  8. }
  9. public static void finishActivity() {
  10. for (Activity activity : activities) {
  11. if (!activity.isFinishing())
  12. activity.finish();
  13. }
  14. }
  15. }

创建BaseActivity作为所有活动的父类 :

  1. public class BaseActivity extends AppCompatActivity {
  2. private ForceOfflineReceiver receiver;
  3. @Override
  4. protected void onCreate(Bundle savedInstanceState) {
  5. super.onCreate(savedInstanceState);
  6. ActivityCollector.addActivity(this);
  7. }
  8. @Override
  9. protected void onResume() { // 始终需要保证处于栈顶的活动才能接收到这条广播
  10. super.onResume();
  11. IntentFilter intentFilter = new IntentFilter();
  12. intentFilter.addAction("com.example.yezhou.com.broadcastbestpractice.FOR_OFFLINE");
  13. receiver = new ForceOfflineReceiver();
  14. registerReceiver(receiver,intentFilter);
  15. }
  16. @Override
  17. protected void onPause() {
  18. super.onPause();
  19. if (receiver != null) {
  20. unregisterReceiver(receiver);
  21. receiver = null;
  22. }
  23. }
  24. @Override
  25. protected void onDestroy() {
  26. super.onDestroy();
  27. ActivityCollector.removeActivity(this);
  28. }
  29. class ForceOfflineReceiver extends BroadcastReceiver {
  30. @Override
  31. public void onReceive(final Context context, Intent intent) {
  32. AlertDialog.Builder builder = new AlertDialog.Builder(context); // 创建一个对话框
  33. builder.setTitle("warning"); // 设置对话框标题
  34. builder.setMessage("You are force to be offline.Please try to login again."); // 设置对话框内容
  35. builder.setCancelable(false); // 是否可以取消
  36. builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
  37. @Override
  38. public void onClick(DialogInterface dialogInterface, int i) {
  39. ActivityCollector.finishActivity(); // 销毁所有活动
  40. Intent intent = new Intent(context, LoginActivity.class);
  41. context.startActivity(intent); // 重新启动LoginActivity
  42. }
  43. });
  44. builder.show();
  45. }
  46. }
  47. }

创建一个动态广播,(静态注册的广播接收器是没有办法弹出一个对话框的,出于安全原因的考虑,Android是不允许Activity或Dialog凭空出现的,一个Activity的启动必须要建立在另一个Activity的基础之上,也就是以此形成的返回栈。而Dialog则必须在一个Activity上面弹出(除非是System Alert类型的Dialog))
登录界面的代码activty_login.xml :

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" >
  3. <LinearLayout android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="60dp">
  4. <TextView android:layout_width="90dp" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:textSize="18sp" android:text="Account:"/>
  5. <EditText android:id="@+id/account" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:layout_gravity="center_vertical"/>
  6. </LinearLayout>
  7. <LinearLayout android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="60dp">
  8. <TextView android:layout_width="90dp" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:textSize="18sp" android:text="Password:"/>
  9. <EditText android:id="@+id/password" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:layout_gravity="center_vertical" android:inputType="textPassword"/>
  10. </LinearLayout>
  11. <Button android:id="@+id/login" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:text="Login"/>
  12. </LinearLayout>
  13. public class LoginActivity extends BaseActivity { // 登录界面代码
  14. private EditText accountEdit;
  15. private EditText passwordEdit;
  16. private Button login;
  17. @Override
  18. protected void onCreate(Bundle savedInstanceState) {
  19. super.onCreate(savedInstanceState);
  20. setContentView(R.layout.activity_login);
  21. accountEdit = (EditText) findViewById(R.id.account);
  22. passwordEdit = (EditText) findViewById(R.id.password);
  23. login = (Button) findViewById(R.id.login);
  24. login.setOnClickListener(new View.OnClickListener() {
  25. @Override
  26. public void onClick(View view) {
  27. String account = accountEdit.getText().toString();
  28. String password = passwordEdit.getText().toString(); // account:admin password:123456
  29. if (account.equals("admin") && password.equals("123456")) {
  30. Intent intent = new Intent(LoginActivity.this, MainActivity.class);
  31. startActivity(intent);
  32. finish(); // 登录成功此Activity无用销毁
  33. } else {
  34. Toast.makeText(LoginActivity.this, "account or password is invalid", Toast.LENGTH_SHORT).show();
  35. }
  36. }
  37. });
  38. }
  39. }

登录之后进入的主界面 activity_main.xml :

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" >
  3. <Button android:id="@+id/force_offline" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:text="Send force offline broadcast" />
  4. </LinearLayout>

点击按钮即进行强制下线操作。
MainActivity.java :

  1. public class MainActivity extends BaseActivity { //发出强制下线广播
  2. @Override
  3. protected void onCreate(Bundle savedInstanceState) {
  4. super.onCreate(savedInstanceState);
  5. setContentView(R.layout.activity_main);
  6. Button forceOffLine = (Button) findViewById(R.id.force_offline);
  7. forceOffLine.setOnClickListener(new View.OnClickListener() {
  8. @Override
  9. public void onClick(View view) {
  10. Intent intent = new Intent("com.example.yezhou.com.broadcastbestpractice.FOR_OFFLINE");
  11. sendBroadcast(intent);
  12. }
  13. });
  14. }
  15. }

把login活动设置为启动项 :

  1. <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">
  2. <activity android:name=".MainActivity">
  3. </activity>
  4. <activity android:name=".LoginActivity">
  5. <intent-filter>
  6. <action android:name="android.intent.action.MAIN" />
  7. <category android:name="android.intent.category.LAUNCHER" />
  8. </intent-filter>
  9. </activity>
  10. </application>

登录界面,输入admin 密码123456,登录 :
Exler
点击button :
Exler
跳出对话框,点击OK:
Exler

最终返回login界面。


此博文为个人学习笔记,仅供个人学习使用,希望对大家有帮助。

发表评论

表情:
评论列表 (有 0 条评论,559人围观)

还没有评论,来说两句吧...

相关阅读

    相关 Android广播BroadcastReceiver

    Android 系统里定义了各种各样的广播,如电池的使用状态,电话的接收和短信的接收,开机启动都会产生一个广播。当然用户也可以自定义自己的广播。 既然说到广播,那么必定有一个