【Android】如何寻找出某个Intent是否可用(queryIntentActivities)

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
 

你可能感兴趣的:(android,String,application,action,menu,behavior)