对于我的应用程序,我想接收从其他应用程序(如图库)共享的文件并在后台线程中处理它。以前,我使用了一个 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/