美文网首页回家
Android全局广播和局部广播

Android全局广播和局部广播

作者: 放羊娃华振 | 来源:发表于2021-01-04 19:36 被阅读0次

    一概述

    本地广播事件的发送和接收都在本应用,不影响其他应用也不受其他应用影响,只能被动态注册,不能静态注册,主要用法都在LocalBroadcastManager类中,而全局广播则是在Android设备上,主要注册了就都能收到。

    使用

    1、全局广播Android中使用

    布局文件

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
    
        <TextView
            android:id="@+id/tvSend"
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:layout_marginTop="20dp"
            android:background="#aeaeae"
            android:gravity="center"
            android:text="发送广播"
            android:textColor="#000000" />
    
        <TextView
            android:id="@+id/tvReceiver"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="20dp"
            android:gravity="center"
            android:text="展示接受的广播信息"
            android:textColor="#000000" />
    
    </LinearLayout>
    

    Java中发送和注册的代码

    public class NormalBroadCastActivity extends Activity {
    
        private TextView tvReceiver;
        private final String ACTION_TEST = "demos.android.stormdzh.com.androiddemos.broadcast.NormalBroadCastActivity.TestBroadcastReceiver";
        private int index =0;
        @Override
        protected void onCreate(@Nullable Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
    
            setContentView(R.layout.adapter_broadcast_normal);
    
            tvReceiver = findViewById(R.id.tvReceiver);
    
            findViewById(R.id.tvSend).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    send("参数:"+index);
                    index++;
                }
            });
    
            //注册广播
            registerBroadCast();
        }
    
        private void send(String param) {
    
            try {
                Intent intent = new Intent();
                intent.putExtra("param", param);
                intent.setAction(ACTION_TEST);
                sendBroadcast(intent);//发送标准广播
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    
        private TestBroadcastReceiver mTestBroadcastReceiver;
    
        private void registerBroadCast() {
            try {
                //注册点击事件广播
                IntentFilter filter = new IntentFilter(ACTION_TEST);
                filter.setPriority(Integer.MAX_VALUE); //设置级别
                mTestBroadcastReceiver = new TestBroadcastReceiver();
                registerReceiver(mTestBroadcastReceiver, filter);
            } catch (Exception e) {
                e.printStackTrace();
            }
    
        }
    
        public class TestBroadcastReceiver extends BroadcastReceiver {
    
            @Override
            public void onReceive(Context context, Intent intent) {
    
                if (intent == null) return;
                if (!TextUtils.equals(intent.getAction(), ACTION_TEST)) {
                    return;
                }
                final String param = intent.getStringExtra("param");
                Log.i("dzh", " 广播里面收到参数:" + param);
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        tvReceiver.setText(" 广播里面收到参数:" + param);
                    }
                });
    
            }
        }
    }
    
    
    2、本地广播Android中使用

    由于xml文件都是一致的,就不重复写了,核心是使用了LocalBroadcastManager,下面直接贴上java代码

    public class LocalBroadCastActivity extends Activity {
    
        private TextView tvReceiver;
        private final String ACTION_TEST = "demos.android.stormdzh.com.androiddemos.broadcast.NormalBroadCastActivity.TestBroadcastReceiver";
        private int index =0;
        @Override
        protected void onCreate(@Nullable Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
    
            setContentView(R.layout.adapter_broadcast_normal);
    
            tvReceiver = findViewById(R.id.tvReceiver);
    
            findViewById(R.id.tvSend).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    send("参数:"+index);
                    index++;
                }
            });
    
            //注册广播
            registerBroadCast();
        }
    
        private void send(String param) {
    
            try {
                Intent intent = new Intent();
                intent.putExtra("param", param);
                intent.setAction(ACTION_TEST);
                LocalBroadcastManager.getInstance(this).sendBroadcast(intent);//发送标准广播
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    
        private TestBroadcastReceiver mTestBroadcastReceiver;
    
        private void registerBroadCast() {
            try {
                //注册点击事件广播
                IntentFilter filter = new IntentFilter(ACTION_TEST);
                filter.setPriority(Integer.MAX_VALUE); //设置级别
                mTestBroadcastReceiver = new TestBroadcastReceiver();
                LocalBroadcastManager.getInstance(this).registerReceiver(mTestBroadcastReceiver, filter);
            } catch (Exception e) {
                e.printStackTrace();
            }
    
        }
    
        public class TestBroadcastReceiver extends BroadcastReceiver {
    
            @Override
            public void onReceive(Context context, Intent intent) {
    
                if (intent == null) return;
                if (!TextUtils.equals(intent.getAction(), ACTION_TEST)) {
                    return;
                }
                final String param = intent.getStringExtra("param");
                Log.i("dzh", " 广播里面收到参数:" + param);
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        tvReceiver.setText(" 广播里面收到参数:" + param);
                    }
                });
    
            }
        }
    }
    

    三、总结

    简单的使用就是这么多,接下来有空研究下广播的实现原理。

    相关文章

      网友评论

        本文标题:Android全局广播和局部广播

        本文链接:https://www.haomeiwen.com/subject/ffwnictx.html