Android activity not getting broadcast from local service

前端 未结 2 1086
被撕碎了的回忆
被撕碎了的回忆 2021-01-22 11:19

From the examples this looked straightforward. Maybe you can show me what I did wrong. I can\'t get an activity to receive a broadcast sent from a local service.

I have

相关标签:
2条回答
  • 2021-01-22 11:29

    Eureka! I found it! The problem is that I supplied a data URI in my broadcast intent. The Android intent matching rules get a little complicated. If you supply a data URI, then your intent filter must specify a matching MIME type.

    Unfortunately, although the Android documentation says that the data type can be inferred from the data URI, apparently Android doesn't know that a file://.../example.jpg is an image. So this doesn't work:

    intentFilter.addDataType("image/*");
    

    However, instead of specifying a type, I can specify a scheme that I accept:

    intentFilter.addDataScheme("file");
    

    That works! It's a little rough---and a little artificial to restrict my broadcasts to file: URIs, but as that's all I'm using for the moment, it works.

    Note that apparently I could manually specify the MIME type in the intent when I broadcast it, but that's too much trouble for now, as I'm downloading images from Picasa so I already know that they are images (and don't care the specific MIME type). And if it gets too much trouble, I could ditch the whole setData() thing altogether and set an extra---but of course I want to do things the Right Way.

    0 讨论(0)
  • 2021-01-22 11:35

    have you included your receiver in your activity's manifest?

    <receiver
      android:name=".YourReceiver">
      <intent-filter>
        <action
          android:name="intent_name"></action>
      </intent-filter>
    </receiver>
    
    0 讨论(0)
提交回复
热议问题