how to check installed apps android - android

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);

Related

Android: Issue in getting a list of all installed apps including system apps

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;
}

How to get list of all daydreams installed on android phone

I have managed to get list of all live wallpapers installed on phone. But i would like to get the list of all daydreams installed on my android phone. Can someone please help me with the intent that i should use to get this list.
This code prints all the Daydream available in the device:
PackageManager packageManager = getPackageManager();
Intent dreamIntent = new Intent(DreamService.SERVICE_INTERFACE);
List<ResolveInfo> resolveInfoList = packageManager.queryIntentServices(dreamIntent, PackageManager.GET_META_DATA);
for (ResolveInfo resolveInfo : resolveInfoList) {
if (resolveInfo.serviceInfo == null) {
continue;
}
Log.d(TAG, String.valueOf(resolveInfo.loadLabel(packageManager)));
}

How to get installed applications in Android and no system apps?

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();
}

Android check if application package is launchable

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
}

Android: Check to see if Facebook app is present on user's device

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;
}

Categories

Resources