I would like to find the packages that have "Launch by Default" set in the Android application manager. The application manager typically says something like "This app is set to open by default for some actions" and has a "Clear defaults" button for these packages.
As an example this image shows the application manager page for Adobe Reader, which I have set as the default for opening PDF files. I would like to be able to determine programmatically that this package (com.adobe.reader) has a "Launch by Default" set and, more generally, all the packages that have such a "Launch by Default" set. (Clearly the Application Manager can do this.)
I can get all the installed packages via:
List<PackageInfo> packages = getPackageManager() .getInstalledPackages(0);
How do I tell which ones have "Launch by Default" set? Is there a way to also find the actions?
Thanks.
This seems to work for me:
/**
* Get info on the preferred (launch by default) applications.
* @return
*/
public String getPreferredAppInfo() {
List<PackageInfo> packages = getPackageManager()
.getInstalledPackages(0);
List<IntentFilter> filters = new ArrayList<IntentFilter>();
List<ComponentName> activities = new ArrayList<ComponentName>();
String info = "";
int nPref = 0, nFilters = 0, nActivities = 0;
PackageInfo pkg = null;
for (int i = 0; i < packages.size(); i++) {
pkg = packages.get(i);
nPref = getPackageManager().getPreferredActivities(filters,
activities, pkg.packageName);
nFilters = filters.size();
nActivities = activities.size();
if (nPref > 0 || nFilters > 0 || nActivities > 0) {
// This is a launch by default package
info += "\n" + pkg.packageName + "\n";
for (IntentFilter filter : filters) {
info += "IntentFilter:\n";
for (int j = 0; j < filter.countActions(); j++) {
info += " action: " + filter.getAction(j) + "\n";
}
for (int j = 0; j < filter.countCategories(); j++) {
info += " category: " + filter.getCategory(j) + "\n";
}
for (int j = 0; j < filter.countDataTypes(); j++) {
info += " type: " + filter.getDataType(j) + "\n";
}
for (int j = 0; j < filter.countDataAuthorities(); j++) {
info += " data authority: "
+ filter.getDataAuthority(j) + "\n";
}
for (int j = 0; j < filter.countDataPaths(); j++) {
info += " data path: " + filter.getDataPath(j)
+ "\n";
}
for (int j = 0; j < filter.countDataSchemes(); j++) {
info += " data path: " + filter.getDataScheme(j)
+ "\n";
}
// for (ComponentName activity : activities) {
// info += "activity="
// + activity.flattenToString() + "\n";
// }
}
}
}
return info;
}
I was thrown by the fact that the return value for PackageManager.getPreferredActivities() seems to be zero instead of the total number of registered preferred activities. If it weren't always zero, checking for non-zero would be sufficient to see if a package has "launch by default" set. I also found PackageManager.getPreferredPackages() always returned zero for me. I also found passing null for PackageManager.getPreferredActivities() for the filters and activities (as mentioned in the Javadoc) gave a NullPointerException.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments