不提供用于共享 Intent 的 Android 服务

标签 android intentfilter

对于我的应用程序,我想接收从其他应用程序(如图库)共享的文件并在后台线程中处理它。以前,我使用了一个 Activity 来实现它,它运行良好,但让用户无缘无故地离开了他之前正在工作的应用程序。我尝试切换到 IntentService

在我的 Android list 中我有这个

    <service
        android:name=".TestService"
        android:label="Test Image Service"
        android:exported="true"
        android:enabled="true"
        >
        <intent-filter>
            <action android:name="android.intent.action.SEND" />
            <category android:name="android.intent.category.DEFAULT" />
            <data android:mimeType="image/*" />
        </intent-filter>
    </service>

测试服务很简单

public class TestService extends IntentService {
    public TestService() {
        super("TestServiceImageReceiver");
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        String type = intent.getType();

        Log.d("TestService", type);
    }
}

现在我将其加载到设备上,验证应用程序的其余部分是否正在运行,转到图库,共享图像,但在“共享图片通过”对话框中找不到该服务。向某些操作添加相同的 intent-filter 使其工作。

最佳答案

Now I load this on a device, verify the rest of the application is running, go to the gallery, share an image and can't find this service in the "Share Picture via" dialog

ACTION_SEND 是一个 Activity Action ,与 startActivity() 一起使用。服务将无法工作。

Previously I used an Activity for that which worked nicely but made the user leave the application he was working before for no good reason.

然后使用 Theme.NoDisplay Activity 从其 onCreate() 启动您的 IntentService,然后调用 finish().

关于不提供用于共享 Intent 的 Android 服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25098873/

相关文章:

java - 单选按钮 : Listener in XML or JAVA?

Android:如何在 ant 命令行构建期间传递版本信息?

java - 使用 Android 和外部证书进行 SSL 身份验证

android - 如何通过扫描NDEF NFC标签启动应用程序?

action - 从通知栏在我的Android应用程序中启动一个片段

android-intent - Android TextToSpeech 初始化 - 将默认 TTS 引擎设置为 "android.speech.tts"

Android ListView选择动画

android - 每当我单击按钮/JetPack/Kotlin 时,在可组合函数中推送动画以重新开始

android - Intent putExtra,NullPointerException

android - 带有 '#' 的 Intent 过滤器 pathPrefix 不起作用