Creating custom chooser for share intent by package name - android

I have a Text in my android activity and I want give the user option to share it on social apps like whatsapp, line, facebook, twitter etc.
But I want to create a custom chooser so that it won't show unintended apps in the chooser.
I'm aware of this snippet
Intent shareIntent = new Intent(android.content.Intent.ACTION_SEND);
shareIntent.setType("text/plain");
startActivity(Intent.createChooser(shareIntent, "Share via"));
How can I make it so that in the chooser it'd only show the apps which I can specify by their package names.
Thanks

Even though I think this question is duplicated, but since I can't find the duplicated question yet, let me provide an answer first.
List<Intent> targetedShareIntents = new ArrayList<Intent>();
Intent shareInent = new Intent(Intent.ACTION_SEND);
shareInent.setType("text/plain");
List<ResolveInfo> resInfo = activity.getPackageManager().queryIntentActivities(shareInent, 0);
// put the name of the packages you want in this ArrayList
ArrayList<String> wantedPackage = new ArrayList<>();
if (!resInfo.isEmpty()) {
for (ResolveInfo info : resInfo) {
Intent targetedShare = new Intent(android.content.Intent.ACTION_SEND);
targetedShare.setType("text/plain");
String infoPackageName = info.activityInfo.packageName.toLowerCase();
if (wantedPackage.contains(infoPackageName)) {
targetedShare.putExtra(Intent.EXTRA_TEXT, "put your text here");
targetedShare.setPackage(info.activityInfo.packageName.toLowerCase());
targetedShareIntents.add(targetedShare);
resPackageNames.add(infoPackageName);
}
}
Intent chooserIntent = Intent.createChooser(targetedShareIntents.remove(0), "Chooser title");
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, targetedShareIntents.toArray(new Parcelable[]{}));
startActivity(chooserIntent);
}

Related

Intent chooser filtering overrides action names

Using the code from this answer, I'm building an image sharing intent chooser, that should only display a certain set of apps (Instagram, Twitter, Facebook and nothing else).
That works great, but the displaying names of the app are overriden.
With this code:
public static void startImageSharingIntent(Activity caller, List<String> possiblePackagesNames, Uri imgUri) {
try {
List<Intent> targetedShareIntents = new ArrayList<Intent>();
Intent intentTemplate = new Intent(Intent.ACTION_SEND);
intentTemplate.setType("image/*");
List<ResolveInfo> resInfo = caller.getPackageManager().queryIntentActivities(intentTemplate, 0);
if (!resInfo.isEmpty()) {
for (ResolveInfo info : resInfo) {
if (possiblePackagesNames.contains(info.activityInfo.packageName.toLowerCase())) {
Intent targetedShare = new Intent(android.content.Intent.ACTION_SEND);
targetedShare.setType("image/*");
targetedShare.putExtra(Intent.EXTRA_STREAM, imgUri);
targetedShare.setPackage(info.activityInfo.packageName);
//adding bottom line puts the app name in the intent
targetedShare.setClassName(info.activityInfo.packageName, info.activityInfo.name);
targetedShareIntents.add(targetedShare);
}
}
Intent chooserIntent = Intent.createChooser(targetedShareIntents.remove(0), "Share");
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, targetedShareIntents.toArray(new Parcelable[]{}));
caller.startActivity(chooserIntent);
}
} catch (Exception e) {
e.printStackTrace();
}
}
If I comment the line targetedShare.setClassName(info.activityInfo.packageName, info.activityInfo.name);, this is the resulting chooser:
Uncommenting it displays:
In the first one it displays an "Android system" option, which is wrong; but in the second one the label for different actions of Twitter are renamed to Twitter only.
Is there a way to avoid the "Android system" option but not overriding the default intent name?

Android - Using dynamic text field for sharing using chooser dialog

