Android - Check if Settings.ACTION_XXX exists on the current device - android

I did peel the documentation in order to see if there is any method that can check if a particular Settings exists on the current device but I did not find anything.
For example, I would like to do the following:
if "Settings.ACTION_DATA_ROAMING_SETTINGS" exists on the current device then ...
If anyone has the answer, I'm taker.

Intent intent = new Intent(android.provider.Settings.ACTION_DATA_ROAMING_SETTINGS, null);
List<ResolveInfo> activities = getPackageManager().queryIntentActivities(intent, 0);
Run the above code to check whether there are some components that handle this specific action. If the activities is not empty, the answer is yes.

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

DevicePolicyManager.enableSystemApp() method does not work as expected

I'm trying to use enableSystemApp method to activate default system apps after provisioning device with the app that is set to device owner mode.
There are two methods to do this:
1) void enableSystemApp (ComponentName admin, String packageName) - in this case you need to pass package name explicitly as String. It works fine, the app gets enabled.
For example, calling this
devicePolicyManager.enableSystemApp(deviceAdminComponent, "com.google.android.gm");
enables default Gmail client, which is disabled after provisioning.
2) int enableSystemApp (ComponentName admin, Intent intent) - in this case, you need to pass an implicit intent and Android should enable all system apps that match this intent. In addition, this method returns int number of apps that match the intent. And here's the problem - I can't get this method to work, it always returns 0 and doesn't enable anything.
Here's the snippet I'm trying to use:
Intent intent = new Intent();
intent.setAction(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_APP_EMAIL);
int i = devicePolicyManager.enableSystemApp(deviceAdminComponent, intent);
It does not work and i == 0 in this case. What am I doing wrong?
Any help is appreciated!
Under the hood, the method that accepts an intent queries to get the list of activities that respond to that intent and then loops through the list passing in the package name string to enable the package. It's similar to doing this:
Intent intent = new Intent();
intent.setAction(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_APP_EMAIL);
List<ResolveInfo> infoes = getPackageManager()
.queryIntentActivities(intent, MATCH_DIRECT_BOOT_AWARE | MATCH_DIRECT_BOOT_UNAWARE);
for (ResolveInfo info in infoes) {
devicePolicyManager.enableSystemApp(deviceAdminComponent, info.activityInfo.packageName);
}
Since you are able to enable the app using the package name string, the fault most likely lies in the way the intent is being resolved - which is supported by the fact that it always returns 0.
It is counter-intuitive, but my suspicion is that the application does not resolve the ACTION_MAIN intent because the app is disabled. Have you tried a less generic intent? I would try the following
Intent i;
// #1
// This goes full circle, but I expect it should work
i = getPackageManager().getLaunchIntentForPackage("com.google.an‌​droid.gm")
// #2
i = new Intent(Intent.ACTION_SEND).setPackageName("com.google.android.gm");
// #3
// Generic, but should resolve _all_ email apps - not just the default one.
// The mailto schema filters out non-email apps
i = new Intent(Intent.ACTION_VIEW , Uri.parse("mailto:"));
Option #1 and #2 are more academic. Both require the package name at which point you may as well use the string overload of enableSystemApp. Option #3 is my best guess for something generic that might still work, but it's possible that it still won't work because the app is disabled.
Note: I find it interesting that enableSystemApp only passes the MATCH_DIRECT_BOOT_AWARE and MATCH_DIRECT_BOOT_UNAWARE flags when querying activities that can resolve the intent, because the MATCH_DISABLED_COMPONENTS and MATCH_SYSTEM_ONLY flags seem much more relevant in this situation.

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.

Get a list of every launcher in 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.

how to dynamically load the app list in android

Let's say, there are four apps in the system: app1, app2, app3, app4.
Be default, when the system is up, all apps will be shown in the home screen. Now if we provide a customized log in screen, user A log in, then for this user, he can only see (and use ) app1 and app2.
Then A log out, user B log in, he can only see app3 and app4.
Does API provide such capability to load the app list dynamically?
Hope someone can help, thanks.
I think the answer depends on how you build your logging system. But, in theory,the basisc around applications list in Android system would be something like that :
Intent intent = new Intent(Intent.ACTION_MAIN, null);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
List pkgAppsList = mContext.getPackageManager().queryIntentActivities(intent, 0);

Categories

Resources