Get list of applications which can share data

别等时光非礼了梦想. 提交于 2019-12-10 14:19:33

问题


This code shows default share dialog

Intent sharingIntent = new Intent(Intent.ACTION_SEND);
sharingIntent.setType("text/html");
sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, "Message"));
startActivity(Intent.createChooser(sharingIntent,"Share using"));

Question: Instead of showing the list of applications in the default system dialog, I want to get the list of applications and show them in my custom list.


回答1:


Use the PackageManager with the Intent to get the list of applications which can listen to the SEND intent. From the list of applications returned, get the details you would like to display, eg. the icon, name, etc. You would need the package name to launch the app when the user clicks on it.

PackageManager pm = getActivity().getPackageManager();
Intent mainIntent = new Intent(Intent.ACTION_SEND, null);
mainIntent.setType("text/plain");
List<ResolveInfo> resolveInfos = pm.queryIntentActivities(mainIntent, 0); // returns all applications which can listen to the SEND Intent
for (ResolveInfo info : resolveInfos) {
    ApplicationInfo applicationInfo = info.activityInfo.applicationInfo;

    //get package name, icon and label from applicationInfo object and display it in your custom layout 

    //App icon = applicationInfo.loadIcon(pm);
    //App name  = applicationInfo.loadLabel(pm).toString();
    //App package name = applicationInfo.packageName;
}

After you have this set of application details, you can use this in the Adapter of your GridView and show the details.




回答2:


So instead of the usual popup that shows applications in a list you want a custom popup that shows the applications in a grid view?

This is possible by creating a popup with a grid view yourself. Regardless of it being a share action. Then build a list of applications you would like to show. You can get these using the resolveActivity method from Intent (or see Swayam's answer).
Then use that list to populate the grid view.



来源:https://stackoverflow.com/questions/37704682/get-list-of-applications-which-can-share-data

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!