如何获得活动课的意图过滤器 [英] How to get Activity Class from intent filter

查看:121
本文介绍了如何获得活动课的意图过滤器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设有一个与包名 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.

一旦你选择你想要的目标,可以使组件名使用包名和类和活性<一href="http://developer.android.com/reference/android/content/Intent.html#setComponent%28android.content.ComponentName%29"相对=nofollow> setComponent()所需的意图明确的目标,你想要的活动

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屋!

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