Finding out if an intent is available
Sometimes you want to find if an application has registered for a certain intent. For example you want to check if a certain receiver is available and if you enable some functionality in your app.
某些时候你想要知道某个AP是否有注册了一个明确的intent,比如说你想要检查某个receiver是否存在,然后根据是否存在来这个receiver来在你的AP里面enable某些功能
This can be done via checking the PackageManager. The following code checks if an intent exists. You can check via this method for intent and change your application behavior accordingly for example disable or hide menu items.
我们可以通过PackageManager来check它,下面的代码检测了一个intent是否存在.
public boolean isIntentAvailable(Context context, String action) { final PackageManager packageManager = context.getPackageManager(); final Intent intent = new Intent(action); List<ResolveInfo> resolveInfo = packageManager.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY); if (resolveInfo.size() > 0) { return true; } return false; }这篇文章有点类似于之前写的判断某个AP是否在系统中存在(PackageManager与PackageInfo)
翻译自;http://www.vogella.de/articles/AndroidIntent/article.html
本文介绍了一种通过PackageManager检查特定Intent是否被应用注册的方法,并提供了一个示例代码片段,该片段可用于确定应用程序中是否存在特定的Intent接收器。
2474

被折叠的 条评论
为什么被折叠?



