Specifying a SEND (sharing) intent filter for a Service

六眼飞鱼酱① 提交于 2020-01-02 02:28:10

问题


I am trying to filter and handle intents with android.intent.action.SEND actions in one of my Services. I wrote the following in my AndroidManifest.xml:

<service 
    android:name=".app.ScreamerService"
    android:label="@string/app_name" >
    <intent-filter>
        <action android:name="android.intent.action.SEND"/>
        <category android:name="android.intent.category.DEFAULT"/>
        <data android:mimeType="*/*"/>
    </intent-filter>                                                                    
</service>

Now, the problem is that I don't see my application in the list of "share via" options when, for instance, trying to share a web page from the browser or a contact from the list of contacts. If, however, I move the intent filters to the main <activity> tag (instead of the <service>), my application name and icon do appear on the list of "share via" options.

What am I doing wrong here? Can't a SEND action be directed to a Service?


回答1:


I am trying to filter and handle intents with android.intent.action.SEND actions in one of my services.

ACTION_SEND is an activity action and therefore cannot be picked up by services or broadcast receivers.

Now, the problem is that I don't see my application in the list of "share via" options when, for instance,

That is because it is not an activity.

Can't a SEND action be directed to a Service?

Things that appear in a chooser (e.g.,. for ACTION_SEND) must be activities. Your activity is welcome to communicate with a service, though.




回答2:


This should work but:

Try with a broadcast receiver first to get the intent and launch your service.

if that doesn't work:

Use a dummy activity with no layout. (make the theme translucent and call finish right after you handle the intent).

Also your mime type means that you are handling every single file type. is that what you need? i think you should make it more precise in the selection. you will get negative feedback if someone tries to use it with a type you don't support. my suggestion is to test types and add them one by one as you are confident you can handle them.



来源:https://stackoverflow.com/questions/11084840/specifying-a-send-sharing-intent-filter-for-a-service

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!