I want to get all application which appears in the menu screen. But, now I only get the user installed apps or all the application (included the system application).
My current code is:
final PackageManager pm = getActivity().getPackageManager();
List<PackageInfo> apps = pm.getInstalledPackages(PackageManager.GET_META_DATA);
ArrayList<PackageInfo> aux = new ArrayList<PackageInfo>();
for (int i = 0; i < apps.size(); i++) {
if (apps.get(i).versionName != null && ((apps.get(i).applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 1)) {
aux.add(apps.get(i));
}
With this code, I can get the user installed apps, and if I comment the 'if' instruction, I will get the system apps.
So, I want to get the user installed apps and apps like contacts, gallery and so on.
UPDATE:
final PackageManager pm = getActivity().getPackageManager();
Intent intent = new Intent(Intent.ACTION_MAIN, null);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
List<ResolveInfo> apps = pm.queryIntentActivities(intent, PackageManager.GET_META_DATA);
final PackageManager pm = getPackageManager();
List<ApplicationInfo> packages = pm.getInstalledApplications(PackageManager.GET_META_DATA);
Using PackageInfo:
private boolean isSystemPackage(PackageInfo packageInfo) {
return ((packageInfo.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0);
}
Using ResolveInfo:
private boolean isSystemPackage(ResolveInfo resolveInfo) {
return ((resolveInfo.activityInfo.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0);
}
Using ApplicationInfo:
private boolean isSystemPackage(ApplicationInfo applicationInfo) {
return ((applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0);
}
This filters the system package. See this question. Credits: Nelson Ramirez and Kenneth Evans.
final PackageManager pm = getActivity().getPackageManager();
Intent intent = new Intent(Intent.ACTION_MAIN, null);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
List<ResolveInfo> apps = pm.queryIntentActivities(intent, PackageManager.GET_META_DATA);
I had the same requirement. Ultimately I added another condition to filter down the app list. I just checked whether the app has 'launcher intent'.
So, the resultant code looks like...
PackageManager pm = getPackageManager();
List<ApplicationInfo> apps = pm.getInstalledApplications(PackageManager.GET_GIDS);
for (ApplicationInfo app : apps) {
if(pm.getLaunchIntentForPackage(app.packageName) != null) {
// apps with launcher intent
if((app.flags & ApplicationInfo.FLAG_UPDATED_SYSTEM_APP) == 1) {
// updated system apps
} else if ((app.flags & ApplicationInfo.FLAG_SYSTEM) == 1) {
// system apps
} else {
// user installed apps
}
appsList.add(app);
}
}
Below code will give you all the system apps related to Operating System.
This will not include Apps like YouTube, Gmail, etc
try {
ApplicationInfo ai = getPackageManager().getApplicationInfo(item.packageName, 0);
if ((ai.flags & ApplicationInfo.FLAG_SYSTEM ) != 0) {
if((ai.flags & ApplicationInfo.FLAG_UPDATED_SYSTEM_APP ) == 0) {
appItem.setSystemApp(true);
Mylog.v("System Apps " + appItem.getAppName());
}
}
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
Related
hey can anyone explain to me this if condition , btw the code works but i dont understand the if condition
PackageManager pm = context.getPackageManager();
List<ApplicationInfo> apps = pm.getInstalledApplications(0);
for(ApplicationInfo app : apps) {
if((app.flags & (ApplicationInfo.FLAG_UPDATED_SYSTEM_APP | ApplicationInfo.FLAG_SYSTEM)) > 0) {
// It is a system app
} else {
// It is installed by the user
}
}
To get the list of installed apps on Android, the code is used as follows:
Intent intent = new Intent(Intent.ACTION_MAIN, null);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
List<ResolveInfo> appsList = context.getPackageManager().queryIntentActivities(intent, 0);
I have a listview which show the all installed apps including some system apps but does not show gallery, contact, messages apps. Please tell me how can I get all these system apps. Here is my code
public static List getInstalledApplication(Context c)
{
// return c.getPackageManager().getInstalledApplications(PackageManager.GET_META_DATA);
List<ApplicationInfo> installedApps = new ArrayList<ApplicationInfo>();
PackageManager pm = c.getPackageManager();
List<ApplicationInfo> apps = pm.getInstalledApplications(0);
for(ApplicationInfo app : apps) {
//checks for flags; if flagged, check if updated system app
if((app.flags & ApplicationInfo.FLAG_UPDATED_SYSTEM_APP) == 1) {
installedApps.add(app);
//it's a system app, not interested
} else if ((app.flags & ApplicationInfo.FLAG_SYSTEM) == 1) {
//Discard this one
//in this case, it should be a user-installed app
// installedApps.add(app);
} else {
installedApps.add(app);
}
}
return installedApps;
}
Tell me where I make mistake. Help me with some code.
The apps that you want to list (Gallery, Message, etc) are written in system partition and hence, (app.flags & ApplicationInfo.FLAG_SYSTEM) == 1) will be true.
FLAG_SYSTEM
if set, this application is installed in the device's system image.
Thats why the apps that you want to also list are getting skipped.
If you want to get apps which are listed in your launcher i.e apps with category <Launcher> get the list using following code
final PackageManager packageManager = getPackageManager();
Intent intent = new Intent(Intent.ACTION_MAIN, null);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
List<ResolveInfo> resInfos = packageManager.queryIntentActivities(intent, 0);
// using hashset so that there will be no duplicate packages,
// if no duplicate packages then there will be no duplicate apps
HashSet<ApplicationInfo> installedApps = new HashSet<ApplicationInfo>(0);
// getting package names and adding them to the hashset
for (ResolveInfo resolveInfo : resInfos) {
installedApps.add(resolveInfo.activityInfo.applicationInfo);
}
Since you want to show all the installed apps. You could get rid of the if-else block in your code and simply add all the apps and display them.
or make these change in you code
1) Fetch all the apps by this code.
List<ApplicationInfo> apps = getPackageManager().getInstalledPackages(0);
2) And separate system apps from user installed with the following code:
List<ApplicationInfo> apps = getPackageManager().getInstalledApplications(0);
for(ApplicationInfo app : apps) {
if((app.flags & (ApplicationInfo.FLAG_UPDATED_SYSTEM_APP | ApplicationInfo.FLAG_SYSTEM)) > 0) {
// It is a system app
} else {
// It is installed by the user
}
}
Try this different ways.
//First
Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
List pkgAppsList = context.getPackageManager().queryIntentActivities(mainIntent, 0);
// Second
PackageManager pm = getPackageManager();
List packages = pm.getInstalledApplications(PackageManager.GET_META_DATA);
for (ApplicationInfo packageInfo : packages)
{
Log.d(TAG, "Installed package :" + packageInfo.packageName);
Log.d(TAG, "Launch Activity :" + pm.getLaunchIntentForPackage(packageInfo.packageName));
}
// Third
private List getInstalledComponentList()
throws NameNotFoundException {
Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
List ril = getPackageManager().queryIntentActivities(mainIntent, 0);
List componentList = new ArrayList();
String name = null;
for (ResolveInfo ri : ril) {
if (ri.activityInfo != null) {
Resources res = getPackageManager().getResourcesForApplication(ri.activityInfo.applicationInfo);
if (ri.activityInfo.labelRes != 0) {
name = res.getString(ri.activityInfo.labelRes);
} else {
name = ri.activityInfo.applicationInfo.loadLabel(
getPackageManager()).toString();
}
componentList.add(name);
}
}
return componentList;
}
I am working on system application handling in my app. I have generated the complete list of all the installed apps in Android device in my app. Now my task is to identify the SYSTEM Apps from that list of application. Or Identify the CURRENT FOREGROUND App is an SYSTEM APP or INSTALLED APP ?
Using below code currently:
PackageManager packageManager = getPackageManager();
Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
List<ResolveInfo> appList = packageManager.queryIntentActivities(mainIntent, 0);
Collections.sort(appList, new ResolveInfo.DisplayNameComparator(packageManager));
List<PackageInfo> packs = packageManager.getInstalledPackages(0); //PackageManager.GET_META_DATA
for(int i=0; i < packs.size(); i++) {
PackageInfo p = packs.get(i);
ApplicationInfo a = p.applicationInfo;
if ((!includeSysApps) && ((a.flags & ApplicationInfo.FLAG_SYSTEM) == 1))
{
continue;
}
apps.add(p.packageName);
below code differs the system apps and installed apps.
List<ApplicationInfo> installedApps = getPackageManager().getInstalledApplications(0);
for (int i = 0; i < installedApps.size(); i++) {
if(installedApps.get(i).sourceDir.startsWith("/data/app/")){
//Non System Apps
}else{
//system Apps
}}
I'm looking for the best way to check if application is launchable.
There is my code :
PackageManager packageManager = context.getPackageManager();
List<PackageInfo> packs = packageManager.getInstalledPackages(0);
for (int i = 0; i < packs.size(); i++) {
PackageInfo p = packs.get(i);
if (packageManager.getLaunchIntentForPackage(p.applicationInfo.packageName) != null) {
// Get application info
}
}
This works, but when i do app profiling i noticed that packageManager.getLaunchIntentForPackage() method consumes a lot of execution time, so i'm looking for an alternative way to check if each application is launchable without getting the launch intent.
Any idea ?
Thank you !
I found solution for my own problem, i hope it will help someone :
PackageManager packageManager = context.getPackageManager();
Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
final List<ResolveInfo> apps = packageManager.queryIntentActivities(
mainIntent, 0);
Collections.sort(apps, new ResolveInfo.DisplayNameComparator(
packageManager));
for (ResolveInfo resolveInfo : apps) {
// Get application data here
}
I've kept a button which takes user to my facebook page.
In order that the official facebook app is opened I use the following url:
fb://pages/PAGE_ID
instead of http://facebook.com/PAGE_ID
Because in that case you get a list of browsers to open the url instead of teh facebook app.
It works if the user has facebook app installed. However it crashes if the user doesn't have facebook app.
Is there any way to check if the user has the facebook app?
Have you already checked this?
You can always check if an app is installed like this.
In an native android app, this is quite easy to achieve:
Uri dataUri = Uri.parse("fb://....");
Intent receiverIntent = new Intent(Intent.ACTION_VIEW, dataUri);
PackageManager packageManager = getPackageManager();
List<ResolveInfo> activities = packageManager.queryIntentActivities(receiverIntent, 0);
if (activities.size() > 0) {
startActivity(receiverIntent);
} else {
Uri webpage = Uri.parse("http://www.facebook.com/...");
Intent webIntent = new Intent(Intent.ACTION_VIEW, webpage);
packageManager = getPackageManager();
activities = packageManager.queryIntentActivities(webIntent, 0);
if (activities.size() > 0) {
startActivity(webIntent);
}
}
I think you can reuse this code I wrote for checking if twitter app was installed on the device, to check if facebook app is installed. For twitterApps list you have to replace the values by "com.facebook.katana".
public Intent findTwitterClient() {
final String[] twitterApps = { "com.twitter.android", "com.handmark.tweetcaster", "com.seesmic", "com.thedeck.android", "com.levelup.touiteur", "com.thedeck.android.app" };
Intent tweetIntent = new Intent(Intent.ACTION_SEND);
tweetIntent.putExtra(Intent.EXTRA_TEXT, "#hashtagTest");
tweetIntent.setType("text/plain");
final PackageManager packageManager = getPackageManager();
List<ResolveInfo> list = packageManager.queryIntentActivities(tweetIntent, PackageManager.MATCH_DEFAULT_ONLY);
for (int i = 0; i < twitterApps.length; i++) {
for (ResolveInfo resolveInfo : list) {
String p = resolveInfo.activityInfo.packageName;
if (p != null && p.startsWith(twitterApps[i])) {
tweetIntent.setPackage(p);
return tweetIntent;
}
}
}
return null;
}