Android how to share URL + text together on social media - android

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

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")
)
)
}
}

Share text with WhatsApp on Android "Can't send empty message"

When I'm trying to share text using intent mechanism and pick WhatsApp, it says:
Can't send empty message
I've read an official docs about Android integration here: https://faq.whatsapp.com/en/android/28000012
My code:
public void shareText(String label, CharSequence title, CharSequence body) {
final Intent intent = new Intent(Intent.ACTION_SEND);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_SUBJECT, title.toString());
intent.putExtra(Intent.EXTRA_TEXT, TextUtils.concat(title, body));
final Intent chooser = Intent.createChooser(intent, label);
chooser.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
if (chooser.resolveActivity(mContext.getPackageManager()) != null) {
mContext.startActivity(chooser);
}
}
Am I doing something wrong? Or is it bug with WhatsApp messenger?
P.S. arguments title and body are not empty in my case.
What you have done is,
intent.putExtra(Intent.EXTRA_TEXT, TextUtils.concat(title, body));
while TextUtils.concat(title, body) returns CharSequence probably that whatsapp does not support.
You have to pass the value as a String leaving you two solutions.
You can convert the whole to a String by toString()
intent.putExtra(Intent.EXTRA_TEXT, TextUtils.concat(title, body).toString());
Converting it a String before passing it to intent.
String someValue = TextUtils.concat(title, body).toString();
and adding it here as,
intent.putExtra(Intent.EXTRA_TEXT, someValue);
Here You can send data from your app to Whatsapp and any other like a messenger
Intent share = new Intent(Intent.ACTION_SEND);
share.setType("text/plain");
share.putExtra(Intent.EXTRA_TEXT, " Your text ");
startActivity(Intent.createChooser(share, " Your text "));
Here sendEmtpyMassages is Button Just copy this method It's Work for you
sendEmtpyMassages.setOnClickListener {
val context: Context = applicationContext
val sendIntent = Intent("android.intent.action.MAIN")
sendIntent.action = Intent.ACTION_VIEW
sendIntent.setPackage("com.whatsapp")
val url = "https://api.whatsapp.com/send?phone=" + "&text=" + " "
sendIntent.data = Uri.parse(url)
if (sendIntent.resolveActivity(context.packageManager) != null) {
startActivity(sendIntent)
}
}

How to share image and caption from whatsapp on my android application

So I'm sharing an image from whatsapp app to my android app. However, the caption which is attached to it is not part of the intent. Is there a way I can check if my caption is part of the intent?
I've already been able to achieve sharing of image. But the caption attached to it does not come with it. Also when I print the intent type, it shows as 'image/jpeg' and I believe that because of this, the caption is not getting shared in the intent in the first place.
ImageView SharedImage;
TextView SharedText;
Intent intent = getIntent();
String action = intent.getAction();
String type = intent.getType();
Log.d("Check Type", action + " " + type);
Uri receiveUri = intent.getParcelableExtra(Intent.EXTRA_STREAM);
if (receiveUri != null) {
SharedImage.setImageURI(null);
SharedImage.setImageURI(receiveUri);
}
You should be able to get the caption through
intent.getStringExtra(Intent.EXTRA_TEXT);

Send multiple text items in ShareIntent?

