Android 广播

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android 广播相关的知识,希望对你有一定的参考价值。

广播:

  • 有序广播

    • 发出广播之后同一时刻只有一个广播接收器能够接收到广播,优先级高的先接受,可以截断正在传递的广播,后面的就无法收到广播消息了.
  • 动态创建广播:

    • 创建一个类继承BroadcastReceiver,重写onReceive,有广播来到就会执行onReceive方法,动态注册的一定要取消注册.

      public class MainActivity extends Activity {
      private IntentFilter intentFilter;
      private NetworkChangeReceiver networkChangeReceiver;
      
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
          intentFilter = new IntentFilter();
          intentFilter.addAction("android.net.conn.CONNECTIVITY_CHANGE");// 当系统发生变化时发出的是值为括号内的广播,想要监听什么广播就添加相应的action
          networkChangeReceiver = new NetworkChangeReceiver();
          registerReceiver(networkChangeReceiver, intentFilter);// 注册广播
      }
      
      protected void onDestroy() {
          super.onDestroy();
          // 动态注册的一定要取消注册
          unregisterReceiver(networkChangeReceiver);
      }
      
      class NetworkChangeReceiver extends BroadcastReceiver {
          // 每当网络发生变化就会执行此方法
          public void onReceive(Context arg0, Intent arg1) {
              //系统服务类,用来管理网络连接
              ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
              NetworkInfo networkInfo = connectivityManager
                      .getActiveNetworkInfo();
      
              if (networkInfo != null && networkInfo.isAvailable()) {
                  Toast.makeText(MainActivity.this, "网络可用", 0).show();
              }else{
                  Toast.makeText(MainActivity.this, "网络不可用", 0).show();
              }
      
          }
      
      }
      

      }

  • 静态注册实现开机自启

    • 创建一个类,继承自BroadcastReceiver,收到广播就会执行onReceive方法

          public class BootCompleteReceiver extends BroadcastReceiver {
      
      public void onReceive(Context arg0, Intent arg1) {
          Toast.makeText(arg0, "开机",0).show();
      
      }}
      
    • 清单文件注册:

      <receiver android:name=".BootCompleteReceiver" >
      <intent-filter>
          <action android:name="android.intent.action.BOOT_COMPLETED" />
      </intent-filter>
      

       

  • 发送自定义广播:

    • 首先构建一个intent对象,然后调用Context的sendBroadcast()方法把广播发送出去,这样所有监听这条广播的都会接受到消息

      Intent intent = new Intent("com.example.broadcasttest.MY_BROADCAST");
      sendBroadcast(intent);
      
  • 发送有序广播

    Intent intent = new Intent("com.example.broadcasttest.MY_BROADCAST");
    sendOrderedBroadcast(intent, null);
    
    • 在注册清单的intent-filter中添加android:priority="100"属性来设置优先级,优先级越高越先收到.可以使用abortBroadcast();将广播截断.
  • 本地广播

    • 在应用程序的内部进行传递,并且广播接收器只能接受来自本应用的广播

      public class MainActivity extends Activity {
          private LocalReceiver localReceiver;
          private IntentFilter intentFilter;
          private LocalBroadcastManager localBroadcastManager;
      
          protected void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.activity_main);
              localBroadcastManager = LocalBroadcastManager.getInstance(this);
      
              intentFilter = new IntentFilter();
              intentFilter.addAction("com.example.broadcasttest.LOCAL_BROADCAST");
              localReceiver = new LocalReceiver();
              localBroadcastManager.registerReceiver(localReceiver, intentFilter);
              //注册本地广播监听器
          }
      
      
          public void click(View v){
              Intent intent = new Intent("com.example.broadcasttest.LOCAL_BROADCAST");
              localBroadcastManager.sendBroadcast(intent);//发送本地广播
      
          }
          class LocalReceiver extends BroadcastReceiver {
      
              public void onReceive(Context context, Intent arg1) {
                  Toast.makeText(context, "received local broadcast",
                          Toast.LENGTH_SHORT).show();
              }
      
          }
          protected void onDestroy() {
              super.onDestroy();
              localBroadcastManager.unregisterReceiver(localReceiver);
          }
      
      }
      

- 系统级别的弹窗

    AlertDialog.Builder builder = new AlertDialog.Builder(context);
    builder.setTitle("强制下线");
    builder.setMessage("在他处登录,你被强制下线");
    builder.setCancelable(false);//设置不可取消
    builder.setPositiveButton("OK",new OnClickListener() {

        public void onClick(DialogInterface arg0, int arg1) {
            //TODO
        }
    });
    AlertDialog dialog = builder.create();
    dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
    dialog.show();

以上是关于Android 广播的主要内容,如果未能解决你的问题,请参考以下文章

Android启动广播时怎样往广播中传递参数

android 之广播机制

android有序广播和无序广播的区别

Android高效安全的本地广播LocalBroadcast完全解析

android app中的服务为啥接收不到广播

Android广播阻塞、延迟问题