If the user chooses Twitter, I want to send an abbreviated text field (due to character limit). From this SO post -- Branching the Android Share Intent extras depending on which method they choose to share -- I learned that I can implement targeted intents. However, when I use the code below, a number of apps show as "Android System" and Twitter does not show up in the dialog. I even removed the if block trying to catch Twitter, and it still did not appear.
List<Intent> targetedShareIntents = new ArrayList<Intent>();
Intent sharingIntent = new Intent(Intent.ACTION_SEND);
sharingIntent.setType("text/plain");
PackageManager pm = rootView.getContext().getPackageManager();
List<ResolveInfo> activityList = pm.queryIntentActivities(sharingIntent, 0);
for(final ResolveInfo app : activityList){
String packageName = app.activityInfo.packageName;
Intent targetedShareIntent = new Intent(Intent.ACTION_SEND);
targetedShareIntent.setType("text/plain");
if(TextUtils.equals(packageName, TWITTER_PACKAGE_NAME)){
targetedShareIntent.putExtra(Intent.EXTRA_TEXT, getTextToShare(data, "Twitter"));
} else {
targetedShareIntent.putExtra(Intent.EXTRA_SUBJECT, data.getTitle());
targetedShareIntent.putExtra(Intent.EXTRA_TEXT, getTextToShare(data));
}
targetedShareIntent.setPackage(packageName);
targetedShareIntents.add(targetedShareIntent);
}
Intent chooserIntent = Intent.createChooser(targetedShareIntents.remove(targetedShareIntents.size() - 1), "Share this story");
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, targetedShareIntents.toArray(new Parcelable[]{}));
startActivity(chooserIntent);
I'm not really sure what's going on with the following:
Intent.createChooser(targetedShareIntents.remove(targetedShareIntents.size() - 1), "Share this story");
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, targetedShareIntents.toArray(new Parcelable[]{}));
In the SO post, targetedShareIntents.remove(0) was used although targetedShareIntents.remove(targetedShareIntents.size() - 1) was proposed as a more reusable solution. Any help or explanations on this is greatly appreciated.
I think TextUtils.equals(packageName, TWITTER_PACKAGE_NAME) can't find exactly com.twitter.android package name, so you need to try below code for match Twitter package name
if(packageName.toLowerCase().startsWith("com.twitter")){
targetedShareIntent.putExtra(Intent.EXTRA_TEXT, getTextToShare(data, "Twitter"));
} else {
targetedShareIntent.putExtra(Intent.EXTRA_SUBJECT, data.getTitle());
targetedShareIntent.putExtra(Intent.EXTRA_TEXT, getTextToShare(data));
}

Share text/image on Facebook

I am new on progamming android and I have some problems on sharing contents.
I did an android app that involves a test. In the last activity I'd like to offer the opportunity to share the users results via shareintent, here a frame of the code:
display= (TextView) findViewById (R.id.tvDisplay);
display.setText("Well Done, your score is" + score ); //score is an int
sharebutton= (Button)findViewById(R.id.sharebutton);
sharebutton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
results = display.getText().toString();
Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.setType("text/plain");
shareIntent.putExtra(Intent.EXTRA_TEXT,"My score in the test is" + results);
shareIntent.putExtra(Intent.EXTRA_SUBJECT, "TEST");
startActivity(Intent.createChooser(shareIntent, "Share.."));
}
});
The problem is that when I click on the button, I can share the exact text in all of sharer tools (email, wapp, etc..), but not through Facebook. Why?
I tried to allow to post image and text on the FB wall but I can't give the exact path of the image that is on the drawable Folder.
Thank you for all the advices.
It depends upon Facebook app whether they are accepting text or not via intent,I know they accept images and bitmap files but for text they don't accept it.I've already tried this earlier in one of my own project but couldn't supply text via intent.
The Facebook application does not handle either the EXTRA_SUBJECT or EXTRA_TEXT fields.
I have already answer this question here.May be you can follow this link in case you need reference.
first store image in sdcard then share image on facebook
File filePath = new File(Environment
.getExternalStorageDirectory(),
"/foldername/imagename.jpg");
// share("facebook", filePath.toString());
System.out.println(filePath);
List<Intent> targetedShareIntents = new ArrayList<Intent>();
Intent share = new Intent(android.content.Intent.ACTION_SEND);
share.setType("image/jpeg");
List<ResolveInfo> resInfo = getActivity().getPackageManager()
.queryIntentActivities(share, 0);
if (!resInfo.isEmpty()) {
for (ResolveInfo info : resInfo) {
Intent targetedShare = new Intent(
android.content.Intent.ACTION_SEND);
targetedShare.setType("image/jpeg"); // put here your
// mime
// type
if (info.activityInfo.packageName.toLowerCase()
.contains("facebook")
|| info.activityInfo.name.toLowerCase()
.contains("facebook")) {
targetedShare.putExtra(Intent.EXTRA_SUBJECT,
"Sample Photo");
targetedShare.putExtra(Intent.EXTRA_TEXT,
"This photo is created by App Name");
targetedShare.putExtra(Intent.EXTRA_STREAM,
Uri.fromFile(filePath));
targetedShare
.setPackage(info.activityInfo.packageName);
targetedShareIntents.add(targetedShare);
}
}
Intent chooserIntent = Intent.createChooser(
targetedShareIntents.remove(0),
"Select app to share");
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS,
targetedShareIntents.toArray(new Parcelable[] {}));
startActivity(chooserIntent);
}

