如何找到默认启动设置的软件包?

时间:2013-12-28 17:59:25

标签: android

我想在Android应用程序管理器中找到设置为“默认启动”的软件包。应用程序管理器通常会说“此应用程序默认设置为某些操作打开”,并为这些程序包设置了“清除默认值”按钮。

作为示例,此图显示了Adobe Reader的应用程序管理器页面,我将其设置为打开PDF文件的默认设置。我希望能够以编程方式确定此程序包(com.adobe.reader)具有“默认启动”设置,更一般地说,所有具有“默认启动”设置的程序包。 (显然,应用程序管理器可以执行此操作。)

Adobe Reader Application Manager Page

我可以通过以下方式获取所有已安装的软件包:

  List<PackageInfo> packages = getPackageManager()
          .getInstalledPackages(0);

如何判断哪些设置了“默认启动”?有没有办法找到行动?

感谢。

3 个答案:

答案 0 :(得分:1)

这似乎对我有用:

/**
 * 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;
}

我被抛出的事实是,PackageManager.getPreferredActivities()的返回值似乎是零而不是已注册的首选活动的总数。如果它不总是为零,则检查非零是足以查看包是否具有“默认启动”设置。我还发现PackageManager.getPreferredPackages()总是为我返回零。我还发现为过滤器和活动(如Javadoc中提到的)的PackageManager.getPreferredActivities()传递null给出了NullPointerException。

答案 1 :(得分:0)

使用PackageManager.getLauncherIntent(String packageName)作为包的默认主启动器活动...

答案 2 :(得分:0)

检查以下代码

// 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);
     }

或者您可以尝试使用此链接获取pdf文件link