四大应用组件之广播

 自定义广播:
  1. 广播静态注册

    如何静态注册:
1.1.  定义BroadcastReceiver广播接收者

/**
 * 广播一旦onReceive 就是成为垃圾对象
 * 下一次接收,系统重新创建广播
 */
public class MyReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        String name= intent.getStringExtra("name");
        Log.e("denganzhi1","MyReceiver:"+name);

        // 中断广播
        abortBroadcast();

    }
}

1.2.  AndroidManifest.xml  注册广播

    <!-- 广播的静态注册,应用安装就会注册,但是不会创建对象,接收广播才创建对象
          接收以后就死了,重新接收,重新创建-->
        <receiver android:name=".MyReceiver">
            <intent-filter android:priority="2">
                <action android:name="com.example.administrator.myapplication.action"></action>
            </intent-filter>
        </receiver>

1.3. 发送广播:

   Intent intent=new Intent("com.example.administrator.myapplication.action");
        intent.putExtra("name","xiaoming");
        sendBroadcast(intent);

    如何动态注册:

1.2.  在Activity中动态注册 registerReceiver

MyReceiver myReceiver=null;   
 protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        // 动态注册在Activity中注册, 调用方法就去注册
        // 广播接收器 绑定到宿主 Activity中,
        // 所以Activity死了时候,必须unregisterReceiver, 否则会报错
if(myReceiver==null){
    myReceiver=new MyReceiver();
    IntentFilter intentFilter=new IntentFilter("com.example.administrator.myapplication.action");
    registerReceiver(myReceiver,intentFilter);
}else{
    Log.e("denganzhi","已经注册");
}
    }

    @Override
    protected void onDestroy() {
        if(myReceiver!=null){
            unregisterReceiver(myReceiver);
        }
        super.onDestroy();
    }

  2. 静态注册和动态注册区别:

区别: 广播的静态注册,应用安装就会注册,但是不会创建对象,接收广播才创建对象
          接收以后就死了,重新接收,重新创建
    
        动态注册: 执行方法以后才注册,activity退出的时候接除注册对象回收
 
   场景区分: 如果该广播只是服务于某一个Activitry,那么使用动态注册
   如果服务区应用,那么使用静态注册

3.   有序广播l 

 特点:有序广播,按照优先级来接收,并且可以中断广播 
   AndroidManifest.xm,  通过android:priority 执行接收优先级

<!-- 广播的静态注册,应用安装就会注册,但是不会创建对象,接收广播才创建对象
          接收以后就死了,重新接收,重新创建-->
        <receiver android:name=".MyReceiver">
            <!-- 最大是2147483647  Integr.Max
             值越大,越先调用 -->
            <intent-filter android:priority="2">
                <action android:name="com.example.administrator.myapplication.action"></action>
            </intent-filter>
        </receiver>

        <receiver android:name=".MyReceiver1">
            <intent-filter android:priority="1">
                <action android:name="com.example.administrator.myapplication.action"></action>
            </intent-filter>
        </receiver>

  发送有序广播:

  public void sendbroad(View view) {
        Intent intent=new Intent("com.example.administrator.myapplication.action");
        intent.putExtra("name","xiaoming");
        // 发送有序广播,第一个参数接受者权限
        sendOrderedBroadcast(intent,null);
    }

   中断广播,那么后面的无法接收:

/**
 * 广播一旦onReceive 就是成为垃圾对象
 * 下一次接收,系统重新创建广播
 */
public class MyReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        String name= intent.getStringExtra("name");
        Log.e("denganzhi1","MyReceiver:"+name);

        // 中断广播
        abortBroadcast();

    }
}
发布了75 篇原创文章 · 获赞 68 · 访问量 11万+

猜你喜欢

转载自blog.csdn.net/dreams_deng/article/details/104713874
今日推荐