Get a list of every launcher in Android - android

In my application I want to show a list of every available launcher (for homescreen) on that specific Android phone. Is it possible to get some kind of information from Android OS and how do I make this call?
Thanks!
Kind regards
Daniel

You can query the list of ResolverInfo that match with a specific Intent. The next snippet of code print all installed launchers.
PackageManager pm = getPackageManager();
Intent i = new Intent(Intent.ACTION_MAIN);
i.addCategory(Intent.CATEGORY_HOME);
List<ResolveInfo> lst = pm.queryIntentActivities(i, 0);
for (ResolveInfo resolveInfo : lst) {
Log.d("Test", "New Launcher Found: " + resolveInfo.activityInfo.packageName);
}

The code snippet above does NOT work accurately, as the result of launchers' list also includes system's setting's app whose package name is com.android.settings. This unexpected result happens on both my Pixel 2 (Android 8.0 ) and Nexus 6 (Android 7.1).

Try the following:
Obtain the list of installed applications:
List pkgList = getPackageManager().getInstalledPackages(PackageManager.GET_ACTIVITIES);
Iterate over this list and obtain launcher activity using:
getPackageManager().getLaunchIntentForPackage(packageName);
For details read here: PackageManager. Hope this helps.

Related

Which flag for standard apps in PackageManager.getInstalledApplications(flag)?

When I open the Android main menu on my Android smartphone, I get a set of apps like Youtube, Calculator, Email clients etc. No system stuff or any libraries are visible there.
To retrieve these apps programamtically, I do:
PackageManager.getInstalledApplications(flag: Int)
where I get a list of ApplicationInfo, which also contains alot more than mentioned installed standard apps. What flag do I have to set to get only the same apps, which I see when I swipe up on my Smartphone?
val mainIntent = Intent(Intent.ACTION_MAIN, null)
mainIntent.addCategory(Intent.CATEGORY_LAUNCHER)
val appList = context.getPackageManager().queryIntentActivities( mainIntent, 0)
You can try this to get all user apps (and the ones your launcher shows)
You might need to add additional permissions in your manifest above Android 10 though
Edit: If you want ApplicationInfo instead of ResolveInfo in your list you can retrieve it like this:
appList[your_index].activityInfo.applicationInfo

Android get dialer package name

I need to get package name of an app which post notification about a missed call. As far as I understand it makes dialer app of a device. Can I get package name of dialer app on the device, I need it from API 19?
You can filter all dialer applications using Intent.ACTION_DIAL action and PackageManager.queryIntentActivities(...). Where it will return list of applications which can dial phone. Read more at Android: How to show a list of dialer app installed on my device instead of directly calling default dialer
You can use below code, as it is
public List<String> getPackagesOfDialerApps(Context context){
List<String> packageNames = new ArrayList<>();
// Declare action which target application listen to initiate phone call
final Intent intent = new Intent();
intent.setAction(Intent.ACTION_DIAL);
// Query for all those applications
List<ResolveInfo> resolveInfos = context.getPackageManager().queryIntentActivities(intent, 0);
// Read package name of all those applications
for(ResolveInfo resolveInfo : resolveInfos){
ActivityInfo activityInfo = resolveInfo.activityInfo;
packageNames.add(activityInfo.applicationInfo.packageName);
}
return packageNames;
}
To get an app's package (and activity name) you can use adb command.
Launch the app and then:
adb shell dumpsys window windows | grep -E 'mCurrentFocus|mFocusedApp'
From the output:
mFocusedApp=AppWindowToken{
e224f9d token=Token{
d8b2e74 ActivityRecord{
1d97647 u0 com.android.dialer/.app.DialtactsActivity t1078
}
}
}
You can say the app's package is com.android.dialer and activity name is .app.DialtactsActivity

How to open application from list of Installed Application [Android]

