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
Related
I have the following code which used to send text from my app to Email:
Intent mail = new Intent(Intent.ACTION_VIEW);
mail.setClassName("com.google.android.gm","com.google.android.gm.ComposeActivityGmail");
mail.putExtra(Intent.EXTRA_EMAIL, new String[] { });
mail.setData(Uri.parse(""));
mail.putExtra(Intent.EXTRA_SUBJECT, "Country Decryption");
mail.setType("plain/text");
mail.putExtra(Intent.EXTRA_TEXT, "my text");
ctx.startActivity(mail);
It works , but as you see, it uses Gmail app, how do I make it use Email application instead of Gmail?
I mean this app:
and what about sharing to Facebook ? I found that Facebook does not support sharing using intent anymore, and I have to use Facebook SDK, but I couldn't find any simple procedure to do that. Is there any simple way?
Regards.
well to use other email apps am afraid you would have to create a chooser dialog and let the user choose which app to use, something like this
Intent emailIntent = new Intent(Intent.ACTION_SENDTO, Uri.fromParts(
"mailto","abc#mail.com", null));
emailIntent.putExtra(Intent.EXTRA_EMAIL, "address");
emailIntent.putExtra(Intent.EXTRA_SUBJECT, "Subject");
emailIntent.putExtra(Intent.EXTRA_TEXT, "Body");
startActivity(Intent.createChooser(emailIntent, "Send Email..."));
You have to create a specific filter on your ACTION_SEND and you can read a complete answer here.
This is the code in which you can choose which app show in the app chooser
List<ResolveInfo> resInfo = pm.queryIntentActivities(sendIntent, 0);
List<LabeledIntent> intentList = new ArrayList<LabeledIntent>();
for (int i = 0; i < resInfo.size(); i++) {
// Extract the label, append it, and repackage it in a LabeledIntent
ResolveInfo ri = resInfo.get(i);
String packageName = ri.activityInfo.packageName;
if(packageName.contains("android.email")) {
emailIntent.setPackage(packageName);
} else if(packageName.contains("twitter") || packageName.contains("facebook") || packageName.contains("mms") || packageName.contains("android.gm")) {
Intent intent = new Intent();
intent.setComponent(new ComponentName(packageName, ri.activityInfo.name));
intent.setAction(Intent.ACTION_SEND);
intent.setType("text/plain");
if(packageName.contains("twitter")) {
intent.putExtra(Intent.EXTRA_TEXT, resources.getString(R.string.share_twitter));
} else if(packageName.contains("facebook")) {
// Warning: Facebook IGNORES our text. They say "These fields are intended for users to express themselves. Pre-filling these fields erodes the authenticity of the user voice."
// One workaround is to use the Facebook SDK to post, but that doesn't allow the user to choose how they want to share. We can also make a custom landing page, and the link
// will show the <meta content ="..."> text from that page with our link in Facebook.
intent.putExtra(Intent.EXTRA_TEXT, resources.getString(R.string.share_facebook));
} else if(packageName.contains("mms")) {
intent.putExtra(Intent.EXTRA_TEXT, resources.getString(R.string.share_sms));
} else if(packageName.contains("android.gm")) { // If Gmail shows up twice, try removing this else-if clause and the reference to "android.gm" above
intent.putExtra(Intent.EXTRA_TEXT, Html.fromHtml(resources.getString(R.string.share_email_gmail)));
intent.putExtra(Intent.EXTRA_SUBJECT, resources.getString(R.string.share_email_subject));
intent.setType("message/rfc822");
}
intentList.add(new LabeledIntent(intent, packageName, ri.loadLabel(pm), ri.icon));
}
}
This works to intent just the gmail app.
Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
sharingIntent.setType("text/plain");
sharingIntent.setPackage("com.google.android.gm");
String shareBody = "Here is the share content body";
sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Subject Here");
sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, shareBody);
startActivity(sharingIntent);
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 am sending mail via my app with this code:
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_EMAIL, new String[] {"email#example.com"});
intent.putExtra(Intent.EXTRA_SUBJECT, "subject here");
intent.putExtra(Intent.EXTRA_TEXT, "body text");
Uri uri = Uri.parse("file://" + file_name+".jpg");
intent.putExtra(Intent.EXTRA_STREAM, uri);
startActivity(Intent.createChooser(intent, "Send email..."));
This works just fine, but how can I do this without Intent.createChooser and do not let the user choose app for sharing and go directly to Gmail app, assuming each android phone have it.
You can used the following code to open whatever intent you want eg gmail, facebook, email etc..Simple in the type as used in code pass "gmail" if you want to open gmail, pass "face" if u want to open facebook
Intent intent = new Intent(android.content.Intent.ACTION_SEND);
intent.setType("text/html");
List<ResolveInfo> resInfo = getPackageManager().queryIntentActivities(intent, 0);
if (!resInfo.isEmpty())
{
for (ResolveInfo info : resInfo)
{
if (info.activityInfo.packageName.toLowerCase().contains(type) || info.activityInfo.name.toLowerCase().contains(type))
{
intent.putExtra(android.content.Intent.EXTRA_TEXT, htmlBody);
intent.setPackage(info.activityInfo.packageName);
startActivity(Intent.createChooser(intent, getResources().getString(R.string.share_send_text)));
}
}
I want to open the default email client instead of showing the options. I tried but i am not getting please can anyone help me.
I used the following code:
final Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
emailIntent.setType("text/html");
emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "My Allergy Journal");
emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, Html.fromHtml("<small>"+sb.toString()+"</small>"));
startActivity(Intent.createChooser(emailIntent, "Email:"));
It's show the options
But I want to open then Default email client directly.
Frame a String in the format String URI="mailto:?subject=" + subject + "&body=" + body;
and
Intent intent = new Intent(Intent.ACTION_VIEW);
Uri data = Uri.parse(URI);
intent.setData(data);
startActivity(intent);
This will open up the default e-mail program selected by the user.
Linkify does it this way. Check out it's source code, if you like.
You can used the following code to open whatever intent you want eg gmail, facebook, email etc..Simple in the type as used in my code pass "gmail" if you want to open gmail, pass "face" if u want to open facebook
Intent intent = new Intent(android.content.Intent.ACTION_SEND);
intent.setType("text/html");
List<ResolveInfo> resInfo = getPackageManager().queryIntentActivities(intent, 0);
if (!resInfo.isEmpty())
{
for (ResolveInfo info : resInfo)
{
if (info.activityInfo.packageName.toLowerCase().contains(type) || info.activityInfo.name.toLowerCase().contains(type))
{
intent.putExtra(android.content.Intent.EXTRA_TEXT, htmlBody);
intent.setPackage(info.activityInfo.packageName);
startActivity(Intent.createChooser(intent, getResources().getString(R.string.share_send_text)));
}
}
Now i can use the share intent to open the share dialog
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_TEXT, link);
startActivity(Intent.createChooser(intent, "Share with"));
but i need the dialog not to appear and share directly to one social network for example (FB or twitter)
any advice how to do that ?
There is a way to directly open the intent you wants. You can get the list of intents and open only one.
See this code:
private void initShareIntent(String type) {
boolean found = false;
Intent share = new Intent(android.content.Intent.ACTION_SEND);
share.setType("image/jpeg");
// gets the list of intents that can be loaded.
List<ResolveInfo> resInfo = getPackageManager().queryIntentActivities(share, 0);
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, "your text");
share.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(new File(myPath)) ); // Optional, just if you wanna share an image.
share.setPackage(info.activityInfo.packageName);
found = true;
break;
}
}
if (!found)
return;
startActivity(Intent.createChooser(share, "Select"));
}
}
If you wanna open twitter, do that:
initShareIntent("twi");
if facebook:
initShareIntent("face");
if mail:
initShareIntent("mail"); // or "gmail"
If you wanna show a list of intents that match with the type, insted of use the first mach, see this post: Android Intent for Twitter application
No you can't. Intent's are supposed to work this way. If you have to force a particular app to open, use explicit intents if the target apps support those. Without knowing the package names or the component names of the target apps, or the type or mime type of data, you can't force a particular app to work on generalized intents.
First, list all apps to share.
private java.util.List<ResolveInfo> showAllShareApp() {
java.util.List<ResolveInfo> mApps = new ArrayList<ResolveInfo>();
Intent intent = new Intent(Intent.ACTION_SEND, null);
intent.putExtra(Intent.EXTRA_TEXT, "This is my text to send.");
intent.setType("text/plain");
PackageManager pManager = getPackageManager();
mApps = pManager.queryIntentActivities(intent,
PackageManager.COMPONENT_ENABLED_STATE_DEFAULT);
return mApps;
}
Then, select one
private void share(ResolveInfo appInfo) {
Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_TEXT, "This is my text to send.");
if (appInfo != null) {
sendIntent
.setComponent(new ComponentName(
appInfo.activityInfo.packageName,
appInfo.activityInfo.name));
}
sendIntent.setType("text/plain");
// startActivity(Intent.createChooser(sendIntent, "Share"));
startActivity(sendIntent);
}
Here's an sample project.Hope it helps.
You can get all email client using ACTION_SENDTO like
Intent getMailClients = new Intent(Intent.ACTION_SENDTO);
getMailClients.setData(Uri.parse("mailto:"));
final PackageManager pm = this.getPackageManager();
final List<ResolveInfo> emailsClients = pm.queryIntentActivities(getMailClients, 0);
if (emailsClients.size() == 0) {
Toast.makeText(this, "There are no email clients installed", Toast.LENGTH_LONG).show();
return;
}
and then create your own chooser(dialog with list of founded apps). When user click on item you can do smth like this.
Intent sendMailIntent = new Intent(Intent.ACTION_SEND);
sendMailIntent.setType(some type like text/plain or other you need);
...
there you can set SUBJECT,EMAILTO, attach files
...
final List<ResolveInfo> matches = pm.queryIntentActivities(sendMailIntent, 0);
ResolveInfo sendingProgramm = null;
for (final ResolveInfo info : matches) {
if (info.activityInfo.packageName.equals(clickedResolveInfo.activityInfo.packageName)) {
sendingProgramm = info;
sendMailIntent
.setClassName(sendingProgramm.activityInfo.packageName, sendingProgramm.activityInfo.name);
break;
}
}
startActivity(sendMailIntent);
Maybe it will help you.