问题
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