zoukankan      html  css  js  c++  java
  • Android学习笔记-----------广播

    Android学习笔记-----------广播

    一.广播接收者的注册

      1.动态注册

        动态注册就是在代码中进行注册,调用的是registerReceiver(BroadcastReceiver broadcastReceiver, IntentFileter interFileter)方法,具体步骤如下:

        a).创建一个类,使其继承BroadcastReceiver类.重写其onReceive方法,onReceive方法中所执行的代码就是广播接收者接收到广播后所要执行的程序;

        b).在定义一个IntentFileter对象,调用其addAction(String action)方法,action就是你要接收的广播类型;

        c).调用registerReceiver(BroadcastReceiver broadcastReceiver, IntentFileter interFileter)方法注册广播接收者.

      以下是代码示例:

     1 public class MainActivity extends BaseActivity {
     2 
     3     private IntentFilter intentFilter;
     4     private MyBroadcastReciver broadcastReciver;
     5 
     6     @Override
     7     protected void onCreate(Bundle savedInstanceState) {
     8         super.onCreate(savedInstanceState);
     9         setContentView(R.layout.activity_main);
    10         // 注册广播接收者
    11         intentFilter = new IntentFilter();
    12         // 当前广播接收者可以一条com.sjj.getactivityname.MyBroadcastReciver类型的广播
    13         intentFilter.addAction("com.sjj.getactivityname.MyBroadcastReciver");
    14         broadcastReciver = new MyBroadcastReciver();
    15         registerReceiver(broadcastReciver, intentFilter);
    16     }
    17 
    18     @Override
    19     protected void onDestroy() {
    20         // 当退出时,注销广播接收者,释放内存
    21         unregisterReceiver(broadcastReciver);
    22         super.onDestroy();
    23     }
    24 
    25     class MyBroadcastReciver extends BroadcastReceiver {
    26 
    27         @Override
    28         public void onReceive(Context context, Intent intent) {
    29             // 当收到广播后,吐司
    30             Toast.makeText(context, "动态注册的广播接收者", 0).show();
    31         }
    32 
    33     }
    34 }

      2.静态注册

        静态注册是在清单文件中对广播接收者进行注册,具体方式与注册Activity的方式一样,接收的广播类型与前面动态注册的广播接收者是一致的.

    1  <receiver
    2             android:name="com.sjj.register.broadcastrevicer.MyBroadcastReciver"
    3             >
    4             <intent-filter >
    5                 <action android:name="com.sjj.getactivityname.MyBroadcastReciver"/>
    6             </intent-filter>
    7  </receiver>

     二.发送广播

      发送广播有两种形式,一种是发送标准广播,标准广播的特点在于接收此广播的广播接收者都会在同一时间接收到这个广播.

      第二种是有序广播,有序广播的特点是接收这种类型的广播接收者收到广播是有先后顺序的

      1.发送标准广播

        和Activity的跳转一样,发送广播也需要一个意图(Intent)对象,调用其setAction(String action)方法设置要发送的广播类型,在通过sendBroadcast(Intent intent)方法来发送广播

        以下是代码示例

     1 Button btSend = (Button) findViewById(R.id.bt_sendstandard);
     2         //通过按钮的点击事件来发送广播
     3         btSend.setOnClickListener(new View.OnClickListener() {
     4             
     5             @Override
     6             public void onClick(View v) {
     7                 //创建意图对象
     8                 Intent intent = new Intent();
     9                 //设置发送的广播类型
    10                 intent.setAction("com.sjj.getactivityname.MyBroadcastReciver");
    11                 //发送广播
    12                 sendBroadcast(intent);
    13             }
    14         });

       2.发送有序广播

        发送有序广播的方法调用的方法是sendOrderedBroadcast();可以设置IntentFileter的属性priority来控制广播接收者优先级;

        在广播接收者的onReceive方法中调用abortBroadcast()来中断广播的传递;

        以下是代码示例

     1 public class MainActivity extends ActionBarActivity {
     2     private IntentFilter filter;
     3     private MyBroadcastReciver broadcastReciver;
     4     private MySecondBroadcastReciver broadcastReciver2;
     5     private Button btSendBroadcasr;
     6     @Override
     7     protected void onCreate(Bundle savedInstanceState) {
     8         super.onCreate(savedInstanceState);
     9         setContentView(R.layout.activity_main);
    10         btSendBroadcasr = (Button) findViewById(R.id.bt_sendbroadcast);
    11         filter = new IntentFilter();
    12 //        //设置第一个广播接收者的优先级
    13 //        filter.setPriority(100);
    14         filter.addAction("com.sjj.sendoderbroadcast.MyBroadcast");
    15         broadcastReciver = new MyBroadcastReciver();
    16         //注册第一个广播接收者
    17         registerReceiver(broadcastReciver, filter);
    18         //设置第二个广播接收者的优先级
    19         filter.setPriority(100);
    20         filter.addAction("com.sjj.sendoderbroadcast.MyBroadcast");
    21         broadcastReciver2 = new MySecondBroadcastReciver();
    22         //注册第二个广播接收者
    23         registerReceiver(broadcastReciver2, filter);
    24         btSendBroadcasr.setOnClickListener(new View.OnClickListener() {
    25             
    26             @Override
    27             public void onClick(View v) {
    28                 Intent intent = new Intent();
    29                 intent.setAction("com.sjj.sendoderbroadcast.MyBroadcast");
    30                 //发送有序广播
    31                 sendOrderedBroadcast(intent,null);
    32             }
    33         });
    34     }
    35     
    36     @Override
    37     protected void onDestroy() {
    38         super.onDestroy();
    39         //退出程序,注销注册
    40         unregisterReceiver(broadcastReciver);
    41         unregisterReceiver(broadcastReciver2);
    42     }
    43 }

    三.本地广播

      前面的定义的广播与广播接收者都是全局的,发送的广播可以被其他程序的收到,相同也可以收到其他程序发出的广播;

      本地广播的使用范围只限于定义这个广播的应用程序;

      需要注意的是,本地广播的广播接收者注册和发送本地广播都需要广播管理者(LocalBroadcastManager)来执行.

      以下是代码示例:

      

     1 public class MainActivity extends ActionBarActivity {
     2 
     3     private Button btSendLocalBroadcast;
     4     private IntentFilter intentFilter;
     5     private LocalBroadcastManager broadcastManager;
     6     private MyBroadcast myBroadcast;
     7     @Override
     8     protected void onCreate(Bundle savedInstanceState) {
     9         super.onCreate(savedInstanceState);
    10         setContentView(R.layout.activity_main);
    11         btSendLocalBroadcast = (Button) findViewById(R.id.bt_send_localbroadcast);
    12         //初始化变量
    13         intentFilter = new IntentFilter();
    14         myBroadcast = new MyBroadcast();
    15         //初始化广播管理者
    16         broadcastManager = LocalBroadcastManager.getInstance(MainActivity.this);
    17         intentFilter.addAction("com.sjj.localBroadcast.MyBroadcast");
    18         //通过广播管理者注册广播接收者
    19         broadcastManager.registerReceiver(myBroadcast,intentFilter);
    20         //通过点击按钮来发送广播
    21         btSendLocalBroadcast.setOnClickListener(new View.OnClickListener() {
    22             
    23             @Override
    24             public void onClick(View v) {
    25                 Intent intent = new Intent("com.sjj.localBroadcast.MyBroadcast");
    26                 //通过广播管理者来发送广播
    27                 broadcastManager.sendBroadcast(intent);
    28             }
    29         });
    30     }    
    31     
    32     @Override
    33     protected void onDestroy() {
    34         // TODO Auto-generated method stub
    35         super.onDestroy();
    36         //通过广播管理者注销注册
    37         broadcastManager.unregisterReceiver(myBroadcast);
    38     }
    39 }
  • 相关阅读:
    GAMIT中遇到的错误
    bash: ./install_software: Permission denied
    xmanager无法加载远程桌面
    GMT的安装
    小总结:Gamit中常见常用命令
    动态分配指针数组(以解决)
    Gamit使用gftp软件下载数据
    Python基础(1)
    JAVA中关于多线程的理解
    JAVA 基本绘图——利用JFrame JPanel 绘制扇形
  • 原文地址:https://www.cnblogs.com/Jhope/p/5325906.html
Copyright © 2011-2022 走看看