Android使用服务,新手的好奇心

标签 android service

<分区>

我想了解有关服务的一些基本的、实用的、Android 开发人员引用页面中未明确提及的内容。

  1. 如何从尚未启动的另一个应用调用服务?

  2. 如何查询其他应用提供了哪些服务?

  3. 是否允许服务使用 UI 方法? (文档说他们特别意味着不向用户提供 UI,但他们的示例中有一个 Toast)

  4. 服务是否可重入? (如果两个应用程序调用相同的服务,它们是否会同时“服务”而不会弄乱任何局部变量)

  5. 服务是插件的制作方式吗?

  6. 我可以使用服务来扩展我已安装的应用程序的功能吗? (即我可以“安装”一个服务吗

  7. 服务能否启动正常 Activity ?他们能让他们的客户完成吗?


最佳答案

How can I call a service from another app, that has not even started?

像 Activity 这样的服务是通过使用 Intent 系统启动的。您可以使用 startService(intent); 方法启动服务。只要服务被正确配置为响应来自它自己的沙箱之外的 Intent ,这就跨越了应用程序边界。

How do I query which services are provided by other applications?

文档。我无法想象您想要在没有文档的情况下询问应用程序服务的情况,因为您不知道要传递什么才能使服务正确执行。

Are services allowed to use UI methods? (the documentation says they are specifically meant not to have UI to the user, but their example has a Toast in it)

服务可以访问应用程序上下文(因此可以发布 Toast 或启动它们自己的 Activities)。他们还可以看到在同一个沙箱中运行的应用程序,这意味着他们可以通过处理程序直接更改正在运行的 Activity 的 UI。然而,就其本质而言,服务并非设计用于与 UI 交互,尽管它是可能的。

Are services re-entrant? (if two apps call the same service, will they be 'serviced' at the same time without messing with any local variable)

服务可以用不同的标志启动(并且可以以不同的方式编程)以允许它们以异步方式服务接收到的Intents或将Intents排队执行串行地。您可以找到更多相关信息(以及您问题的其余部分 here )

Are services the way plug-ins are made?

插件?

Can I use a service to extend the functionality of my already installed app? (i.e. can I 'install' just a service

您可以将服务安装为单独的应用程序,如果它没有使用命名的 Intent,它可以捕获正在调用的 startService()。但是,此服务将位于不同的沙箱中,并且无法访问正在运行的应用程序的变量/状态。

Can services launch normal activities? Can they cause their clients to finish?

是的,如果服务在同一个沙箱中运行,他们可以看到与其一起运行的应用程序。它可以使用 startActivity(),因为它可以访问应用程序上下文,并且可以调用应用程序内部的静态方法(可能包含对 Activity 的静态访问)。

关于Android使用服务,新手的好奇心,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10795907/

相关文章:

android - 如何在数据绑定(bind)中使用 kotlin.Int?

Android Studio 制表符/缩进空间设置不起作用(卡在 2 个空格缩进处)

Android工作室gradle删除

java - 安卓地理编码器 : search by zip code returns unexpected results

asp.net-mvc - ASP.NET MVC 中的 Entity Framework 和对象上下文生存期

android - 如何实时更新 RecyclerView?

安卓 : Starting a Service from an Intent service

javascript - 适用于移动应用程序的 Node.js Web 服务

android - 如何从服务中调用主 Activity 中的函数?

android - BroadcastReceiver 发送通知并在 Activity 时更新 ui, Activity