0

我想做一些你可能称之为动态文件夹的东西,其中包含一个列表应用程序,这些应用程序由例如 id(包名称)或可见名称的某些部分过滤。

所以我需要一些可以用于联系人的东西:

Cursor contacts = managedQuery(ContactsContract.Contacts.CONTENT_URI,
                    new String[]{
                        ContactsContract.Contacts.DISPLAY_NAME,
                        ContactsContract.Contacts._ID
                    },
                    ContactsContract.Contacts.HAS_PHONE_NUMBER + "='1'"
                    , null, null)

但我需要查询以列出应用程序。

从列表中选择时,我还需要一种运行应用程序的方法。

4

1 回答 1

1

您可以编写如下内容:

    PackageManager pm = this.getPackageManager();

    Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
    mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);

    List<ResolveInfo> appList = pm.queryIntentActivities(mainIntent, 0);
    Collections.sort(appList, new ResolveInfo.DisplayNameComparator(pm));

    int found = -1;
    for (int i = 0; i < appList.size(); i++) {
        String pack = appList.get(i).activityInfo.applicationInfo.packageName;
        if (pack.equals("com.mypackage")) // Your custom criteria comes here...
            found = i;
    }
    // Launch found application
            Intent intent = new Intent();
        intent.setAction(Intent.ACTION_MAIN);
        intent.addCategory(Intent.CATEGORY_LAUNCHER);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
        intent.setComponent(new ComponentName(appList.get(found).activityInfo.applicationInfo.packageName, appList
                .get(found).activityInfo.name));
        startActivity(intent);
于 2013-01-18T23:32:29.280 回答