在android中找不到getPackageManager()方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22396382/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can't find getPackageManager() method in android
提问by user3418401
I have problem with my project.
我的项目有问题。
I can't find getPackageManager()
method although i imported android.content.pm.PackageManager;
getPackageManager()
虽然我导入了,但我找不到方法 android.content.pm.PackageManager;
what wrong with this piece of code
这段代码有什么问题
List<PackageInfo> packs = getPackageManager().getInstalledPackages(0);
.Thanks for your helping
.谢谢你的帮助
回答by NameSpace
The error is not in your line of code, but where you are calling it. getPackageManager()
is a method of Context. You can use this method inside an Activity (because an Activity is a Context), but if you are calling it elsewhere, you need to pass a Context. In a fragment you may also have access to the getActivity() function, which returns the Acitivity-Context.
错误不在您的代码行中,而在您调用它的地方。 getPackageManager()
是 Context 的一种方法。您可以在 Activity 内部使用此方法(因为 Activity 是一个 Context),但是如果您在其他地方调用它,则需要传递一个 Context。在片段中,您还可以访问 getActivity() 函数,该函数返回 Acitivity-Context。
Context context...;
context.getPackageManager();
getActivity().getPackageManager();
回答by SHASHANK PUJARI
If you are using it in Activity you will not get an error or warning for getPacketManager
, but if you are using it in Fragments you should prefix it with getActivity
.
如果您在活动使用它,你不会得到一个错误或警告getPacketManager
,但如果你是在片段使用它,你应该使用前缀getActivity
。
example:
例子:
PackageManager pm = getActivity().getPackageManager();
PackageManager pm = getActivity().getPackageManager();
回答by Hariharan
Try this..
尝试这个..
I guess you are extendsis Fragment
我猜你是延伸的 Fragment
So, you have to use getActivity().getPackageManager()
like below
所以,你必须getActivity().getPackageManager()
像下面这样使用
List<PackageInfo> packs = getActivity().getPackageManager().getInstalledPackages(0);
Or extendsis BroadcastReceiver
或扩展是BroadcastReceiver
In side onReceive
you have to use context.getPackageManager()
like below
在侧面onReceive
你必须context.getPackageManager()
像下面这样使用
List<PackageInfo> packs = context.getPackageManager().getInstalledPackages(0);
回答by Ronak Mehta
I think this may be due to Context
我认为这可能是由于 Context
If you are using
如果您正在使用
Activity :Then you can directly access this method by importing android.content.pm.PackageManager;
Activity :然后就可以通过导入直接访问这个方法了android.content.pm.PackageManager;
If you are using Fragment :Then you need to provide getActivity()
to avail method
如果您使用的是Fragment :那么您需要提供getActivity()
可用方法
I.E.
IE
List<PackageInfo> packs = getActivity().getPackageManager().getInstalledPackages(0);
回答by ARK
you can also try context.getPackageManager() if you have context passed as a parameter in your class constructor eg for Broadcast receiver
如果您在类构造函数中将上下文作为参数传递,例如用于广播接收器,您也可以尝试 context.getPackageManager()
回答by Ashwin S Ashok
yep.
if you extends fragment use getActivity().getPackageManager().getInstalledPackages(0);
else context.getPackageManager().getInstalledPackages(0);
是的。如果您扩展片段使用getActivity().getPackageManager().getInstalledPackages(0);
其他context.getPackageManager().getInstalledPackages(0);