Android check if application package is launchable - android

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
}

Related

how to check installed apps 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);

How can I get OS default apps in android

I'm developing a launcher app, I need to retrieve an Android OS default Phone app, Browser app and SMS apps', application Info (Application name, Package name, Launcher icon). Following code is used to get all launchable applications.
private static List<ApplicationInfo> getInstalledApps(Context context, PackageManager pm) {
List<ApplicationInfo> installedApps = context.getPackageManager().getInstalledApplications(0);
List<ApplicationInfo> laughableInstalledApps = new ArrayList<>();
for(int i =0; i<installedApps.size(); i++){
if(pm.getLaunchIntentForPackage(installedApps.get(i).packageName) != null){
laughableInstalledApps.add(installedApps.get(i));
}
}
return laughableInstalledApps;
}
After spending some time with the code, I found a way get what I wanted.
Default Dial App
Intent mainIntent = new Intent(Intent.ACTION_DIAL, null);
mainIntent.addCategory(Intent.CATEGORY_DEFAULT);
List<ResolveInfo> pkgAppsList = getPackageManager().queryIntentActivities(mainIntent, 0);
ActivityInfo info = pkgAppsList.get(0).activityInfo;
Default SMS App
String smsPkgName = Telephony.Sms.getDefaultSmsPackage(context);
ApplicationInfo info = getPackageManager().getApplicationInfo(smsPkgName, 0);
Default Browser App
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://"));
ResolveInfo resolveInfo = getPackageManager().resolveActivity(browserIntent,
PackageManager.MATCH_DEFAULT_ONLY);
ActivityInfo info = resolveInfo.activityInfo;
Try with PackageManager#getPreferredActivities(java.util.List, java.util.List, java.lang.String), where the first parameter is a list of IntentFilter, which you would like to get default apps for. Then the answer is written in list passed as second parameter.
Here are some common intents for which you might try to find default apps.

How to get metadata from ActivityInfo?

I want to read activity metadata from another app. I have cole like this:
PackageManager packageManager = getPackageManager();
Intent intent = new Intent(MY_ACTION);
List<ResolveInfo> pluginsInfo = packageManager.queryIntentActivities(intent, PackageManager.GET_RESOLVED_FILTER);
for (ResolveInfo plugin : pluginsInfo) {
ResolveInfo info = plugin;
ActivityInfo aInfo = info.activityInfo;
Bundle metadata = aInfo.metaData;
}
And sometimes it works good, but sometimes metadata is null. On same emulator and same app version. Is there other way to do this?
And answer is simple:
packageManager.queryIntentActivities(intent, PackageManager.GET_RESOLVED_FILTER | PackageManager.GET_META_DATA);

Usage of Application Info , Package Info and Resolve Info

What is the use of Application info and how to use it.
I'm confused how to retrieve the installed apps from the device and to display it with logo
Can anyone please help me to sort it out.
Differentiate the terms applicaiton info, package info and resolve info.
to display a list of installed apps, you can try this
final Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
final List pkgAppsList = context.getPackageManager().queryIntentActivities( mainIntent, 0);
You will get all the necessary data in the ResolveInfo to start a application. You can check ResolveInfo
or try this code
PackageManager pm = this.getPackageManager();
Intent intent = new Intent(Intent.ACTION_MAIN, null);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
ArrayList<ResolveInfo> list = (ArrayList<ResolveInfo>)
pm.queryIntentActivities(intent, PackageManager.PERMISSION_GRANTED);
for (ResolveInfo rInfo : list) {
System.out.println("Installed Applications " + rInfo.activityInfo.applicationInfo.loadLabel(pm).toString());
}

how to identify a Foreground activity is an System Activity or an INSTALLED App in Android?

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

Categories

Resources