add or remove options from createChooser - android

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);

Related

Intent.EXTRA_INITIAL_INTENTS does not add apps in chooser

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);

Android share intent Twitter: How to share only by Tweet or Direct Message?

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;
}

Android -- Include both text and html sharing services in chooser?

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?

How to remove one item for Sending the User to Another App in android?

How can i remove Google+ from the intent?
Intent intent = new Intent(Intent.ACTION_SEND);
...
// Always use string resources for UI text.
// This says something like "Share this photo with"
String title = getResources().getString(R.string.chooser_title);
// Create intent to show chooser
Intent chooser = Intent.createChooser(intent, title);
// Verify the intent will resolve to at least one activity
if (intent.resolveActivity(getPackageManager()) != null) {
startActivity(chooser);
}
How can i remove Google+ from the intent?
List<Intent> targetIntents = new ArrayList<Intent>();
Intent templateIntent = new Intent(Intent.ACTION_SEND);
templateIntent.setType("text/plain");
List<ResolveInfo> packages = this.getPackageManager().
queryIntentActivities(templateIntent, 0);
// remove desired package
for (ResolveInfo candidate : packages) {
String packageName = candidate.activityInfo.packageName;
if (!packageName.equals("com.google.android.apps.plus")) { //GooglePlus Package name
Intent target = new Intent(android.content.Intent.ACTION_SEND);
target.setType("text/plain");
target.putExtra(Intent.EXTRA_TEXT, "Your Text Here");
target.setPackage(packageName);
targetIntents.add(target);
}
}
Intent chooserIntent = Intent.createChooser(targetIntents.remove(0), "Share.");
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, targetIntents.toArray(new Parcelable[]{}));
startActivity(chooserIntent);

Intent.ACTION_SENDTO show two options , I want to show only one

Intent.ACTION_SENDTO in shows two options but my clent is asking to remove the gmail option and i don't see a way out please help me
Intent emailIntent =
new Intent(Intent.ACTION_SENDTO,
Uri.fromParts( "mailto",userInput.getText().toString(), null));
emailIntent.putExtra(Intent.EXTRA_SUBJECT, "Press Release");
emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, "Please view this press release");
startActivity(Intent.createChooser(emailIntent,"Send mail using..."));
Use
emailIntent.setPackage(PackageName of Email app); before calling startActivity.
You need to set email client package name but, in Samsung devices com.sec.android.email is the default In-Built Mail client, but in HTC it is com.htc.android.mail and so on. So first you need to filter that application and then set to intent. I am adding the solution
Intent emailIntent = new Intent(Intent.ACTION_SENDTO, Uri.fromParts(
"mailto", userInput.getText().toString(), null));
emailIntent.putExtra(Intent.EXTRA_SUBJECT, "Press Release");
emailIntent.putExtra(android.content.Intent.EXTRA_TEXT,
"Please view this press release");
// Identify the package name of email client and set to intent
List<ResolveInfo> resInfo = getPackageManager().queryIntentActivities(emailIntent, 0);
if (!resInfo.isEmpty()) {
for (ResolveInfo info : resInfo) {
if (info.activityInfo.packageName.toLowerCase().contains(".android.email")
|| info.activityInfo.name.toLowerCase().contains(".android.email")) {
emailIntent.setPackage(info.activityInfo.packageName);
// And now call
startActivity(Intent.createChooser(emailIntent, "Send mail using..."));
}
}
}
You should read Android: How to get the native Email clients package name
IF YOU WANT TO REMOVE GMAIL CLIENT FROM LIST create a custom cooser
List<Intent> intents = new ArrayList<Intent>();
Intent sendIntent = new Intent(android.content.Intent.ACTION_SEND);
sendIntent.setType("text/plain");
List<ResolveInfo> resInfo = getPackageManager().queryIntentActivities(sendIntent, 0);
if (!resInfo.isEmpty()){
for (ResolveInfo resolveInfo : resInfo) {
String packageName = resolveInfo.activityInfo.packageName;
Intent neededShareIntent = new Intent(android.content.Intent.ACTION_SEND);
neededShareIntent.setType("text/plain");
neededShareIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Subject");
neededShareIntent.setPackage(packageName);
if (!StringUtils.equals(packageName, "com.google.android.gm")){
intents.add(neededShareIntent);
}
}
Intent chooserIntent = Intent.createChooser(intents.remove(0), "Select app to share");
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, intents.toArray(new Parcelable[]{}));
startActivity(chooserIntent);
}
Pls test this code and check

Categories

Resources