Share hyperlink via Android Intent - android

This is my hyperlink
String linkText = "Visit the <a href='http://stackoverflow.com'>StackOverflow</a> web page."
I have add this in below function
fun whatsappshr(context: Context, displayData: Display_data) {
val message : String = displayData.appdata + linkText
val Intentfun = Intent(Intent.ACTION_SEND)
Intentfun.type = "text/plain"
Intentfun.setPackage("com.whatsapp")
Intentfun.putExtra(Intent.EXTRA_TEXT, message)
try {
ContextCompat.startActivity(itemView.context, Intentfun, null)
} catch (ex: ActivityNotFoundException) {
Toast.makeText(itemView.context, "Whatsapp not Installed", Toast.LENGTH_SHORT).show()
}
}
when my app share the data via Intent, it does not send a link. It send the whole data from linkText.
Is there any way to send hyperlink via android intent? Guys, please help me to resolve this.

Related

how to handle share text on whatsapp if more than one whatsapp exist in android programatically?

I am sharing text on WhatsApp using this code. By doing this I can only share on WhatsApp messenger but what if users have more than one WhatsApp install like WA Messenger, WhatsApp business, etc?
I searched a lot but couldn't find a solution, if anyone knows put your answer here this will help a lot of people.
private fun shareLink(mobile: String, msg: String) {
try {
val url =
"https://wa.me/$mobile" + "?text=" + URLEncoder.encode(msg, "utf-8")
val shareIntent = Intent(Intent.ACTION_SEND)
shareIntent.type = "text/plain"
shareIntent.setPackage("com.whatsapp")
shareIntent.putExtra(Intent.EXTRA_TEXT, url)
startActivity(shareIntent)
}catch (e:PackageManager.NameNotFoundException){
e.printStackTrace()
startActivity(
Intent(
Intent.ACTION_VIEW,
Uri.parse("http://play.google.com/store/apps/details?id=com.whatsapp")
)
)
}
}

I want to send a message by email but the Intent doesn't work. Why?

There is a part of my app where I send an email using a button but for some reason the Intent doesn't work and I don't understand why.
binding.IvMail.setOnClickListener {
val email = Intent(Intent.ACTION_SEND)
.setType("text/plain")
.putExtra(Intent.EXTRA_SUBJECT, getString(R.string.email_subject))
.putExtra(Intent.EXTRA_TEXT, getString(R.string.email_text))
if (activity?.packageManager?.resolveActivity(email, 0) != null) {
startActivity(email)
}
}
I already searched for other ways to do it but everyone is using Intent.
// try this(JAVA).
Intent i = new Intent(Intent.ACTION_SEND);
i.setType("message/rfc822");
i.putExtra(Intent.EXTRA_EMAIL , new String[]{"recipient#example.com"});
i.putExtra(Intent.EXTRA_SUBJECT, "subject of email");
i.putExtra(Intent.EXTRA_TEXT , "body of email");
try {
startActivity(Intent.createChooser(i, "Send mail..."));
} catch (android.content.ActivityNotFoundException ex) {
Toast.makeText(MyActivity.this, "There are no email clients installed.", Toast.LENGTH_SHORT).show();
}
////For(Kotlin)
sendEmail(recipient, subject, message)
private fun sendEmail(recipient: String, subject: String, message:
String) {
/*ACTION_SEND action to launch an email client installed on your Android device.*/
val mIntent = Intent(Intent.ACTION_SEND)
/*To send an email you need to specify mailto: as URI using
setData() method
and data type will be to text/plain using setType() method*/
mIntent.data = Uri.parse("mailto:")
mIntent.type = "text/plain"
// put recipient email in intent
/* recipient is put as array because you may wanna send email to multiple emails
so enter comma(,) separated emails, it will be stored array*/
mIntent.putExtra(Intent.EXTRA_EMAIL, arrayOf(recipient))
//put the Subject in the intent
mIntent.putExtra(Intent.EXTRA_SUBJECT, subject)
//put the message in the intent
mIntent.putExtra(Intent.EXTRA_TEXT, message)
try {
//start email intent
startActivity(Intent.createChooser(mIntent, "Choose Email Client..."))
}
catch (e: Exception){
//if any thing goes wrong for example no email client application or any exception
//get and show exception message
Toast.makeText(this, e.message, Toast.LENGTH_LONG).show()
}
}
}
You have to use ACTION_SENDTO instead of ACTION_SEND.
See your new code below:
binding.IvMail.setOnClickListener {
val email = Intent(Intent.ACTION_SENDTO) // here you have to use SENDTO
.setType("text/plain")
.putExtra(Intent.EXTRA_SUBJECT, getString(R.string.email_subject))
.putExtra(Intent.EXTRA_TEXT, getString(R.string.email_text))
if (activity?.packageManager?.resolveActivity(email, 0) != null) {
startActivity(email)
}
}
Let me know if not solved yet
Your code is fine. If it is not running, it would seem that your if statement is not satisfied and is skipping Intent resolvement.
What you can do to determine the problem is:
Debug if the code blocks are reached, by setting breakpoints and running debug app like this:
Log if the code blocks above are reached, by setting logs and looking for them in logcat tool like this:
You could also look in the Logcat for errors that happened when the intent was being ran.

