检索 Activity 实例的 ActivityInfo
我在帮助程序类中有一个 Activity 实例,并且尝试从 AndroidManifest.xml
文件中的条目获取属性。
我可以获得所有 ActivityInfo
实例的列表,但如何为我的应用程序定位特定实例?
活动实例与帮助程序类不在同一包中,并且未在同一清单中定义(帮助程序类是活动应用程序项目中包含的库)。
到目前为止我所拥有的:
Activity activity = //the instance
String applicationPackage = activity.getApplicationInfo().packageName;
PackageInfo info = activity.getPackageManager().getPackageInfo(packageName, PackageManager.GET_ACTIVITIES);
for (ActivityInfo activityInfo : info.activities) {
if (/* activityInfo applies to our activity instance */) {
return activityInfo.someProperty;
}
}
I have an instance of an Activity in a helper class and I'm trying to obtain attributes from its entry in the AndroidManifest.xml
file.
I can get a list of all of the ActivityInfo
instances but how do I to target the specific one for my application?
The activity instance is not in the same package as the helper class and not defined in the same manifest (helper class is a library included in the activity's application project).
What I have so far:
Activity activity = //the instance
String applicationPackage = activity.getApplicationInfo().packageName;
PackageInfo info = activity.getPackageManager().getPackageInfo(packageName, PackageManager.GET_ACTIVITIES);
for (ActivityInfo activityInfo : info.activities) {
if (/* activityInfo applies to our activity instance */) {
return activityInfo.someProperty;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我对您问题的理解:您需要获取有关应用程序中特定活动的信息。
目前,您正在使用
getPackageInfo
从包管理器获取包信息,但如果您只关心应用程序中的一个 Activity,则无需执行此操作。您可以使用getActivityInfo
代替,只要您知道 Activity 的完全限定名称并使用 ComponentName,例如:com.example.myapp/com.example.myapp.myactivity
PackageManager 文档
My understanding of your question: You need to get the information about a particular Activity within your Application.
Currently you are using
getPackageInfo
to obtain your package information from the package manager, but you don't need to do this if you only care about one Activity within the Application. You can usegetActivityInfo
instead, so long as you know the fully qualified name of the Activity and use a ComponentName, ex:com.example.myapp/com.example.myapp.myactivity
PackageManager Docs
如果要检索当前活动的信息
ActivityInfo info = getPackageManager().getActivityInfo(getComponentName(), 0);
If you want to retrieve the information of the current activity
ActivityInfo info = getPackageManager().getActivityInfo(getComponentName(), 0);