My question is very similar to these ones:
Is it possible to share multiple text by using share intent?
Passing Multiple text/plain values in share intent
I've tried the suggested solutions and it hasn't worked. I'm trying to send two items: "name" and "arrival time" in share intent. The data from which the two texts(Strings) are downloaded into the activity via AsyncTask and RecyclerView is in the method below.
//initializing
Schedule stationArrival;
private String stationShareStationName;
private String stationShareArrivalTime;
#Override
public void returnScheduleData(ArrayList<Schedule> simpleJsonScheduleData)
{
if (simpleJsonScheduleData.size() > 0) {
scheduleAdapter = new ScheduleAdapter(simpleJsonScheduleData, StationScheduleActivity.this);
scheduleArrayList = simpleJsonScheduleData;
mScheduleRecyclerView.setAdapter(scheduleAdapter);
scheduleAdapter.setScheduleList(scheduleArrayList);
stationArrival = scheduleArrayList.get(0);
stationShareStationName = stationArrival.getStationScheduleName();
stationShareArrivalTime = stationArrival.getExpectedArrival();
}
else
{
Toast.makeText(StationScheduleActivity.this, "Data currently unavailable", Toast.LENGTH_SHORT).show();
}
if (mShareActionProvider != null)
{
mShareActionProvider.setShareIntent(createShareIntent());
}
}
ShareIntent code. Currently it's wrong since only one of the items is displayed in the share screen. I've tried to put the two items into an ArrayList but it doesn't work since I have a separate method for the ShareIntent. Is there a way to do this w/o restructuring the current code? Thank you in advance.
public Intent createShareIntent()
{
Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.setType("text/plain");
shareIntent.putExtra(Intent.EXTRA_TEXT,stationShareStationName);
shareIntent.putExtra(Intent.EXTRA_TEXT,stationShareArrivalTime);
return shareIntent;
}
You can concat two stationShareStationName and stationShareArrivalTime and make single string. You can also use newline characters in between these variables ("\n") as below:
public Intent createShareIntent()
{
String stationShareStationName ="xxx";
String stationShareArrivalTime = "xxx";
String data =stationShareStationName + "\n" + stationShareArrivalTime
Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.setType("text/plain");
shareIntent.putExtra(Intent.EXTRA_TEXT,data);
return shareIntent;
}

How to send message to particular whatsapp contact programmatically using intent? [duplicate]

This question already has answers here:
Send text to specific contact programmatically (whatsapp)
(29 answers)
Closed 6 years ago.
I searched various answers but all of them are outdated.
Whenever I tried using send to it only opens contact chooser.
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");//phone number without "+" prefix
startActivity(sendIntent);
Update:
The aforementioned hack cannot be used to add any particular message, so here is the new approach. Pass the user mobile in international format here without any brackets, dashes or plus sign. Example: If the user is of India and his mobile number is 94xxxxxxxx , then international format will be 9194xxxxxxxx. Don't miss appending country code as a prefix in mobile number.
private fun sendMsg(mobile: String, msg: String){
try {
val packageManager = requireContext().packageManager
val i = Intent(Intent.ACTION_VIEW)
val url =
"https://wa.me/$mobile" + "?text=" + URLEncoder.encode(msg, "utf-8")
i.setPackage("com.whatsapp")
i.data = Uri.parse(url)
if (i.resolveActivity(packageManager) != null) {
requireContext().startActivity(i)
}
} catch (e: Exception) {
e.printStackTrace()
}
}
Note: This approach works only with contacts added in user's Whatsapp
account.
You have to set the package name in the Intent like this
intent.setPackage("com.whatsapp");
Full example:
Uri uri = Uri.parse("smsto:" + smsNumber);
Intent i = new Intent(Intent.ACTION_SENDTO, uri);
i.putExtra("sms_body", smsText);
i.setPackage("com.whatsapp");
startActivity(i);
You just fire the below intent on the click on button:
Uri mUri = Uri.parse("smsto:" + mobile1);
Intent mIntent = new Intent(Intent.ACTION_SENDTO, mUri);
mIntent.setPackage("com.whatsapp");
mIntent.putExtra("sms_body", "The text goes here");
mIntent.putExtra("chat", true);
startActivity(Intent.createChooser(mIntent, ""));
if number available on whatsapp then that particular user chat open and you send your message.If number not available on whatsapp the alert diaolge open in that case.
hope this help you ;-)
Here is solution
private void openWhatsApp(String number) {
String whatsAppMessage = "Hello!";
Uri uri = Uri.parse("smsto:" + number);
Intent i = new Intent(Intent.ACTION_SENDTO, uri);
i.setPackage("com.whatsapp");
startActivity(i);
}
Call above function and pass number, by which by want to open chat in Whatsapp messenger.
Hope it will work for you. :)

Categories

Resources