zoukankan      html  css  js  c++  java
  • Exception android.content.ReceiverCallNotAllowedException: IntentReceiver components are not allowed to bind to services(广播中的异常)

    This question is over a year old and is definitely long and complicated and the English language is difficult to understand. However, it still probably deserves some kind of answer.

    If I understand, you are basically asking for the difference between the different Android Contextobjects. The main difference is scope or lifetime.

    The application's Context (that you can get with Activity.getApplicationContext() orContext.getApplicationContext() has a lifetime of your entire application. The application Context is created when your application (or parts of it) are started. It lives (more or less) forever.

    An activity's Context is created when that Activity is created and goes away when that activity is destroyed. If you are doing anything with the UI, you need to do that using the activity's Context so that when the activity is destroyed, those things that are related to that activity are also cleaned up.

    There are other Contexts like the one you get in a BroadcastReceiver or an AppWidgetProvider inonReceive(). This Context has a very short lifetime (it is destroyed immediately after theonReceive() method returns). Therefore you can only do limited things with this Context.

    You want to register a Receiver to listen for the broadcast Intents you are interested in. You cannot do this with the Context you get in onReceive() because that Context has a short lifetime and the listener has to be linked to something. In this case you can use the application's Context to link the listener to. This works, but you need to realize that you've created some objects that will never go away because they are always active. At least you should make sure that you clean this up when the user deletes your widget from his home screen by removing the listener using unregisterReceiver().

     

    就是要多一步 context.getApplicationContext().

  • 相关阅读:
    Retrofit/Okhttp API接口加固技术实践(上)
    浅析C#中的结构体和类
    iOS中 支付宝钱包具体解释/第三方支付 韩俊强的博客
    Java并发之volatile二
    dynamic initializer和全局变量
    二叉树转换成森林&森林变成二叉树
    这才是真正的裸眼3D!超级震撼!!
    每一个开发人员都应该有一款自己的App
    Hibernate HQL的使用
    我的Android进阶之旅------>Android 关于arm64-v8a、armeabi-v7a、armeabi、x86下的so文件兼容问题
  • 原文地址:https://www.cnblogs.com/xuewater/p/2745146.html
Copyright © 2011-2022 走看看