SOLVED :
I was splitting the voice command .if user says "Open Browser", i was splitting it
with "Open", so the "Browser" keyword was containing space before it.
i splitted it with "Open " & it worked.. This silly mistake made to me to spend whole day on this ..
I am trying to Open a Specific app from the list which has the Installed apps details.
To be specific , i m using it with RecognizerIntent , means if i say "calculator" , then
it should open calculator App.
i have done the following to get the list of installed apps :
PackageManager pm ;
List<ApplicationInfo> installedApps;
pm = getPackageManager();
installedApps = pm.getInstalledApplications(PackageManager.GET_META_DATA);
After getting installed app in List , i loop through the list to find out if the app which i need is present or not as following :
for(ApplicationInfo ai : installedApps)
{
String appName = ai.loadLabel(pm).toString().toLowerCase();
if(appName.equals(AppSearch[1)) //AppSearch[1] contains the result of speech i.e calculator
{
Intent openApp = new Intent(pm.getLaunchIntentForPackage(ai.packageName));
startActivity(openApp);
openApp.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
}
}
The problem is it doesn't goes into the above "if" condition..
if i simply print the application's name in "for" loop then it shows all the names,
but then why i m not able to compare it & then open it ??
What am i doing wrong please guide me in this .. thanks )
For speech recognition, I would not recommend expecting the result to be 100% accurate, especially with app names. Moreover, from your comment, it seems you don't want to check whether the strings equal, but if the app name contains the voice command, which makes a big difference.
If you just want to check whether the voice result is containes in the app name, use
appName.contains(AppSearch[1]);

Given an applications package name, how to know if that application supports widgets on homescreen

Given an applications package name how to know if that application supports widgets on homescreen ??
David Wasser's solution was my first guess too, but it failed to detect one of my widget.
Here's another approach if the first one doesn't work for you.
You can list all receivers for the ACTION_APP_WIDGET_UPDATE action, which allhome screen widget should listen to, and then filter by package name :
PackageManager pm = getPackageManager();
Intent intent = new Intent();
intent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
List<ResolveInfo> list = pm.queryBroadcastReceivers(intent, 0);
for (ResolveInfo info : list) {
Log.i("package", info.activityInfo.packageName);
}
Use PackageManager and call getPackageInfo(packageName, GET_RECEIVERS) for the application.
Loop through all PackageInfo.receivers and call PackageManager.getReceiverInfo(componentName, GET_META_DATA) for each ActivityInfo object in PackageInfo.receivers.
Look at the field metadata. This may be null or it may contain a Bundle. If it contains a Bundle, look in the Bundle for an item with key "android.appwidget.provider". If you find that then you can be pretty sure that the application supports widgets on the homescreen.

Obtaining the android:label from other applications

i'm programming an android app, a Tasker, and i really don't find the way to obtain the label of another app. This is my point, i'm using an ListActivity where you select an installed app, then when you click it creates the Intent and all the app stuff, what i wanna do is to show to the user the android:label from the app he selected, i found in ResolverInfo an attribute called .activityInfo.labelRes, and i think is the label descriptor for the R class of the app the user selected, is there anyway to obtain the string that matches to that id???
Thanks!
D.Gómez
You can use the loadLabel(PackageManager) method of ResolveInfo to get the label of an activity. Here's a full example which finds all the launcher activities on the device and prints them to logcat:
// Get the package manager
PackageManager pm = getPackageManager();
// Create an intent that matches all launcher activities
// (and ignores non-launcher activities)
Intent launcherIntent = new Intent(Intent.ACTION_MAIN);
launcherIntent.addCategory(Intent.CATEGORY_LAUNCHER);
// Get all activites matching the intent
List<ResolveInfo> launchers = pm.queryIntentActivities(launcherIntent, 0);
for(ResolveInfo info : launchers) {
// Get the activity label and print it
CharSequence label = info.loadLabel(pm);
Log.v("LabelTest", "App found: " + label);
}
To answer the second part of your question too, about accessing the resources of an application: They can be accessed by calling getPackageManager().getResourcesForApplication(String), which will return a Resources object that you can use, though in your case, that should not be necessary.

Categories

Resources