Broadcasts —–Security considerations and best practices「建议收藏」

Broadcasts —–Security considerations and best practices「建议收藏」Herearesomesecurityconsiderationsandbestpracticesforsendingandreceivingbroadcasts:Ifyoudon’tneedtosendbroadcaststocomponentsoutsideofyourapp,thensendandreceivelocal

大家好,又见面了,我是你们的朋友全栈君。

Here are some security considerations and best practices for sending and receiving broadcasts:

  • If you don’t need to send broadcasts to components outside of your app, then send and receive local broadcasts with the LocalBroadcastManager which is available in the Support Library. The LocalBroadcastManager is much more efficient (no interprocess communication needed) and allows you to avoid thinking about any security issues related to other apps being able to receive or send your broadcasts. Local Broadcasts can be used as a general purpose pub/sub event bus in your app without any overheads of system wide broadcasts.

  • If many apps have registered to receive the same broadcast in their manifest, it can cause the system to launch a lot of apps, causing a substantial impact on both device performance and user experience. To avoid this, prefer using context registration over manifest declaration. Sometimes, the Android system itself enforces the use of context-registered receivers. For example, the CONNECTIVITY_ACTION broadcast is delivered only to context-registered receivers.

  • Do not broadcast sensitive information using an implicit intent. The information can be read by any app that registers to receive the broadcast. There are three ways to control who can receive your broadcasts:

    • You can specify a permission when sending a broadcast.
    • In Android 4.0 and higher, you can specify a package with setPackage(String) when sending a broadcast. The system restricts the broadcast to the set of apps that match the package.
    • You can send local broadcasts with LocalBroadcastManager.
  • When you register a receiver, any app can send potentially malicious broadcasts to your app’s receiver. There are three ways to limit the broadcasts that your app receives:

    • You can specify a permission when registering a broadcast receiver.
    • For manifest-declared receivers, you can set the android:exported attribute to “false” in the manifest. The receiver does not receive broadcasts from sources outside of the app.
    • You can limit yourself to only local broadcasts with LocalBroadcastManager.
  • The namespace for broadcast actions is global. Make sure that action names and other strings are written in a namespace you own, or else you may inadvertently conflict with other apps.

  • Because a receiver’s onReceive(Context, Intent) method runs on the main thread, it should execute and return quickly. If you need to perform long running work, be careful about spawning threads or starting background services because the system can kill the entire process after onReceive() returns. For more information, see Effect on process state To perform long running work, we recommend:

    • Calling goAsync() in your receiver’s onReceive() method and passing the BroadcastReceiver.PendingResult to a background thread. This keeps the broadcast active after returning from onReceive(). However, even with this approach the system expects you to finish with the broadcast very quickly (under 10 seconds). It does allow you to move work to another thread to avoid glitching the main thread.
    • Scheduling a job with the JobScheduler. For more information, see Intelligent Job Scheduling.
  • Do not start activities from broadcast receivers because the user experience is jarring; especially if there is more than one receiver. Instead, consider displaying a notification.



版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/149655.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 信息学奥赛GoC编程测试题题库

    信息学奥赛GoC编程测试题题库在线答题和答案详解请参考https://blog.csdn.net/zhengzyx2040/article/details/118632561一、单选题(15题,每题4分,满分60分)1、GoC的编译+运行的快捷键是()A、F5B、F11C、F8D、F122、GoC程序的源文件扩展名是(),经过编译后生成的可执行文件扩展名是()A、CPPEXEB、EXECPP…

    2022年6月18日
    60
  • Java中Scanner 的用法/ Scanner怎么使用

    Java中Scanner 的用法/ Scanner怎么使用Java中要想输入时便要用到Scanner首先在使用之前导入util包要想通过控制台进行输入,首先要构造一个Scanner对象,它附属于”标准输入流Scannerin=newScanner(System.in);现在我们就可以使用Scanner类的各种方法了使用Scanner读取字符串/整数/浮点数importjava.util.Scanner;//需要导入util包S…

    2022年7月20日
    14
  • mysql econnreset_Nodejs 套接字报错处理 Error: read ECONNRESET

    mysql econnreset_Nodejs 套接字报错处理 Error: read ECONNRESET错误信息:Error:readECONNRESETatTCP.onStreamRead(internal/stream_base_commons.js:162:27)出现上述情况一般是客户端关闭了socket连接导致的错误,这个错误会导致程序的异常退出解决办法:varpReq=http.request(options,function(pRes){cSock.writeHead…

    2022年6月17日
    79
  • (原创)通过ActivityManager杀死第三方应用方式[通俗易懂]

    (原创)通过ActivityManager杀死第三方应用方式[通俗易懂]ActivityManageram=(ActivityManager)context.getSystemService(Context.ACTIVITY_SERVICE);am.killBackgroundProcesses(responseAppInfo.getPackname());

    2022年9月6日
    2
  • shell高级技巧:提取vcf文件中一个contig

    shell高级技巧:提取vcf文件中一个contig这是一个很小众的需求 大部分变异检测都是基于组装质量比较高的基因组 而不是那种初步拼接的 contig 由于初步拼接的参考序列通常会有成千上万个 contig 序列 也就导致在 VCF 的头文件的 contig ID xxx length xxx 部分会有成千上万个 contig 将这个文件加载到 IGV 时 IGV 会去解析 VCF 这将会是非常缓慢的过程 最好的策略就是只提取其 ID xxx length xxx

    2025年6月2日
    0
  • asp.net(c#)的货币格式化

    asp.net(c#)的货币格式化

    2021年8月7日
    56

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号