2

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

Adobe Reader Application Manager Page

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.

Kenneth Evans
  • 2,179
  • 19
  • 26

3 Answers3

1

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.

Kenneth Evans
  • 2,179
  • 19
  • 26
0

use PackageManager.getLauncherIntent(String packageName) for a default main launcher Activity of a package...

Gopal Gopi
  • 11,101
  • 1
  • 30
  • 43
  • Maybe I'm missing something, but I don't see how getLauncherIntent tells me whether a package has "Launch by Default" set or not. That would be set by the user. I don't see anything in the Intent returned that would indicate this. The Intent appears to typically have action=Main, category=LAUNCHER, flag=FLAG_RECEIVER_FOREGROUND, and a package and component independent of whether is is set to be "Launch by Default" or not. For example, I have Adobe Reader set to open .pdf files. I don't see anything in the com.adobe.reader Intent that indicates this. – Kenneth Evans Dec 31 '13 at 21:10
  • @KennethEvans your requirement seems interesting...and these may help you... http://developer.android.com/reference/android/content/pm/PackageManager.html#MATCH_DEFAULT_ONLY and http://developer.android.com/reference/android/content/pm/PackageManager.html#queryIntentActivities%28android.content.Intent,%20int%29 and http://stackoverflow.com/questions/4094109/android-how-to-i-get-a-list-of-all-available-intent-filters and http://stackoverflow.com/questions/2097813/how-to-parse-the-androidmanifest-xml-file-inside-an-apk-package – Gopal Gopi Jan 02 '14 at 11:45
0

check below code

// intents array list has all the launcher intents
List<PackageInfo> packages = getPackageManager()
          .getInstalledPackages(0);
Intent  intent = null;

for (Iterator iterator = packages.iterator(); iterator.hasNext();) 
{
    PackageInfo packageInfo = (PackageInfo) iterator.next();
    String packageName = packageInfo.packageName;
    // adobe reader example is taken. 
      if(packageName.equals(com.adobe.reader))
      {
        intent =getPackageManager().getLaunchIntentForPackage(packageName));
            break;
      }
}
   if(intent != null)
     {
        startActivty(intent);
     }

Or you can try this link for pdf files link

Community
  • 1
  • 1
Sush
  • 3,864
  • 2
  • 17
  • 35
  • Thanks. See the comment under the other similar suggestion. – Kenneth Evans Jan 01 '14 at 17:40
  • I don't want to start the Activity. I want to know if the _package_ is set to be launched by default for some action. In the example Adobe Reader is set to be launched by default for opening PDF files (which means it doesn’t prompt you with a list of all Activities that open PDF files). I don't find anything in the Intent returned by getLauncherIntent for Adobe Reader that tells me this. The action might even launch some other Activity in the package. I also don't see anything essentially different in the Intent returned for Activities that launch by default and those that don't. – Kenneth Evans Jan 01 '14 at 19:24