android queryIntentActivities如何寻找出某个intent是否可用

来源:互联网 发布:android网络编程 编辑:程序博客网 时间:2024/06/07 04:03

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是否存在.

[java] view plaincopyprint?
  1. public boolean isIntentAvailable(Context context, String action) {  
  2.     final PackageManager packageManager = context.getPackageManager();  
  3.     final Intent intent = new Intent(action);  
  4.     List<ResolveInfo> resolveInfo =  
  5.             packageManager.queryIntentActivities(intent,  
  6.                     PackageManager.MATCH_DEFAULT_ONLY);  
  7.    if (resolveInfo.size() > 0) {  
  8.         return true;  
  9.     }  
  10.    return false;  
  11. }  
1 0
原创粉丝点击