Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't find getPackageManager() method in android

Tags:

android

I have problem with my project.

I can't find getPackageManager() method although i imported android.content.pm.PackageManager;

what wrong with this piece of code

List<PackageInfo> packs = getPackageManager().getInstalledPackages(0); 

.Thanks for your helping

like image 869
user3418401 Avatar asked Mar 14 '14 04:03

user3418401


2 Answers

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.

    Context context...;      context.getPackageManager();     getActivity().getPackageManager(); 
like image 200
NameSpace Avatar answered Oct 10 '22 01:10

NameSpace


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.

example:

PackageManager pm = getActivity().getPackageManager();

like image 37
Shashank Pujari Avatar answered Oct 10 '22 01:10

Shashank Pujari