获取手机中安装的所有社交媒体应用程序的列表?

时间:2018-05-22 07:12:12

标签: android package-name

我正在开发一个应用程序,列出移动用户中安装的所有应用程序。我检索了所有应用程序,并将其列在 RecyclerView 中。现在我想将社交媒体应用程序与该列表分开以用于其他目的。有没有办法分离社交媒体应用程序? 我使用下面的代码从手机中检索所有应用程序包名称。

public List<String> GetAllInstalledApkInfo(){

    List<String> ApkPackageName = new ArrayList<>();

    Intent intent = new Intent(Intent.ACTION_MAIN,null);

    intent.addCategory(Intent.CATEGORY_LAUNCHER);

    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED );

    List<ResolveInfo> resolveInfoList = context1.getPackageManager().queryIntentActivities(intent,0);

    for(ResolveInfo resolveInfo : resolveInfoList){

        ActivityInfo activityInfo = resolveInfo.activityInfo;

        ApkPackageName.add(activityInfo.applicationInfo.packageName);
    }

    return ApkPackageName;

}

1 个答案:

答案 0 :(得分:1)

如果您为每个应用程序获取其包名,您可以直接询问商店应用所属的类别,使用此库解析html响应页面:

org.jsoup.jsoup1.8.3

以下是解决问题的代码段:

public class MainActivity extends AppCompatActivity {

public final static String GOOGLE_URL = "https://play.google.com/store/apps/details?id=";
public static final String ERROR = "error";

...


   private class FetchCategoryTask extends AsyncTask<Void, Void, Void> {

       private final String TAG = FetchCategoryTask.class.getSimpleName();
       private PackageManager pm;
       private ActivityUtil mActivityUtil;

       @Override
       protected Void doInBackground(Void... errors) {
          String category;
           pm = getPackageManager();
           List<ApplicationInfo> packages = pm.getInstalledApplications(PackageManager.GET_META_DATA);
           Iterator<ApplicationInfo> iterator = packages.iterator();
           while (iterator.hasNext()) {
               ApplicationInfo packageInfo = iterator.next();
               String query_url = GOOGLE_URL + packageInfo.packageName;
               Log.i(TAG, query_url);
               category = getCategory(query_url);
               // store category or do something else
           }
           return null;
        }


        private String getCategory(String query_url) {
           boolean network = mActivityUtil.isNetworkAvailable();
           if (!network) {
               //manage connectivity lost
               return ERROR;
           } else {
               try {
                   Document doc = Jsoup.connect(query_url).get();
                   Element link = doc.select("span[itemprop=genre]").first();
                   return link.text();
               } catch (Exception e) {
                   return ERROR;
               }
           }
        }
    }  
}