Android Email Intent doesn't populate recipients

I am having an issue with Email Intent in Android.
The recipients field is not populating properly.
My code is as such:
Extensions.kt
// Returns a Mail Intent
fun requireMailIntent(subject: String, body: String) = Intent(Intent.ACTION_SENDTO).apply {
data = Uri.parse("mailto:")
putExtra(Intent.EXTRA_EMAIL, arrayOf("email#gmail.com"))
putExtra(Intent.EXTRA_SUBJECT, subject)
putExtra(Intent.EXTRA_TEXT,body)
}
/**
* Checks whether the intent has an app that can
* handle it.
* Should be called before starting an intent
**/
fun Intent.hasSuccessor(context: Context) = resolveActivity(context.packageManager) != null
Fragment.kt
// Submit button
binding.ButtonSubmit.setOnClickListener {
val emailConstruct = constructEmail()
val intent = requireMailIntent(emailConstruct.first, emailConstruct.second)
if(intent.hasSuccessor(requireContext())){
Log.v("INTENT_TEST", "Launching Intent")
startActivity(intent)
}else{
Log.v("INTENT_TEST", "No app found")
}
}
private fun constructEmail(): Pair<String,String>{
val subject = "MES :: Bug Report :: ${viewModel.bugIdentified}"
val message = "Below are the steps \n ${viewModel.bugSteps}"
return Pair(subject, message)
}
Manifest.xml
<!-- For basic package querying, ie browsers, email... -->
<uses-permission
android:name="android.permission.QUERY_ALL_PACKAGES"
tools:ignore="QueryAllPackagesPermission" />
Gradle
// SDK Versions
sdk_compiled_version = 30
sdk_minimum_version = 24
// Build tools
build_tools = "29.0.3"
Upon clicking the button, Android shows me the app chooser, but when i click Gmail, everything populates except the recipients field.
Can someone please help ?
Try using URI
val uriText = "mailto:contact#example.com" +
"?subject=" + "your subject here" +
"&body=" + body
val uri = Uri.parse(uriText)
val sendIntent = Intent(Intent.ACTION_SENDTO)
sendIntent.data = uri
startActivity(Intent.createChooser(sendIntent, "Send Email").addFlags(FLAG_ACTIVITY_NEW_TASK))

Android how to share URL + text together on social media

I try to share url, pointing to some video in internet, and some text. Url and text need to be shared together, on one click to "share" button. I already know how to share video + text, when video is downloaded to Android device. But I want to share just url to video + text. So, url is actually text by itself, and I can't find way to share 2 separate texts. When I try following:
putExtra( Intent.EXTRA_TEXT, "url")
putExtra( Intent.EXTRA_TEXT, "text2")
only text2 is shared.
Here is my code:
val sendIntent: Intent = Intent().apply {
action = Intent.ACTION_SEND
type = "*/*"
putExtra(Intent.EXTRA_TEXT, "url")
putExtra(Intent.EXTRA_TEXT, "text2")
}
val shareIntent = Intent.createChooser(sendIntent, null)
startActivity(shareIntent)
Why is this happening?
You are overwriting the text with "text2", this is the reason why only that part is shared, see the corresponding method inside the Intent class:
public #NonNull Intent putExtra(String name, String value) {
if (mExtras == null) {
mExtras = new Bundle();
}
mExtras.putString(name, value);
return this;
}
How to fix this?
Just combine the URL and the text, e.g.:
putExtra(Intent.EXTRA_TEXT, "url" + "\n\n" + "your text");

Opening email client via Intent (but not to send a message)

