BroadcastReceiver(广播)的讲解与实例

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/shaochen2015821426/article/details/79699946

什么是广播:

广播是Android四大组件之一,全称BroadcastReceiver
广播分为接收方和发送方,就算没有接收方,发送方也会发送数据
可以实现跨多个页面间的数据通信通信
可以实现跨进程间的数据通信

静态注册无序广播:

实现效果:实现两个软件之间的数据通信

1.新建自定义类继承BroadcastReceiver,复写onReceive()
public class MyBroadcastReceiver  extends BroadcastReceiver {

    private  String TAG = "MyBroadcastReceiver";

    @Override
    public void onReceive(Context context, Intent intent) {
        //Toast支付宝
        Toast.makeText(context,"支付宝",Toast.LENGTH_SHORT).show();
        Log.e(TAG, "onReceive: "+"支付宝在此" );

    }
}
2.修改Manifest中注册
 //添加receive标签
        <receiver android:name=".entity.MyBroadcastReceiver">
            //添加启动方式
            <intent-filter >
                //添加action标签,启动时只有相同的action才能启动广播
                <action android:name="zhifubao"></action>
            </intent-filter>
        </receiver>
在另一个软件中发送广播
public class MainActivity extends AppCompatActivity {

    private Button button;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        button = findViewById(R.id.send_broadcast);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //创建intent对象,传入action
                Intent intent = new Intent("zhifubao");
                //发送广播
                sendBroadcast(intent);
                //sendOrderedBroadcast(intent,null);
            }
        });
    }
}

over

动态注册无序广播:

实现效果:实现两个软件之间的数据通信,效果一样,不加载图片了,不过动态注册广播,两进程都在运行,才可以实现通讯。

就是在Manifest注册的信息使用Java代码实现出来,其他地方不做修改

public class MainActivity extends AppCompatActivity {

    private IntentFilter intentFilter;
    private MyBroadcastReceiver myBroadcastReceiver;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //创建intentFilter对象,用于添加action
        intentFilter = new IntentFilter();
        intentFilter.addAction("zhifubao");
        //intentFilter.setPriority(1000);
        //创建MyBroadcastReceiver的对象
        myBroadcastReceiver = new MyBroadcastReceiver();
        //注册广播
        registerReceiver(myBroadcastReceiver, intentFilter);


    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        //注销注册
        unregisterReceiver(myBroadcastReceiver);
    }


}

over

静态注册有序广播:

实现效果:先后Toast支付宝和美团


1.比注册静态无序广播多了一步: Manifest中添加priority属性,设置优先级别,-1000—1000优先级逐渐变大

<receiver android:name=".entity.MyBroadcastReceiver">
            //添加启动方式
            <intent-filter android:priority="1000">
                //添加action标签,启动时只有相同的action才能启动广播
                <action android:name="zhifubao"></action>
            </intent-filter>
        </receiver>
2.重新新建一个项目,复写上面的代码,不过priority=”-1000” ,同样实现广播功能,Toast值为美团

over

动态注册有序广播

效果和静态注册一样

代码实现

就是在Manifest注册的信息使用Java代码实现出来

public class MainActivity extends AppCompatActivity {

    private IntentFilter intentFilter;
    private MyBroadcastReceiver myBroadcastReceiver;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //创建intentFilter对象,用于添加action
        intentFilter = new IntentFilter();
        intentFilter.addAction("zhifubao");
        //设置优先级
        intentFilter.setPriority(1000);
        //创建MyBroadcastReceiver的对象
        myBroadcastReceiver = new MyBroadcastReceiver();
        //注册广播
        registerReceiver(myBroadcastReceiver, intentFilter);


    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        //注销注册
        unregisterReceiver(myBroadcastReceiver);
    }


}

over

展开阅读全文

没有更多推荐了,返回首页