如何以编程方式检查服务是否在AndroidManifest.xml中声明? [英] How to programmatically check if a service is declared in AndroidManifest.xml?

查看:281
本文介绍了如何以编程方式检查服务是否在AndroidManifest.xml中声明?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在写,对正在使用的其他开发人员,包括在他们的项目中的服务库。因此,我没有在AndroidManifest.xml中控制。我解释什么在文档做的,但尽管如此,一个共同的问题是,人们忽视添加相应的<服务/>标记他们的清单,或增加其在错误的地方。

I'm writing a library that provides a Service that is used by other developers by including it in their project. As such, I don't have control over the AndroidManifest.xml. I explain what to do in the docs, but nevertheless a common problem is that people neglect to add the appropriate <service/> tag to their manifest, or add it in the wrong place.

现在,当我的库调用startService而服务未在清单中声明,这种情况发生的唯一的事情就是ActivityManager记录警告。我想抛出一个异常,当发生这种情况,使开发人员知道如何解决它。如何检测清单是否实际声明此服务?

Right now, when my library calls startService while the service isn't declared in the manifest, the only thing that happens is that ActivityManager logs a warning. I would like to throw an exception when this happens so that developers know how to fix it. How can I detect whether the manifest actually declares this Service?

推荐答案

如果没有这样的服务中找到,否则返回组件名的傻,我忽视这一点,但startService类()返回null。所以这对我来说最简单的方法。它看起来像使用软件包管理系统也将正常工作。

Kind of silly for me to overlook this, but startService() returns null if there is no such service found, and returns the ComponentName otherwise. So that's the simplest way for me. It looks like using the PackageManager would also work.

这篇关于如何以编程方式检查服务是否在AndroidManifest.xml中声明?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