问题
I am developing an application which lists all application installed in users mobile. I retrieved all applications and I listed it in RecyclerView. Now I want to separate Social Media applications from that list for some other purposes. Is there any way to separate Social Media Apps ? I am using below code to retrieve all apps package names from phone.
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:
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;
}
}
}
}
}
来源:https://stackoverflow.com/questions/50461802/get-list-of-all-social-media-application-installed-in-phone