Android Intent directly to Facebook sharing?

Following code works perfectly,
Intent sharingIntent = new Intent(Intent.ACTION_SEND);
sharingIntent.setType("text/plain");
sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, "text");
sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Title");
startActivity(Intent.createChooser(sharingIntent, "Share using"));
but it open a list and from this list I have to select Facebook and then everything is smooth.
But is there any config changes that can open Facebook sharing directly instead of going through the menu.
Try the following code, now the intent will open Facebook directly.
List<Intent> targetedShareIntents = new ArrayList<Intent>();
Intent facebookIntent = getShareIntent("facebook", "subject", "text_or_url");
// subject may not work, but if you have a url place it in text_or_url
if(facebookIntent != null)
targetedShareIntents.add(facebookIntent);
Intent chooser = Intent.createChooser(targetedShareIntents.remove(0), "Delen");
chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, targetedShareIntents.toArray(new Parcelable[]{}));
startActivity(chooser);
// To precisely get the facebook intent use the following code
private Intent getShareIntent(String type, String subject, String text)
{
boolean found = false;
Intent share = new Intent(android.content.Intent.ACTION_SEND);
share.setType("text/plain");
// gets the list of intents that can be loaded.
List<ResolveInfo> resInfo = getActivity().getPackageManager().queryIntentActivities(share, 0);
System.out.println("resinfo: " + resInfo);
if (!resInfo.isEmpty()){
for (ResolveInfo info : resInfo) {
if (info.activityInfo.packageName.toLowerCase().contains(type) ||
info.activityInfo.name.toLowerCase().contains(type) ) {
share.putExtra(Intent.EXTRA_SUBJECT, subject);
share.putExtra(Intent.EXTRA_TEXT, text);
share.setPackage(info.activityInfo.packageName);
found = true;
break;
}
}
if (!found)
return null;
return share;
}
return null;
}
Above code is extracted from this stackoverflow-post
this should be possible, if you get first all activities, that supports your intent and then get the correct and do a explicit call. To get all activities use context.getPackageManager.queryIntentActivities(Intent, int). The coding shouldn't be a big problem.
Check this link: PackageManager docu

both labels shows "WeChat" when share to wechat friend and timeline

i've a android app to share message to WeChat(without sdk).
When i directly use the StartChooser method, the display-name 'Send to Moment' and 'Send to Chat' display well.
But when i want to remove the apps i donot need using a intent filter as follows, there's problem that both display-name show 'WeChat' rather than 'Send to Moment' and 'Send to Chat'.But at the same time,their icon are right!
Who can tell me how to get the right display label?? Thank you!
Intent it = new Intent(Intent.ACTION_SEND);
it.setType("image/*");
List<ResolveInfo> resInfo = getPackageManager().queryIntentActivities(it, PackageManager.COMPONENT_ENABLED_STATE_DEFAULT);
if (!resInfo.isEmpty()) {
List<Intent> targetedShareIntents = new ArrayList<Intent>();
for (ResolveInfo info : resInfo)
{
Intent targeted = new Intent(Intent.ACTION_SEND);
targeted.setType("image/*");
ActivityInfo activityInfo = info.activityInfo;
if (activityInfo.packageName.contains("tencent.mm") || etc..)
{
targeted.setClassName(activityInfo.packageName, activityInfo.name);
targeted.setPackage(activityInfo.packageName);
targeted.putExtra(Intent.EXTRA_TEXT, "share text");
targeted.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
targetedShareIntents.add(targeted);
}
}
Intent chooserIntent = Intent.createChooser(targetedShareIntents.remove(0), "Share");
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, targetedShareIntents.toArray(new Parcelable[] {}));
startActivity(chooserIntent);
}
!!! update !!! :
if the wechat intent being placed in "Intent.createChooser", the label shown is right, but shown wrong label when placed in "EXTRA_INITIAL_INTENTS".
UPDATE2:
I find the answer at How to make an intent with multiple actions. Using LabeledIntent will solve the problem.Over.
Here is how I get it to work.
CharSequence label = info.loadLabel(getPackageManager());
Intent extraIntents = new LabeledIntent(targeted, activityInfo.packageName, label, info.icon);
targetedShareIntents.add(extraIntents);

Categories

Resources