Android 之LocalBroadcastManager

我们先来看一个发送带权限广播的例子:
我们新建两个工程,一个代表本地广播接收者,一个代表远程广播接受者,在本地我们注册两个广播,在远程我们注册一个广播,我们在本地发送广播:

  public void send(View view){
        Intent intent = new Intent("android.intent.action.send");
        intent.putExtra("msg", "hello receiver.");
        sendBroadcast(intent,"android.permission.look");
    }

我们发送的是一个带有权限的广播,这个权限是自定义的。

然后我们在清单文件中自定义这个权限:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.broadcasedemo">
    <permission android:name="android.permission.look"
        android:protectionLevel="normal"
        />
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name"
            android:theme="@style/AppTheme.NoActionBar">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <receiver android:name=".LocalReceiver">
            <intent-filter android:priority="10">
                <action android:name="android.intent.action.send"/>
                <category android:name="android.intent.category.DEFAULT"/>
            </intent-filter>
        </receiver>
        <receiver android:name=".LocalReceiverTwo">
            <intent-filter android:priority="10">
                <action android:name="android.intent.action.send"/>
                <category android:name="android.intent.category.DEFAULT"/>
            </intent-filter>
        </receiver>
    </application>
    //必须写,否则无法接收到广播
    <uses-permission android:name="android.permission.look"/>
</manifest>

在清单文件中我们注册了两个广播,然后自定义了权限,当我们发送广播的时候,就可以接收到了。
同时我们在远程用同样的方式注册一个,看能不能接收到广播

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.remotereceiver">
    <permission android:name="android.permission.look"
        android:protectionLevel="normal"
        />
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name"
            android:theme="@style/AppTheme.NoActionBar">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <receiver android:name=".RemoteReiceiver">
            <intent-filter >
                <action android:name="android.intent.action.send"/>
                <category android:name="android.intent.category.DEFAULT"/>
            </intent-filter>
        </receiver>
    </application>
    <uses-permission android:name="android.permission.look"/>
</manifest>

注意这里也要自定义权限,否则也是无法接受的。

在Android系统中,BroadcastReceiver的设计初衷就是从全局考虑的,可以方便应用程序和系统、应用程序之间、应用程序内的通信,所以对单个应用程序而言BroadcastReceiver是存在安全性问题的,相应问题及解决如下:

1 .当应用程序发送某个广播时系统会将发送的Intent与系统中所有注册的BroadcastReceiver的IntentFilter进行匹配,若匹配成功则执行相应的onReceive函数。可以通过类似sendBroadcast(Intent, String)的接口在发送广播时指定接收者必须具备的permission。或通过Intent.setPackage设置广播仅对某个程序有效。

2 . 当应用程序注册了某个广播时,即便设置了IntentFilter还是会接收到来自其他应用程序的广播进行匹配判断。对于动态注册的广播可以通过类似registerReceiver(BroadcastReceiver, IntentFilter, String, android.os.Handler)的接口指定发送者必须具备的permission,对于静态注册的广播可以通过android:exported=”false”属性表示接收者对外部应用程序不可用,即不接受来自外部的广播。

上面两个问题其实都可以通过LocalBroadcastManager来解决:

Android v4 兼容包提供android.support.v4.content.LocalBroadcastManager工具类,帮助大家在自己的进程内进行局部广播发送与注册,使用它比直接通过sendBroadcast(Intent)发送系统全局广播有以下几点好处。
1 . 因广播数据在本应用范围内传播,你不用担心隐私数据泄露的问题。
2 . 不用担心别的应用伪造广播,造成安全隐患。
3 . 相比在系统内发送全局广播,它更高效。

下面看看如何使用:

public class MainActivity extends AppCompatActivity {
    private BroadcastReceiver receiver;
    private LocalBroadcastManager manager;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initReceiver();
    }
    private void initReceiver() {
        IntentFilter filter = new IntentFilter();
        filter.addAction("test");
        receiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                if(intent.getAction().equals("test")){
                    Toast.makeText(MainActivity.this, "接收到了广播", Toast.LENGTH_SHORT).show();
                }
            }
        };
        manager = LocalBroadcastManager.getInstance(this);
        manager.registerReceiver(receiver,filter);
    }
    public void send(View view){
        Intent intent = new Intent("test");
        manager.sendBroadcast(intent);
    }
    /**
     * 和正常广播一样,也需要在onDestory中反注册掉
     */
    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(receiver);
    }
}

使用LocalBroadcastManager 要注意以下几点:
1.只能通过代码注册
2.最后要取消注册
3.发送广播需要使用LocalBroadcastManager 的sendBroadcast方法,否则无法接收到广播。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值