Reputation: 167
Hello I am developing an android app and I do not want to use the system "Share to" window, I want to customize it. So is there any way to get a list of possible share intents and put them in a custom ListView Layout ?
Upvotes: 2
Views: 1125
Reputation: 2937
Yes, you can.
The first step is to build the sharing intent:
Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_TEXT, "This is my text to send.");
sendIntent.setType("text/plain");
And then, instead of creating a chooser intent, you ask the package manager for the activities that can service the sendIntent
:
List<ResolveInfo> infos = getPackageManager().queryIntentActivities(sendIntent, 0);
This will give you the list of activities (apps) that the chooser would also show.
You can get the app's icon and label from ResolveInfo
and show them in a list to the user:
ResolveInfo info;
Drawable icon = info.loadIcon(getPackageManager());
String label = info.loadLabel(getPackageManager());
Once the user has selected a ResolveInfo
you can enrich your sendIntent
with the selected activity to handle your intent and then start the activity:
sendIntent.setClassName(info.activityInfo.packageName, info.activityInfo.name);
startActivity(sendIntent);
We asked the system which activities can handle sendIntent
and then let the user pick one of them. So, by definition, that activity can handle sendIntent
. Setting the class name of the activity on the sendIntent
sends the intent directly to that activity.
Upvotes: 5
Reputation: 1597
this post can be helpful for you.
http://clickclickclack.wordpress.com/2012/01/03/intercepting-androids-action_send-intents/
Upvotes: 1
Reputation: 1326
You can use resolveActivity()
method in Intent
class. So you have to create an Intent to share some content as you do it for now, but instead calling startActivity()
you can retrieve matching Activities by calling resolveActivity()
.
With Activity informations you can build your custom Share dialog/screen.
Sorry for my mistake - resolveActivity
returns only one ComponentName - so you retrieve just package name and not a list but just one object. If you want to retrieve complete info about Activities (multiple activities matching your Intent) you should use PackageManager.queryIntentActivities()
- http://developer.android.com/reference/android/content/pm/PackageManager.html#queryIntentActivities%28android.content.Intent,%20int%29
Upvotes: 1