How to get Category for each App on device on Android?

前端 未结 7 1905
隐瞒了意图╮
隐瞒了意图╮ 2020-12-03 04:25

I\'ve got an Android app which scans for all Apps installed on the device and then reports this to a server (it\'s an MDM agent). Any suggestions on how to get the Category

相关标签:
7条回答
  • 2020-12-03 04:47

    if you get for each application its package name, you could ask directly to play store which category an app belongs, parsing html response page with this library:

    org.jsoup.jsoup1.8.3

    Here's a snippet to solve your problem:

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

    You could make these queries in an AsyncTask, or in a service. Hope that you find it helpful.

    0 讨论(0)
提交回复
热议问题