I would like to pre fill a SMS for user who uses facebook messenger.
Here you can find my Kotlin code :
val intent = Intent(Intent.ACTION_SENDTO)
intent.data = Uri.parse("sms:" + smsToString)
intent.putExtra("sms_body", provider.getSmsBody())
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
val list = context!!.packageManager.queryIntentActivities(intent,
PackageManager.MATCH_DEFAULT_ONLY)
val context = activity!!.applicationContext
if (list.isNotEmpty()) {
context.startActivity(intent)
}
This code launch the facebook messenger app with the new sms window, and with the list of contacts prefilled.
But, the sms_body is empty. The same code works when the default sms app is Android Messages.
Is there a solution ?
Thanks :)
Related
I am trying to redirect user to check his email via available email clients. Currently what I have achieved is to send the user directly to Gmail client. Below there are some functions that I use to generate implicit intent:
fun attept1(): Intent {
val intent = Intent.makeMainSelectorActivity(
Intent.ACTION_MAIN,
Intent.CATEGORY_APP_EMAIL
)
intent.addFlags(FLAG_ACTIVITY_NEW_TASK)
return intent
}
fun attempt2(): Intent {
val intent = Intent(ACTION_VIEW)
intent.type = "message/rfc822"
intent.addFlags(FLAG_ACTIVITY_NEW_TASK)
return intent
}
//... somewhere in the code
val emailIntent = attempt1() //or attempt2()
startActivity(emailIntent)
With attempt 1 I am successfully redirecting the user to Gmail but I do have at least 5 email clients installed (BlueMail, Outlook, Yahoo Mail, Email, Temp Mail). They're ignored completely.
With attempt 2, at first it was showing unrelated apps like Slack, but after I have installed other email clients it just shows a toast message: Can't open this file. I have tried with Intent.createChooser(intent, "Choose client: ") but it didn't work either.
I'm trying to open an mail application on Android via intent. The purpose is to have a button in the app that will open your mail inbox. However when I use:
val intent = Intent(Intent.ACTION_MAIN)
intent.addCategory(Intent.CATEGORY_APP_EMAIL)
intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
startActivity(Intent.createChooser(intent, "Email"))
It always opens gmail, while I also have Outlook installed. The only way I get to choose between the mail apps is when yo use the mailto. But I don't intent to send an email, so it is not desired to use that intent.
Doesn't Outlook support this Intent?
use the following snippet to open to outlook directly from a different app
context.startActivity(
Intent().apply {
action = Intent.ACTION_MAIN
addCategory(Intent.CATEGORY_LAUNCHER)
component = ComponentName(
outlookLaunchIntent?.component?.packageName,
outlookLaunchIntent?.component?.className
)
setPackage(outlookLaunchIntent.package)
}
)
com.microsoft.office.outlook is the package name for outlook
For sending it to all email clients use a uri like this
Intent intent = new Intent(Intent.ACTION_VIEW);
Uri data = Uri.parse("mailto:?subject=" + subject + "&body=" + body);
intent.setData(data);
startActivity(intent);
Ref:How to open Email program via Intents (but only an Email program)
I would like to know if it is possible to have users open an email app of their choice without sending an email.
val intent = Intent(Intent.ACTION_SEND)
intent.type = "plain/text"
startActivity(Intent.createChooser(intent, "Choose Email"))
This allows the user to choose, but it opens the compose. I want just to open the email app.
This is a bit tricky, but not impossible.
Find all email clients
val resolveIntent = Intent(Intent.ACTION_SENDTO)
resolveIntent.setData(Uri.parse("mailto:default#recipient.com"))
val resolveInfoList = packageManager.queryIntentActivities(resolveIntent, PackageManager.MATCH_DEFAULT_ONLY)
Start email client
val intents = resolveInfoList.mapNotNull { info -> packageManager.getLaunchIntentForPackage(info.activityInfo.packageName) }.toMutableList()
if(intents.isEmpty()) {
//no mail client installed. Prompt user or throw exception
} else if(intents.size == 1) {
//one mail client installed, start that
startActivity(intents.first())
} else {
//multiple mail clients installed, let user choose which one to start
val chooser = Intent(Intent.ACTION_CHOOSER)
chooser.putExtra(Intent.EXTRA_INTENT, intents.removeAt(0))
chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, intents.toTypedArray())
chooser.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
startActivity(chooser)
}
How can I Open a Whatsapp chat with a specific contact by clicking on a button in my app?
That's the code I use. It opens WhatsApp and let me search the contact I want to send the message to, but it doesn't open the WhatsApp chat with the specific contact number I gave it to.
whatsappButton.setOnClickListener{
var con = itemView.context
val textToShare = "*כח אדם*"
val phoneNumber = blogPost.phone
val sendIntent = Intent()
sendIntent.action = Intent.ACTION_SEND
sendIntent.type = "text/plain"
sendIntent.putExtra("jid", phoneNumber+"#s.whatsapp.net")
sendIntent.putExtra(Intent.EXTRA_TEXT, textToShare)
val extra = sendIntent.extras
startActivity(con,sendIntent,extra)
}
If you want send a message to a specific contact from Contacts app, you should first require permission to access contacts, get the number and try with this:
Intent sendIntent = new Intent("android.intent.action.MAIN");
sendIntent.setComponent(new ComponentName("com.whatsapp","com.whatsapp.Conversation"));
sendIntent.putExtra("jid", PhoneNumberUtils.stripSeparators("YOUR_PHONE_NUMBER")+"#s.whatsapp.net");
startActivity(sendIntent);
Ref: https://stackoverflow.com/a/40285262/2895571
Please check this answer here
Use the following bit of code:
Intent i = new Intent(Intent.ACTION_SENDTO, Uri.parse("content://com.android.contacts/data/" + c.getString(0)));
i.setType("text/plain");
i.setPackage("com.whatsapp"); // so that only Whatsapp reacts and not the chooser
i.putExtra(Intent.EXTRA_SUBJECT, "Subject");
i.putExtra(Intent.EXTRA_TEXT, "I'm the body.");
startActivity(i);
First option: using Uri to convert whatsapp web url into button:
open_whatsapp.setOnClickListener {
val url = "https://api.whatsapp.com/send?phone=XXXXXXXXXX"
val openWhatsappIntent = Intent(Intent.ACTION_VIEW)
openWhatsappInten.data = Uri.parse(url)
startActivity(openWhatsappInten)
}
Second option; this is used as a href in web development :
Tel: XXX XXXX XX
maybe you can add Html format into TextView (Html.fromHtml()) and enable links clickeable to open the whatsapp application.
if you want send message to particular contact in whatsapp , use below url and particular mobile no to the Intent(including Country code).
String url = "https://wa.me/";
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(url+mobile_no));
startActivity(browserIntent);
The code below used to work a week ago. It's purpose is to allow the user to choose if he want to send a text message using WhatsApp or SMS, but now when I choose WhatsApp it doesn't do anything, although SMS keeps working.
Looking at the logcat it's printing: 2018-10-25 18:28:28.915 2147-6714/? I/ActivityManager: START u0 {act=android.intent.action.SENDTO dat=smsto:xxxxxxxxxxx flg=0x3000000 cmp=com.whatsapp/.Conversation (has extras)} from uid 10096 Even passing a valid number with country code, it is printing smsto:xxxxxxxxxxx.
Is there any working code for this purpose or know about this problem?
fun sendMessageToNumber(number: String, text: String) {
val cleanNumber = number.cleanText()
val uri = Uri.parse("smsto:$cleanNumber")
val sendIntent = Intent(Intent.ACTION_SENDTO, uri)
sendIntent.putExtra("sms_body", text)
context?.startActivity(Intent.createChooser(sendIntent, context.getString(R.string.fragment_account_chooser_message_title)))
}
In one of my project I have used following code for sending whatsapp message :
fun sendWhatsappMsg(){
var toNumber = "+91 xxxxx xxxxx" // contains spaces.
toNumber = toNumber.replace("+", "").replace(" ", "")
val sendIntent = Intent("android.intent.action.MAIN")
sendIntent.putExtra("jid", "$toNumber#s.whatsapp.net")
sendIntent.putExtra(Intent.EXTRA_TEXT, "Hello")
sendIntent.action = Intent.ACTION_SEND
sendIntent.setPackage("com.whatsapp")
sendIntent.type = "text/plain"
startActivity(sendIntent)
}
And to send text message I have used following code :
fun sendTextMsg(){
val phone = "xxxxxxxxxx"
val msg = "smsto:" + phone
val smsUri = Uri.parse(msg)
val smsIntent = Intent(Intent.ACTION_SENDTO, smsUri)
startActivity(smsIntent)
}
You can try this. Both are working for me.