如何获得活动课的意图过滤器 [英] How to get Activity Class from intent filter
问题描述
假设有一个与包名 com.example
的应用程序。
Suppose there is an application with package name com.example
.
该应用程序有一个意图过滤器的活动 android.intent.action.SEND
The application has an activity which has an intent filter android.intent.action.SEND
现在我想以编程方式找到支持上述意图过滤器组件类。
Now I want to programatically find the component class that supports the above intent filter.
这code过滤所有符合ACTION_SEND类。
This code filters all the classes that matches ACTION_SEND.
Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_TEXT, "This is my text to send.");
sendIntent.setType("text/plain");
startActivity(sendIntent);
不过,我想只能从匹配 com.example
。
推荐答案
该PackageManager可以检索到的 Context.getPackageManager()和<一href="http://developer.android.com/reference/android/content/pm/PackageManager.html#queryIntentActivities%28android.content.Intent,%20int%29"相对=nofollow> queryIntentActivities ,提供你正在考虑将返回活动的ResolveInfo可以执行这一意图的意图。
The PackageManager can be retrieved with Context.getPackageManager() and queryIntentActivities with the intent you are considering will return the ResolveInfo of Activities that can perform that intent.
ResolveInfo.activityInfo 会给你ActivityInfo其中有的packageName 这就是你正在寻找过滤什么上。
ResolveInfo.activityInfo will give you the ActivityInfo which has packageName which is what you are looking to filter on.
Once you have selected the target you want, you can make the ComponentName for that activity using the package name and class and setComponent() on the desired intent to explicitly target the activity you want.
这篇关于如何获得活动课的意图过滤器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!