Try:
final List AppsList = getApplicationContext().getPackageManager().queryIntentActivities(myIntent, 0);
Question
I am trying to retrieve the list of installed apps in android. I have written the following code:
final Intent myIntent = new Intent(Intent.ACTION_MAIN, null);
myIntent.addCategory(Intent.CATEGORY_LAUNCHER);
final List AppsList = Context.getPackageManager().queryIntentActivities(myIntent, 0);
I am getting this error:
Cannot make a static reference to the non-static method getPackageManager() from the type Context
Knowing that both Context
and PackageManager
are abstract classes, cannot resolve the error. Kindly help.
Solution
Try:
final List AppsList = getApplicationContext().getPackageManager().queryIntentActivities(myIntent, 0);
OTHER TIPS
You need to use an instance of context to call this method.
Generally when you are in an activity or a service, the context is this
or getContext()
. You should use this technique to :
If you want a more global context, you can use getApplicationContext()
, this is useful if you want for instance, access an embedded database.
Knowing that both Context and PackageManager are abstract classes
Use an instance of a concrete subclass of Context
, such as Activity
.
Since your code probably is already in a method on a subclass of Context
, change the problematic line to:
final List AppsList = getPackageManager().queryIntentActivities(myIntent, 0);
If, for some reason, your code shown above is not in a method on a subclass of Context
, you will need to pass in some Context
instance to wherever this code resides, and call getPackageManager()
on it.