BroadCastReceiver

BroadCastReceiver

什么是BroadcastReceiver ?

BroadcastReceiver是广播接收者安卓四大组件之一,应用于同一APP下多个组件之间传递数据(Activity/Fragment/Service之间传递数据)
(2)2个APP之间传递数据

BroadcastReceiver三要素

广播三要素:
(1)广播发送者 : 发送广播
(2)广播接收者(调频): 用于接收广播
(3)要处理的事情 :处理广播的相关信息, Intent有图对象

广播的生命周期&注册方式

静态注册和动态注册的区别:假如说Activity是接受者:

动态注册:

(1)广播会跟Activity的生命周期的结束而结束;
(2)自由的控制注册和取消,有很大的灵活性

静态注册:

(1)广播不会跟随Activity的生命周期的结束而结束,一直存在,即使应用程序关闭,也会被唤醒接受广播
(2)全局的广播

广播的类型

无序广播(标准广播)

Intent intent = new Intent();
     intent.setAction("com.feng.broad");
     Bundle bundle = new Bundle();
     bundle.putString("msg","大风起兮云飞扬,高薪就业创辉煌");
     intent.putExtras(bundle);
     sendBroadcast(intent);

有序广播

Intent intent1 = new Intent();
 intent1.setAction("com.feng.broad");
 //第一个参数是intent 二是权限名.
 sendOrderedBroadcast(intent1,null);

使用步骤

1.写一个MyReceiver类继承BroadcastReceiver
2.清单文件中注册并设置过滤器
3.广播者发送广播
4.广播接收者判断Action 接收数据

演示案例代码

清单文件:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
   package="com.example.day12">

   <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">
       <receiver
           android:name=".MyReceiver2"
           android:enabled="true"
           android:exported="true">
           <intent-filter android:priority="1000">
               <action android:name="com.feng.broad"></action>
           </intent-filter>

       </receiver>
       <receiver
           android:name=".MyReceiver"
           android:enabled="true"
           android:exported="true">
           <intent-filter android:priority="900">
               <action android:name="com.feng.broad" />
           </intent-filter>
       </receiver>

       <activity android:name=".MainActivity">
           <intent-filter>
               <action android:name="android.intent.action.MAIN" />

               <category android:name="android.intent.category.LAUNCHER" />
           </intent-filter>
       </activity>
   </application>

</manifest>

MainActivity:

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private Button sendId;
    private  MyReceiver myReceiver;
    private Button sendOrderId;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        sendOrderId = findViewById(R.id.send_order_id);
        sendOrderId.setOnClickListener(this);
        sendId = findViewById(R.id.send_id);
        sendId.setOnClickListener(this);
        //1,创建一个广播
//        myReceiver = new MyReceiver();
//        //添加广播过滤器
//        IntentFilter intentFilter = new IntentFilter();
//        //添加action
//        intentFilter.addAction(BroadcastConst.ACTION);
//        //注册
//        registerReceiver(myReceiver,intentFilter);


    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        switch (id) {
            case R.id.send_id:
                Intent intent = new Intent();
                intent.setAction("com.feng.broad");
                Bundle bundle = new Bundle();
                bundle.putInt("msg",123);
                intent.putExtras(bundle);
                sendBroadcast(intent);
                break;

            case R.id.send_order_id:
                Intent intent1 = new Intent();
                intent1.setAction("com.feng.broad");
                sendOrderedBroadcast(intent1,null);
                break;

            default:
                break;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        //注销广播
        unregisterReceiver(myReceiver);
    }
}

广播接收者

	public class MyReceiver extends BroadcastReceiver {
   private static final String TAG = "MyReceiver";
   @Override
   public void onReceive(Context context, Intent intent) {
       //TODO 1:获取action
       String action = intent.getAction();
       if(BroadcastConst.ACTION.equals(action)){
//            Bundle extras = intent.getExtras();
//            int msg = extras.getInt("msg");
           Log.i(TAG, "onReceive: ");
       }
   }
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值