I am trying to share some data from my app. I have to send different text in case of Email and different text in case user chooses other app.
Intent emailIntent = new Intent(Intent.ACTION_SEND);
emailIntent.setType("ye");
PackageManager pm = getPackageManager();
Intent sendIntent = new Intent(Intent.ACTION_SEND);
sendIntent.setType("text/plain");
Intent openInChooser = Intent.createChooser(emailIntent, "Share via");
List<ResolveInfo> resInfo = pm.queryIntentActivities(sendIntent, 0);
List<LabeledIntent> intentList = new ArrayList<>();
for(int i=0;i<resInfo.size();i++)
{
ResolveInfo ri = resInfo.get(i);
String packageName = ri.activityInfo.packageName;
Log.d("package", i + " " + packageName);
if(packageName.contains("android.email")){
emailIntent.setPackage(packageName);
emailIntent.putExtra(Intent.EXTRA_TEXT, "This is email");
}
else
{
Intent intent = new Intent();
intent.setComponent(new ComponentName(packageName,ri.activityInfo.packageName));
intent.setAction(Intent.ACTION_SEND);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_TEXT, "Sharing via other app");
intent.setPackage(packageName);
intentList.add(new LabeledIntent(intent, packageName, ri.loadLabel(pm), ri.icon));
}
}
LabeledIntent [] extraIntents = new LabeledIntent[intentList.size()];
extraIntents = intentList.toArray(extraIntents);
openInChooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, extraIntents);
startActivity(openInChooser);
I have given a random string in my original intent in setType so that it displays only the chosen apps.However when I click on share an empty window comes up.
I have checked with debugger and my extraIntents contains 24 intents. Its after startActivity that nothing comes up in my choosing options.
Update (Sept 9, 2020):
It seems the intents passed with EXTRA_INITIAL_INTENTS does not work the same on Android OS 10 and above. On the share sheet, it shows a new section with text, "Direct share not available", and share sheet does not show apps like WhatsApp that supports direct share.
Original Answer
In your case, emailIntent returns 0 apps because of random Type you have set. In
Intent openInChooser = Intent.createChooser(emailIntent, "Share via");
if the intent you pass returns 0 apps then it ignores EXTRA_INITIAL_INTENTS flag.
Possible solution,
......
extraIntents = intentList.toArray(extraIntents);
Intent firstIntent = extraIntents.remove(0); // assuming you will have at least one Intent
Intent openInChooser = Intent.createChooser(firstIntent, "Share via");
openInChooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, extraIntents);
startActivity(openInChooser);
Related
I want to get emails from users. I am trying to filter the chooser intent but i am stuck at some point.
Chooser intent brings the user's contacts. I just want to show the installed email apps, with 'to' field auto filled of course.
How can I remove user's contacts and show email apps only?
any help would be greatly appreciated.
here is my code so far:
List<Intent> emailAppLauncherIntents = new ArrayList<>();
Intent emailAppIntent = new Intent(Intent.ACTION_SENDTO);
emailAppIntent.setData(Uri.parse("mailto:test#mail.com"));
emailAppIntent.putExtra(Intent.EXTRA_EMAIL, "");
emailAppIntent.putExtra(Intent.EXTRA_SUBJECT, "Feedback on "+getContext().getPackageName());
emailAppIntent.putExtra(Intent.EXTRA_TEXT, mailBody);
PackageManager packageManager = getActivity().getPackageManager();
List<ResolveInfo> emailApps = packageManager.queryIntentActivities(emailAppIntent, PackageManager.MATCH_ALL);
for (ResolveInfo resolveInfo : emailApps) {
String packageName = resolveInfo.activityInfo.packageName;
Intent launchIntent = packageManager.getLaunchIntentForPackage(packageName);
emailAppLauncherIntents.add(launchIntent);
}
Intent chooserIntent = Intent.createChooser(new Intent(), "Select email app:");
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, emailAppLauncherIntents.toArray(new Parcelable[emailAppLauncherIntents.size()]));
startActivity(chooserIntent);
Changing the MIME type is the answer
Use
intent.setType("message/rfc822");
this is how i made it.
Intent intent = new Intent(Intent.ACTION_VIEW);
Uri data = Uri.parse("mailto:"
+ "xyz#abc.com"
+ "?subject=" + "Feedback" + "&body=" + "");
intent.putExtra(Intent.EXTRA_SUBJECT, "Feedback";
intent.putExtra(Intent.EXTRA_EMAIL, new String[] { "xyz#abc.com" });
intent.putExtra(Intent.EXTRA_TEXT, mailBody);
intent.setData(data);
startActivity(intent);
I need to share an image via only Tweet.
Here is my code
Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.setType("image/*");
if (mInsertableToGallery) {
mInsertableToGallery = false;
mInsertedImagePath = MediaStore.Images.Media.insertImage(getContentResolver(), mShareImage,
getResources().getString(R.string.app_name) + System.currentTimeMillis(), getResources().getString(R.string.app_name));
}
// share only 5 specific apps:
List<Intent> targetedShareIntents = new ArrayList<>();
List<ResolveInfo> resInfos = getPackageManager().queryIntentActivities(shareIntent, 0);
if (!resInfos.isEmpty()) {
for (ResolveInfo resolveInfo : resInfos) {
String packageName = resolveInfo.activityInfo.packageName;
Intent targetedShareIntent = new Intent(Intent.ACTION_SEND);
targetedShareIntent.setType("image/*");
targetedShareIntent.setPackage(packageName);
targetedShareIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse(mInsertedImagePath));
if (TextUtils.equals(packageName, "com.facebook.katana") //fb
||TextUtils.equals(packageName, "com.instagram.android") //instagram
||TextUtils.equals(packageName, "jp.naver.line.android") //line
||TextUtils.equals(packageName, "com.google.android.apps.plus") // plus
||TextUtils.equals(packageName, "com.twitter.android")) // twitter
{
targetedShareIntents.add(targetedShareIntent);
}
}
}
Intent chooser = Intent.createChooser(targetedShareIntents.remove(0), "");
chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, targetedShareIntents.toArray(new Parcelable[]{}));
startActivity(chooser);
and I get the result:
When click "Android System", Twitter share popup:
What I want:
How to share via Twitter only by Tweet or Direct Message ?
How to change title & icon of share item. Ex Twitter share item from "Android System" to "Tweet"?
I couldn't manage to get both Tweet and Direct Message to the front but the following code will get you either one there. Actually you can get both to the front but both appears with same name "Tweet" so its kind of unusable.
I have also included facebook, insta and tumblr.
To get Direct Message in place of tweet replace com.twitter.android.composer.ComposerActivity with com.twitter.android.DMActivity. To get both add another ' if ' for DMActivity.
Can't say surely but I don't think you can change the Icon of the package for your app.
List<Intent> targetedShareIntents = new ArrayList<Intent>();
Intent shareIntent = new Intent(android.content.Intent.ACTION_SEND);
shareIntent.setType("image/jpeg");//("text/plain");
Uri uri = Uri.fromFile(outFile);
shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
List<ResolveInfo> resInfo = getPackageManager().queryIntentActivities(shareIntent, 0);
for (ResolveInfo resolveInfo : resInfo) {
String packageName = resolveInfo.activityInfo.packageName;
Intent targetedShareIntent = new Intent(android.content.Intent.ACTION_SEND);
targetedShareIntent.setType("image/jpeg");
targetedShareIntent.putExtra(Intent.EXTRA_STREAM, uri);
if(TextUtils.equals(packageName,"com.facebook.katana")|TextUtils.equals(packageName,"com.instagram.android")
|TextUtils.equals(packageName,"com.tumblr")) {
targetedShareIntent.setPackage(packageName);
targetedShareIntents.add(targetedShareIntent);
}
if(TextUtils.equals(resolveInfo.activityInfo.name,"com.twitter.android.composer.ComposerActivity")) {
targetedShareIntent.setPackage(packageName);
targetedShareIntent.setClassName(
resolveInfo.activityInfo.packageName,
resolveInfo.activityInfo.name);
targetedShareIntents.add(targetedShareIntent);
}
}
Intent chooserIntent = Intent.createChooser(targetedShareIntents.remove(0), "Select app to share");
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, targetedShareIntents.toArray(new Parcelable[]{}));
startActivity(chooserIntent);
For composing new Tweet:
public static Intent getTwitterShareIntent(Context context) {
Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.setType("text/plain");
shareIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
shareIntent.setClassName("com.twitter.android",
"com.twitter.composer.ComposerShareActivity");
return shareIntent;
}
I want to share dynamic text fields for Facebook, Twitter, email and SMS apps. For email, I want to include hyperlinks, so I'd need to use "text/html" instead of "text/plain". Is this impossible? The following code does not work:
List<Intent> targetedShareIntents = new ArrayList<Intent>();
Intent sharingIntent = new Intent(Intent.ACTION_SEND);
sharingIntent.setType("text/plain");
PackageManager pm = getActivity().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");
targetedShareIntent.putExtra(Intent.EXTRA_TEXT, getTextToShare(data));
targetedShareIntent.setPackage(packageName);
targetedShareIntents.add(targetedShareIntent);
}
Intent emailIntent = new Intent(Intent.ACTION_SEND);
emailIntent.setType("text/html");
List<ResolveInfo> emailList = pm.queryIntentActivities(emailIntent, 0);
for(final ResolveInfo app : emailList) {
String packageName = app.activityInfo.packageName;
Intent targetedShareIntent = new Intent(Intent.ACTION_SEND);
targetedShareIntent.setType("text/html");
if (packageName.contains("android.email") || packageName.contains("android.gm")) {
targetedShareIntent.putExtra(Intent.EXTRA_SUBJECT, data.getHeadline());
targetedShareIntent.putExtra(Intent.EXTRA_TEXT, Html.fromHtml(getTextToShare(data, "Email")));
}
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);
EDIT: This does work for Facebook, email -- however, for some reason Twitter does NOT show up. Does anyone know why?
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);
}
String message = "Text I want to share";
Intent share = new Intent(Intent.ACTION_SEND);
share.setType("text/plain");
share.putExtra(Intent.EXTRA_TEXT, message);
startActivity(Intent.createChooser(share,"Share on"));
right now it shows the default options like: Bluetooth, Email, Facebook, Gmail, LinkedIn, Messaging, Share Via Barcode.
or are these the installed apps?
what i want is to know, how i can remove few from this list. like i want to remove Share Via Barcode.
and add something else?
Thank You
Use below code to add a new Item to the Chooser Screen.
Intent share = new Intent(Intent.ACTION_SEND);
share.setType("text/plain");
share.putExtra(Intent.EXTRA_TEXT, message);
Intent addIntent = ;//whatever you want
Intent chooser = new Intent(Intent.ACTION_CHOOSER);
chooser.putExtra(Intent.EXTRA_INTENT, share );
chooser.putExtra(Intent.EXTRA_TITLE, "title");
Intent[] intentArray = {addIntent };
chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, intentArray);
startActivity(chooser);
But removing specific items is not possible. So you could resolve the intent using Packagemanager.resolveActivity and create your own custom list view
I used the following code to get a list of all eMail and SMS apps installed on the device:
Intent shareSMS = new Intent(Intent.ACTION_SENDTO, Uri.fromParts("smsto", "12346556", null));
shareSMS.addCategory(Intent.CATEGORY_DEFAULT);
shareSMS.putExtra("sms_body", message);
Intent shareEmail = new Intent(Intent.ACTION_SENDTO, Uri.fromParts("mailto", "", null));
PackageManager pm = getPackageManager();
List<ResolveInfo> mailActivityList = pm.queryIntentActivities(shareEmail, PackageManager.MATCH_DEFAULT_ONLY);
List<Intent> mailIntents = new ArrayList<Intent>();
for (ResolveInfo resInfo : mailActivityList) {
Intent targetedOpenIntent = new Intent(android.content.Intent.ACTION_SENDTO, Uri.fromParts("mailto", "", null))
.setPackage(resInfo.activityInfo.packageName)
.putExtra(Intent.EXTRA_EMAIL, emails)
.putExtra(Intent.EXTRA_SUBJECT, subject)
.putExtra(Intent.EXTRA_TEXT, message);
mailIntents.add(targetedOpenIntent);
}
Intent chooser = new Intent(Intent.ACTION_CHOOSER);
chooser.putExtra(Intent.EXTRA_INTENT, shareSMS);
chooser.putExtra(Intent.EXTRA_TITLE, "Send request");
chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, mailIntents.toArray(new Parcelable[] { }));
startActivity(chooser);
Adding Option to chooser
Intent share = new Intent(Intent.ACTION_SEND);
share.setType("text/plain");
share.putExtra(Intent.EXTRA_TEXT, message);
Intent extraOptionToAdd = new Intent(this, ExtraOptionActivity.class);
extraOptionToAdd.putExtra(Intent.EXTRA_TEXT, "Text");
LabeledIntent labeledExtraOption = new LabeledIntent(extraOptionToAdd, getPackageName(), "Extra Option!", 0);
Intent chooser = Intent.createChooser(share, "Share Now!");
Intent[] intentArray = {labeledExtraOption};
chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, intentArray);
startActivity(chooser);