Android LocalBroadcastManager解决BroadcastReceiver安全问题及自定义权限

在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    相比在系统内发送全局广播,它更高效。

其使用方法也和正常注册广播类似:

LocalBroadcastManager mLocalBroadcastManager;    
  BroadcastReceiver mReceiver;    
  
  
 IntentFilter filter = new IntentFilter();    
 filter.addAction("test");    
  
 mReceiver = new BroadcastReceiver() {    
            @Override    
            public void onReceive(Context context, Intent intent) {    
                if (intent.getAction().equals("test")) {    
                    //Do Something  
                }   
            }    
        };  
 mLocalBroadcastManager.registerReceiver(mReceiver, filter);  

//当然,和正常广播一样,也要在对应的生命周期中反注册掉:  
 
@Override  
protected void onDestroy() {  
  
   super.onDestroy();  
  
   mLocalBroadcastManager.unregisterReceiver(mReceiver);  
  
}

转自:http://blog.csdn.net/t12x3456/article/details/9256609

自定义权限

实例(微信)

<permission android:protectionLevel="signature" android:name="com.tencent.mm.oauth.permission.SEND"/>

<permission android:protectionLevel="signature" android:name="com.tencent.mm.plugin.permission.WRITE"/>

<permission android:protectionLevel="signature" android:name="com.tencent.mm.plugin.permission.READ"/>

<permission android:protectionLevel="signature" android:name="com.tencent.mm.permission.MM_MESSAGE"/>

<permission android:protectionLevel="signature" android:name="com.tencent.mm.permission.GET_QRCODE_INFO"/>

<uses-permission android:name="com.tencent.mm.oauth.permission.SEND"/>

<uses-permission android:name="com.tencent.mm.plugin.permission.READ"/>

<uses-permission android:name="com.tencent.mm.plugin.permission.WRITE"/>

<uses-permission android:name="com.tencent.mm.plugin.permission.SEND"/>

<uses-permission android:name="com.tencent.mm.permission.MM_MESSAGE"/>

<receiver android:name="com.tencent.mm.booter.MMReceivers$AlarmReceiver" android:process=":push"/>

<receiver android:name="com.tencent.mm.jni.platformcomm.Alarm" android:process=":push" android:exported="false"/>

<provider android:name="com.tencent.mm.plugin.base.stub.MMPluginProvider" android:writePermission="com.tencent.mm.plugin.permission.WRITE" android:authorities="com.tencent.mm.sdk.plugin.provider"/>

<provider android:name="com.tencent.mm.plugin.base.stub.RContactDBProvider" android:writePermission="com.tencent.mm.plugin.permission.WRITE" android:authorities="com.tencent.mm.sdk.contact.provider" android:readPermission="com.tencent.mm.plugin.permission.READ"/>

<provider android:name="com.tencent.mm.plugin.base.stub.RConversationDBProvider" android:writePermission="com.tencent.mm.plugin.permission.WRITE" android:authorities="com.tencent.mm.sdk.conversation.provider" android:readPermission="com.tencent.mm.plugin.permission.READ"/>

<provider android:name="com.tencent.mm.plugin.base.stub.RMsgInfoDBProvider" android:writePermission="com.tencent.mm.plugin.permission.WRITE" android:authorities="com.tencent.mm.sdk.msginfo.provider" android:readPermission="com.tencent.mm.plugin.permission.READ"/>

<activity android:name="com.tencent.mm.plugin.base.stub.WXEntryActivity" android:theme="@style/Theme.Transparent" android:launchMode="singleTop" android:excludeFromRecents="true" android:exported="true"/>

-<activity android:name="com.tencent.mm.ui.qrcode.GetQRCodeInfoUI" android:theme="@style/Theme.Transparent" android:configChanges="keyboardHidden|orientation" android:exported="false" android:permission="com.tencent.mm.permission.GET_QRCODE_INFO">


-<intent-filter>

<action android:name="android.intent.action.VIEW"/>

<category android:name="android.intent.category.DEFAULT"/>

<category android:name="android.intent.category.BROWSABLE"/>

<data android:scheme="http" android:pathPrefix="/r/" android:host="weixin.qq.com"/>

</intent-filter>


-<intent-filter>

<action android:name="android.intent.action.VIEW"/>

<category android:name="android.intent.category.DEFAULT"/>

<category android:name="android.intent.category.BROWSABLE"/>

<data android:scheme="weixin" android:host="qr"/>

</intent-filter>

</activity>



-<receiver android:name="com.tencent.mm.plugin.shoot.remote.RemoteNetSceneMgr$RespReceiver" android:process=":tools" android:permission="com.tencent.mm.permission.SHOOT_STUB_REQ_RESP">


-<intent-filter>

<action android:name="com.tencent.mm.action.SHOOT_STUB_RESP"/>

</intent-filter>

</receiver>


-<receiver android:name="com.tencent.mm.plugin.shootstub.communicate.ReqReceiver" android:permission="com.tencent.mm.permission.SHOOT_STUB_REQ_RESP">


-<intent-filter>

<action android:name="com.tencent.mm.action.SHOOT_STUB_REQ"/>

</intent-filter>

</receiver>

<activity android:name="com.tencent.mm.pluginsdk.ui.tools.FileExplorerUI" android:configChanges="keyboardHidden|orientation"/>

<service android:name="com.tencent.mm.pluginsdk.model.downloader.FileDownloadManger$FileDownloadService"/>


-<receiver android:name="com.tencent.mm.pluginsdk.model.downloader.FileDownloadManger$FileDownloadReceiver">


-<intent-filter>

<action android:name="android.intent.action.DOWNLOAD_COMPLETE"/>

<action android:name="android.intent.action.DOWNLOAD_NOTIFICATION_CLICKED"/>

</intent-filter>

</receiver>

<activity android:name="com.tencent.mm.ui.tools.MMTextInputUI" android:windowSoftInputMode="stateVisible|adjustPan" android:configChanges="keyboardHidden|orientation"/>


-<receiver android:name="com.tencent.mm.compatible.audio.MMRemoteController$Receiver">


-<intent-filter>

<action android:name="android.intent.action.MEDIA_BUTTON"/>

</intent-filter>

</receiver>

</application>

<uses-permission android:name="android.permission.GET_ACCOUNTS"/>

<uses-permission android:name="android.permission.MANAGE_ACCOUNTS"/>

<uses-permission android:name="android.permission.AUTHENTICATE_ACCOUNTS"/>

<uses-permission android:name="android.permission.READ_SYNC_SETTINGS"/>

<uses-permission android:name="android.permission.WRITE_SYNC_SETTINGS"/>

<uses-permission android:name="com.tencent.mm.gwallet.BILLING"/>

<uses-permission android:name="com.android.vending.BILLING"/>

<uses-permission android:name="com.tencent.mm.ext.permission.READ"/>

<uses-permission android:name="com.tencent.mm.ext.permission.WRITE"/>

<uses-permission android:name="com.tencent.mm.permission.SHOOT_STUB_REQ_RESP"/>

<permission android:protectionLevel="signature" android:name="com.tencent.mm.permission.SHOOT_STUB_REQ_RESP"/>

<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>

<uses-permission android:name="android.permission.READ_PHONE_STATE"/>

    原文作者:知道分子
    原文地址: https://blog.csdn.net/Hknock/article/details/13507575
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