When I have the contact id, how do I send an SMS/email (aka text) to it?
I've seen code like this, but none are using the contact it.
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_TEXT, text);
act.startActivityForResult(Intent.createChooser(intent, ""), 0);
If you wish to send the text message directly from your app instead of throwing the intent, you can use sendTextMessage() in android.telephony.SmsManager, see the docs here. That does mean a small annoyance for your user, as well as requiring the permission in the manifest.
To do what you are doing above with the phone number filled in, you have to include: intent.putExtra("address", "07910123456");
Thus, the completed code is:
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_TEXT, text);
intent.putExtra("address", "07910123456");
act.startActivityForResult(Intent.createChooser(intent, ""), 0);
Related
I have a FeedbackActivity.java activity which takes feedback from user with multiple attachments (upto 3 images as attachments).
I am using following code:
Intent intent = new Intent(Intent.ACTION_SEND_MULTIPLE);
intent.setType("message/rfc822");
intent.putExtra(Intent.EXTRA_EMAIL, emails); //emails is an Array of 'String' type
intent.putExtra(Intent.EXTRA_SUBJECT, subject); //subject is a String
intent.putExtra(Intent.EXTRA_TEXT, text) //text is a String
intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris); //uris is an ArrayList of 'Uri' type
//uris stores all Uri of images selected
if(intent.resolveActivity(getPackageManager()) != null){
startActivity(intent);
}
else {
Toast.makeText(this, "Not Good", Toast.LENGTH_SHORT).show();
}
Now this code works fine but the problem is that it shows all sorts of apps which support "message/rfc822" MIME type.
Image is shown below :
I only need to show the email client apps, I tried Uri.parse("mailto:"), but didn't workout and code always moves to else statement and shows the toast "not good".
I read the google documentation but it only shows simple cases.
I tried searching on the web. Many developers are using intent.setType("*/*") or intent.setType("text/plain"). But they all too show apps other than email clients.
Please guide me.
And I wanted to ask in general,
Google documentations show simple examples which is good in a way, but how to learn really in depth on these kind of topics?
Thank you.
So here, we will be using two intents: selectorIntent and emailIntent. selectorIntent is what the emailIntent will use as to show available apps. code:
Intent selectorIntent = new Intent(Intent.ACTION_SENDTO);
selectorIntent.setData(Uri.parse("mailto:"));
final Intent emailIntent = new Intent(Intent.ACTION_SEND_MULTIPLE);
emailIntent.putExtra(Intent.EXTRA_EMAIL, emails);
emailIntent.putExtra(Intent.EXTRA_SUBJECT, subject);
emailIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris);
emailIntent.setSelector(selectorIntent);
if(emailIntent.resolveActivity(getPackageManager()) != null){
startActivity(emailIntent);
}
else {
Snackbar.make(scrollView, "Sorry, We couldn't find any email client apps!", Snackbar.LENGTH_SHORT).show();
}
Now it will choose only apps which are email client.
If there is only one email-client app in your phone than it will directly open that. And if no such application is there, than the code will show Snackbar given in the else part.
Don't use Uri.parse, use Uri.fromParts
Do it like this:
Intent emailIntent = new Intent(Intent.ACTION_SENDTO, Uri.fromParts("mailto","example#mail.com", null));
I am using intent to get the list of all apps i can post text to. However, linkedin is not appearing in that list. Do i need to do anything extra for linkedin?
Intent intent = new Intent();
intent.setAction(Intent.ACTION_SEND);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_TEXT, "dcsd");
package= mContext.getPackageManager();
List<ResolveInfo> appTargets= package.queryIntentActivities(shareIntent, 0);
I am able to get all other apps like Facebook, Twitter except LinkedIn?
What could be the possible reason?
Code is
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("text/plain");
String shareBody = "https://developers.facebook.com/docs/android/share";
intent.putExtra(Intent.EXTRA_TITLE,"Share From Test App");
intent.putExtra(Intent.EXTRA_TEXT,shareBody);
intent.putExtra(Intent.EXTRA_SUBJECT, "hello");
startActivity(Intent.createChooser(intent, "Share With"));
Add this to the intent:
intent.putExtra(Intent.EXTRA_SUBJECT, "dsvs");
In my Android app, i've inserted an ACTION_SEND intent.
Intent intent = new Intent(Intent.ACTION_SEND);
intent.putExtra(Intent.EXTRA_EMAIL, new String[] { emailTo });
intent.putExtra(Intent.EXTRA_CC, new String[] { emailCC });
intent.putExtra(Intent.EXTRA_SUBJECT, defaultSubject);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_TEXT, body);
Is there a way to get the recipient filled by the user after he sent the mail ?
Thanks.
No, sorry. You do not get any results from an ACTION_SEND operation, including any changes the user may have made to the recipient list of an email.
I want to display an activity chooser that shows all apps that can VIEW and/or EDIT some data. Is there an easy way to do this, or do I have to implement my own activity chooser dialog? Or maybe I can just subclass Intent? Thanks.
I found a partial solution by using EXTRA_INITIAL_INTENTS:
Intent viewIntent = new Intent(Intent.ACTION_VIEW);
Intent editIntent = new Intent(Intent.ACTION_EDIT);
viewIntent.setDataAndType(uri, type);
editIntent.setDataAndType(uri, type);
Intent chooserIntent = Intent.createChooser(editIntent, "Open in...");
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { viewIntent });
startActivity(chooserIntent);
I say partial because if an app supports both ACTION_VIEW and ACTION_EDIT it will show up twice in the list, one of which will open the file for viewing and the other for editing, and you wouldn't necessarily know which is which. I think a complete solution would require a custom app chooser, as Tim suggested.
EDIT (Complete Solution!):
I found a solution that doesn't involving writing a custom app chooser. In order to differentiate ACTION_EDIT apps from ACTION_VIEW apps, I found a way to append a "(for editing)" string to the labels for one of them (in my case, ACTION_EDIT) by using the line of code Tim provided. In addition, to ensure the appended string doesn't appear to be a part of the app name, I changed the color of it to cyan:
PackageManager pm = kyoPrint.getPackageManager();
Intent viewIntent = new Intent(Intent.ACTION_VIEW);
Intent editIntent = new Intent(Intent.ACTION_EDIT);
viewIntent.setDataAndType(uri, type);
editIntent.setDataAndType(uri, type);
Intent openInChooser = Intent.createChooser(viewIntent, "Open in...");
// Append " (for editing)" to applicable apps, otherwise they will show up twice identically
Spannable forEditing = new SpannableString(" (for editing)");
forEditing.setSpan(new ForegroundColorSpan(Color.CYAN), 0, forEditing.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
List<ResolveInfo> resInfo = pm.queryIntentActivities(editIntent, 0);
Intent[] extraIntents = new Intent[resInfo.size()];
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;
Intent intent = new Intent();
intent.setComponent(new ComponentName(packageName, ri.activityInfo.name));
intent.setAction(Intent.ACTION_EDIT);
intent.setDataAndType(uri, type);
CharSequence label = TextUtils.concat(ri.loadLabel(pm), forEditing);
extraIntents[i] = new LabeledIntent(intent, packageName, label, ri.icon);
}
openInChooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, extraIntents);
startActivity(openInChooser);
EDIT 2: BUG
If there are no activities found by the first intent, NO activities will be displayed, including any found by the second intent. I ended up writing my own chooser. I just populated an ExpandableListView with headings for each type of intent with their respective activities as children (stored as individual LabeledIntents).
depends on what your data is. But in general using with ACTION_VIEW and some data attached you can use an IntentChoooser to populate the list of choices to the user.
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_TEXT, "some data");
startActivity(Intent.createChooser(intent, "Open with"));
Be sure to set your type correctly so that applications will know that you are wanting to open something that they may be able to handle.
EDIT: I think you would have to use a package manager query to get your two lists then combine them into one and make your own activity / dialog that will pop-up and get populated with the data contained in your combined list.
Here is an example making the query:
List<ResolveInfo> resInfo = getPackageManager().queryIntentActivities(intent, 0);
so if you make your two Intents and call this twice, passing in each intent you should be able to combine the resulting lists to get your full set of possibilities. Then it is up to to create an activity or dialog to show them with.
I have a requirement that I need to send email from my application, I am using below code to send email...
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("Text/Plain");
intent.putExtra(Intent.EXTRA_EMAIL, new String[]{abc#gmail.com});
intent.putExtra(Intent.EXTRA_TEXT, "hello..");
startActivity(Intent.createChooser(intent, email_chooser_title));
The above code launching the email composer. But after I press on send button , I can see one toast message "Message Sending" , But my message not sent.
pl. help me to figure out where I did wrong in this, or let me know if any alternative to solve this.. thanks.
Intent i = new Intent(Intent.ACTION_SEND);
//i.setType("text/plain"); //use this line for testing in the emulator
i.setType("message/rfc822") ; // use from live device
i.putExtra(Intent.EXTRA_EMAIL, new String[]{"test#gmail.com"});
i.putExtra(Intent.EXTRA_SUBJECT,"subject goes here");
i.putExtra(Intent.EXTRA_TEXT,"body goes here");
startActivity(Intent.createChooser(i, "Select email application."));