Is there a way to programically open email client, without a need to forcing message send? I just want the app to let user open his email client for email checking purposes :)
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("message/rfc822");
startActivity(Intent.createChooser(intent, ""));
This code works but it forces user to send a new message.
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_APP_EMAIL);
startActivity(intent);
startActivity(Intent.createChooser(intent, getString(R.string.ChoseEmailClient)));
That kinda worked. But it opend Gmail for me, even since I have other email clients
This code will show a dialog with a list of email clients. Clicking one will launch the application:
try {
List<String> emailClientNames = new ArrayList<String>();
final List<String> emailClientPackageNames = new ArrayList<String>();
// finding list of email clients that support send email
Intent intent = new Intent(Intent.ACTION_SENDTO, Uri.fromParts(
"mailto", "abc#gmail.com", null));
PackageManager pkgManager = AppController.getContext().getPackageManager();
List<ResolveInfo> packages = pkgManager.queryIntentActivities(intent, 0);
if (!packages.isEmpty()) {
for (ResolveInfo resolveInfo : packages) {
// finding the package name
String packageName = resolveInfo.activityInfo.packageName;
emailClientNames.add(resolveInfo.loadLabel(getPackageManager()).toString());
emailClientPackageNames.add(packageName);
}
// a selection dialog for the email clients
AlertDialog.Builder builder = new AlertDialog.Builder(MyActivity.this);
builder.setTitle("Select email client");
builder.setItems(emailClientNames.toArray(new String[]{}), new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// on click we launch the right package
Intent intent = getPackageManager().getLaunchIntentForPackage(emailClientPackageNames.get(which));
startActivity(intent);
}
});
AlertDialog dialog = builder.create();
dialog.show();
}
} catch (ActivityNotFoundException e) {
// Show error message
}
In Kotlin, but this version creates a chooser for user to pick which email app to use. You basically do what Oved does in his answer, except create an actual chooser using LabeledIntent.
fun emailAppIntent(): Intent? {
val emailIntent = Intent(Intent.ACTION_VIEW, Uri.parse("mailto:"))
val packageManager = appLaunchContext.packageManager
val activitiesHandlingEmails = packageManager.queryIntentActivities(emailIntent, 0)
if (activitiesHandlingEmails.isNotEmpty()) {
// use the first email package to create the chooserIntent
val firstEmailPackageName = activitiesHandlingEmails.first().activityInfo.packageName
val firstEmailInboxIntent = packageManager.getLaunchIntentForPackage(firstEmailPackageName)
val emailAppChooserIntent = Intent.createChooser(firstEmailInboxIntent, "")
// created UI for other email packages and add them to the chooser
val emailInboxIntents = mutableListOf<LabeledIntent>()
for (i in 1 until activitiesHandlingEmails.size) {
val activityHandlingEmail = activitiesHandlingEmails[i]
val packageName = activityHandlingEmail.activityInfo.packageName
val intent = packageManager.getLaunchIntentForPackage(packageName)
emailInboxIntents.add(
LabeledIntent(
intent,
packageName,
activityHandlingEmail.loadLabel(packageManager),
activityHandlingEmail.icon
)
)
}
val extraEmailInboxIntents = emailInboxIntents.toTypedArray()
return emailAppChooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, extraEmailInboxIntents)
} else {
return null
}
}
.. then later call it with
val emailChooserIntent = emailAppIntent()
if (emailChooserIntent != null) {
startActivity(emailAppIntent())
}
or handle null however you want.
I think you should replace Intent.ACTION_SEND to Intent.ACTION_VIEW, i am sure this will work as this will prompt with list of application which support MIME type "message/rfc822" so it will include your default email client in your device other than gmail app.
How about this code:
final Intent emailLauncher = new Intent(Intent.ACTION_VIEW);
emailLauncher.setType("message/rfc822");
try{
startActivity(emailLauncher);
}catch(ActivityNotFoundException e){
}
A bit more modern solution in Kotlin
fun tryVerifyMail() {
try {
val intents: List<Intent> = (packageManager.queryIntentActivities(Intent(
Intent.ACTION_SENDTO, Uri.fromParts(
"mailto", "lowhillgamesoy#gmail.com", null
)
), 0) + packageManager.queryIntentActivities(Intent(Intent.ACTION_VIEW).also {
it.type = "message/rfc822"
}, 0)).mapNotNull {
it.activityInfo.packageName
}.toSet().mapNotNull {
packageManager.getLaunchIntentForPackage(it)
}
if(intents.size > 0) {
startActivityForResult(Intent.createChooser(intents.first(), getString(R.string.verify_mail)).also {
if(intents.size > 1) {
it.putExtra(Intent.EXTRA_INITIAL_INTENTS, intents.subList(1, intents.size - 1).toTypedArray())
}
}, OPEN_MAIL_REQUEST_CODE)
} else {
Toast.makeText(this, "Verify your e-mail by clicking the link in your e-mail inbox.", Toast.LENGTH_LONG).show()
}
} catch (e: ActivityNotFoundException) {
// Show error message
e.printStackTrace()
}
}
This also picks both mail clients only supporting ACTION_SEND and ACTION_VIEW and removes duplicates.

Categories

Resources