【问题标题】:What is the use of intent-filter in the manifest for a service?服务清单中意图过滤器的用途是什么?
【发布时间】:2019-11-27 12:44:43
【问题描述】:

我在火车上,突然想到一件事。

如果允许我如下声明我的服务:

<service
    android:name=".service.MyService"
    android:enabled="true"
    android:exported="true">
    <intent-filter>
        <action android:name="INTENT_SAMPLE" />
        <category android:name="android.intent.category.DEFAULT" />
        <data android:mimeType="text/plain" />
    </intent-filter>
</service>

我知道执行以下操作会导致IllegalArgumentException: Service Intent must be explicit 错误。

Intent intent = new Intent("INTENT_SAMPLE");
intent.setType("text/plain");
startService(intent);

那么,这种情况下intent-filter有什么用,如果有的话?

【问题讨论】:

  • 只有当您希望第三方应用程序与该组件通信时,该组件上才具有 。您似乎陷入了假设所有事情都需要一个 的陷阱——实际上,您很少需要一个。显式 Intent 是指在 Intent 本身中指定要与之对话的组件,通常使用将 Java Class 对象作为第二个参数的构造函数。那,而不是隐式 Intent 和 ,应该用于应用程序本地的组件
  • 曾经有过。在 Android 的历史上,这个例外相对较新。安全问题迫使 Android 设计者通过手术移除原始功能。您正在查看手术留下的疤痕。

标签: android intentfilter


【解决方案1】:
   try this code....
   <code>
    //MainActivity.class
    Intent serviceIntent = new Intent(context,MyService.class);
    serviceIntent.setAction("INTENT_SAMPLE");
    startService(serviceIntent);

  // Myservice.class
  public int onStartCommand(Intent intent, int flags, int startId) 
  {
      string action=intent.getAction();
      if(action.equals("your action"))
      {...}
  }
  </code>

【讨论】:

    【解决方案2】:

    如果您想使用服务执行不同的操作,那么声明一个意图过滤器将帮助您的服务匹配您想要执行的不同操作。

    它允许您将相同的服务用于不同的操作,而不是创建两个单独的服务。

    但是,为服务声明意图过滤器并不是一种好的做法,这是文档必须说的:

    注意:为确保您的应用安全,请始终使用明确的意图 启动服务时,不要为您的服务声明意图过滤器 服务。使用隐式意图启动服务是一种安全措施 危险,因为您无法确定什么服务会响应 意图,用户无法看到启动了哪个服务。

    我在 SO 上找到了很多关于相同的答案:

    希望你能明白。

    谢谢。

    【讨论】:

      【解决方案3】:

      意图的定义是 Intent 是一个简单的消息对象,用于在 android 组件(如活动、内容提供者、广播接收器和服务)之间进行通信。 Intent 还用于在 Activity 之间传输数据。

      并且有两种类型的意图 隐含意图 明确的意图

      隐式意图是使用两个定义您要为不同活动执行的操作

      Explicit Intent 用于启动特定的应用组件,例如应用中的特定活动或服务

      这就是为什么您必须在清单中的意图过滤器中定义意图,以便系统知道它是哪种类型的意图

      您可以参考此链接以进一步详细了解该主题:- https://developer.android.com/guide/components/intents-filters https://developer.android.com/guide/topics/manifest/intent-filter-element

      【讨论】:

        【解决方案4】:

        正如其他答案中提到的,意图过滤器可帮助您的服务对要执行的操作类型进行分类。因此,例如,您可以尝试如下启动服务:

        Intent intent = new Intent(this, MyService.class);
        intent.setAction("INTENT_SAMPLE");
        startService(intent);
        

        然后在MyService 类中,像这样检查动作:

        @Override
        public int onStartCommand(Intent intent, int flags, int startId) {
            if("INTENT_SAMPLE".equals(intent.getAction())) {
                // do INTENT_SAMPLE action here
            }
            else {
                // do non INTENT_SAMPLE action here
            }
        }
        

        由于https://developer.android.com/guide/components/intents-filters#Types 不推荐服务的意图过滤器,因此从清单中删除该操作。要达到与上述相同的结果,只需在您的意图中添加一个额外的内容,如下所示:

        Intent intent = new Intent(this, MyService.class);
        intent.putExtra("INTENT_SAMPLE", true);
        startService(intent);
        
        

        然后在MyService 类中,像这样检查动作:

        @Override
        public int onStartCommand(Intent intent, int flags, int startId) {
            if(intent.getBooleanExtra("INTENT_SAMPLE", false)) {
                // do INTENT_SAMPLE action here
            }
            else {
                // do non INTENT_SAMPLE action here
            }
        }
        

        干杯!

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-02-24
          • 1970-01-01
          相关资源
          最近更新 更多