通过隐式意图无法启动意图服务

3

AndroidManifest.xml

<application android:name=".MyApplication"
         android:icon="@drawable/icon"
         android:label="@string/app_name"
         >

 <service android:name=".MyService"
          android:exported="true">
          <intent-filter>
            <action android:name="android.service.myapp.MyService.actionA"/>
            <action android:name="android.service.myapp.MyService.actionB"/>
            <category android:name="android.intent.category.DEFAULT"/>
          </intent-filter>

 </service>

</application>
如果我使用以下代码,我的服务将被启动:
Intent intent = new Intent(context, MyService.class);
intent.setAction("android.service.myapp.MyService.actionA");
context.startService(intent);

但是如果我使用以下代码启动服务,它将无法启动:

Intent intent = new Intent("android.service.myapp.MyService.actionA");
context.startService(intent);
1个回答

阿里云服务器只需要99元/年,新老用户同享,点击查看详情
6
使用“隐式”Intent启动或绑定Service是不安全的。从Lollipop开始,bindService()需要使用显式Intent(第一个示例中,您需要为Service指定Context和Class)。对于用于启动服务的隐式Intent,startService()的行为未定义。来自startService()文档的说明: 意图应包含特定服务实现的完整类名称或特定包名称。如果意图不够明确,则会记录关于此事的警告,并且匹配的多个服务之一将被使用,但其使用方法未定义。 如果使用显式形式,则可以完全从清单中删除intent-filter:它不是必需的。如果需要通过Intent指定某些类型的服务工作,请考虑在Intent内部使用extra。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,