问题描述
我有一个 Android 应用程序,它会扫描设备上安装的所有应用程序,然后将其报告给服务器(它是 MDM 代理).有关如何获取应用程序类别的任何建议?每个人都有不同的类别列表,但基本上是游戏、娱乐、工具/实用程序等.
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 of the App? Everyone has a different list of Categories, but basically something like Game, Entertainment, Tools/Utilities, etc.
据我所知,与设备本身存储的类别无关.我正在考虑使用 android market API 在市场中搜索应用程序并使用搜索返回的 Category 值.不确定这将如何成功找到匹配项.有关如何最好地做到这一点的任何建议?
From what I can tell there is nothing related to Category stored on the device itself. I was thinking of using the android market API to search for the application in the market and use the Category value returned by the search. Not sure how successful this will be finding a match. Any suggestions on how best to do this?
对不同的方法有什么建议吗?
Any suggestions on a different approach?
提前致谢.迈克
推荐答案
如果你为每个应用获取了它的包名,你可以直接询问 play store 一个应用属于哪个类别,用这个库解析 html 响应页面:
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;
}
}
}
}
}
您可以在 AsyncTask 或服务中进行这些查询.希望对您有所帮助.
You could make these queries in an AsyncTask, or in a service. Hope that you find it helpful.
这篇关于如何在 Android 设备上获取每个应用程序的类别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!