No apps can perform this action Android Intent using Kotlin - android

I am using the following code snippets from Android official documentation to share content through applications using Intent but it says "No apps can perform this action." on a physical device. I have messengers, email client and text message clients installed.
val intent = Intent().apply {
intent.action = Intent.ACTION_SEND
intent.type = "text/plain"
intent.putExtra(Intent.EXTRA_TEXT, "Text to share")
}
startActivity(Intent.createChooser(intent, "Sharing"))

I think you should change your intent initialization with apply to this:
val intent = Intent().apply {
action = Intent.ACTION_SEND
type = "text/plain"
putExtra(Intent.EXTRA_TEXT, "Text to share")
}
When you modify the intent variable inside the apply you are modifying the activity intent not the brand new intent.
copy this code and you will see what i'm talking about:
val intent_1 = Intent().apply {
intent.action = Intent.ACTION_SEND
intent.type = "text/plain"
intent.putExtra(Intent.EXTRA_TEXT, "Text to share")
}

This is what I know:
As Fredy Mederos said, the value that you are modified is the Activity.getIntent, not the new Intent.
You should write like this:
val intent = Intent().apply {
action = Intent.ACTION_SEND
type = "text/plain"
putExtra(Intent.EXTRA_TEXT, "Text to share")
}
or more precise:
val intent = Intent().apply {
this.action = Intent.ACTION_SEND
this.type = "text/plain"
this.putExtra(Intent.EXTRA_TEXT, "Text to share")
}
the this is pointed to your initialized new Intent().

The following code works instead of above posted in the question.
val i = Intent(Intent.ACTION_SEND)
i.type = "text/plain"
i.putExtra(Intent.EXTRA_TEXT, "Content to share")
startActivity(Intent.createChooser(i, "Sharing"))
I am not sure why the code in the question does not work but my guess is that intent is related to the activity's intent and it works when I instantiated another object from Intent class.

you can replace startActivity(Intent.createChooser(i, "Sharing"))
by startActivity(i)

Related

Launching Sharesheet without Intent.createChooser

Is there a way to launch the Sharesheet (see https://developer.android.com/training/sharing/send) without having to wrap the called intent into the createChooser-returned intent, but rather specifying the wish for the Sharesheet as an ACTION and/or EXTRA. E.g. instead of
val sendIntent: Intent = Intent().apply {
action = Intent.ACTION_SEND
putExtra(Intent.EXTRA_TEXT, "This is my text to send.")
type = "text/plain"
}
val shareIntent = Intent.createChooser(sendIntent, null)
startActivity(shareIntent)
Something like:
val shareIntent: Intent = Intent().apply {
action = Intent.ACTION_CREATE_CHOOSER
putExtra(Intent.EXTRA_ACTION_TYPE, Intent.ACTION_SEND)
putExtra(Intent.EXTRA_TEXT, "This is my text to send.")
type = "text/plain"
}
startActivity(shareIntent)
My main goal is to get a Sharesheet that shows my frequent contacts/apps at the top. I'm not interested in all the functionality that createChooser might provide.
Should work at least in Android 11.

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 open the default mail inbox from android code?

I'm trying to link a button to the mail app. Not to send mail, but just to open the inbox.
Should I do this with Intent intent = new Intent(...)?
If so, what should be between the ( )?
If the goal is to open the default email app to view the inbox, then key is to add an intent category and use the ACTION_MAIN intent like so:
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_APP_EMAIL);
getActivity().startActivity(intent);
https://developer.android.com/reference/android/content/Intent.html#CATEGORY_APP_EMAIL
This code worked for me. It opens a picker with all email apps registered to device and straight to Inbox:
Intent emailIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("mailto:"));
PackageManager pm = getPackageManager();
List<ResolveInfo> resInfo = pm.queryIntentActivities(emailIntent, 0);
if (resInfo.size() > 0) {
ResolveInfo ri = resInfo.get(0);
// First create an intent with only the package name of the first registered email app
// and build a picked based on it
Intent intentChooser = pm.getLaunchIntentForPackage(ri.activityInfo.packageName);
Intent openInChooser =
Intent.createChooser(intentChooser,
getString(R.string.user_reg_email_client_chooser_title));
// Then create a list of LabeledIntent for the rest of the registered email apps
List<LabeledIntent> intentList = new ArrayList<LabeledIntent>();
for (int i = 1; i < resInfo.size(); i++) {
// Extract the label and repackage it in a LabeledIntent
ri = resInfo.get(i);
String packageName = ri.activityInfo.packageName;
Intent intent = pm.getLaunchIntentForPackage(packageName);
intentList.add(new LabeledIntent(intent, packageName, ri.loadLabel(pm), ri.icon));
}
LabeledIntent[] extraIntents = intentList.toArray(new LabeledIntent[intentList.size()]);
// Add the rest of the email apps to the picker selection
openInChooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, extraIntents);
startActivity(openInChooser);
}
Any suggestions to avoid the crash if the default mail in the device is not configured?
Yes, it's possible to open the Android default email inbox.
Use this code:
Intent intent = getPackageManager().getLaunchIntentForPackage("com.android.email");
startActivity(intent);
This code works, you have to configure your Android device default mail first. If you already configured your mail it works fine. Otherwise, it force closes with a NullPointerException.
To open it new task use the below code:
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_APP_EMAIL);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
Based on the answer https://stackoverflow.com/a/28190156/3289338
Starting from Android 11 the system won't return anything for queryIntentActivities because we first need to add an entry in the queries (in the manifest) like this
<manifest ...>
<queries>
...
<intent>
<action
android:name="android.intent.action.VIEW" />
<data
android:scheme="mailto" />
</intent>
</queries>
...
</manifest>
and here a kotlin version of the solution:
fun Context.openMailbox(chooserTitle: String) {
val emailIntent = Intent(Intent.ACTION_VIEW, Uri.parse("mailto:"))
val resInfo = packageManager.queryIntentActivities(emailIntent, 0)
if (resInfo.isNotEmpty()) {
// First create an intent with only the package name of the first registered email app
// and build a picked based on it
val intentChooser = packageManager.getLaunchIntentForPackage(
resInfo.first().activityInfo.packageName
)
val openInChooser = Intent.createChooser(intentChooser, chooserTitle)
// Then create a list of LabeledIntent for the rest of the registered email apps
val emailApps = resInfo.toLabeledIntentArray(packageManager)
// Add the rest of the email apps to the picker selection
openInChooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, emailApps)
startActivity(openInChooser)
} else {
Timber.e("No email app found")
}
}
private fun List<ResolveInfo>.toLabeledIntentArray(packageManager: PackageManager): Array<LabeledIntent> = map {
val packageName = it.activityInfo.packageName
val intent = packageManager.getLaunchIntentForPackage(packageName)
LabeledIntent(intent, packageName, it.loadLabel(packageManager), it.icon)
}.toTypedArray()
You can simply use below code when for no attachment:
Intent i = new Intent(Intent.ACTION_SENDTO);
i.setData(Uri.parse("mailto:support#mailname.com"));
i.putExtra(Intent.EXTRA_SUBJECT, "Feedback/Support");
startActivity(Intent.createChooser(emailIntent, "Send feedback"));
For details I recommend to visit:
https://developer.android.com/guide/components/intents-common.html#Email
I'm with jetpack compose and this works for me :
val context = LocalContext.current
val intent = Intent(Intent.ACTION_MAIN)
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
intent.addCategory(Intent.CATEGORY_APP_EMAIL)
Button(
onClick = { startActivity(context, intent, null) }
)
You can use this but it is for gmail only
Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
emailIntent.setType("plain/text");
startActivity(emailIntent);
For kotlin:
fun composeEmail(addresses: Array<String>, subject: String) {
val intent = Intent(Intent.ACTION_SENDTO).apply {
data = Uri.parse("mailto:") // only email apps should handle this
putExtra(Intent.EXTRA_EMAIL, addresses)
putExtra(Intent.EXTRA_SUBJECT, subject)
}
if (intent.resolveActivity(packageManager) != null) {
startActivity(intent)
}
}
Ref: https://developer.android.com/reference/android/content/Intent.html#CATEGORY_APP_EMAIL
val intent = Intent(Intent.ACTION_MAIN)
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
intent.addCategory(Intent.CATEGORY_APP_EMAIL)
startActivity(intent)
The code works for me:
Intent intent= new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_APP_EMAIL);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(Intent.createChooser(intent, ""));
Unfortunately it doesn't look promising. This has been asked before
How do I launch the email client directly to inbox view?
you can open the email client in compose mode, but you seem to already know that.
Bit late, here is proper working code.
Intent intent = Intent.makeMainSelectorActivity(
Intent.ACTION_MAIN,
Intent.CATEGORY_APP_EMAIL
);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(Intent.createChooser(intent, "Email"));
For further details check this document:
CATEGORY_APP_EMAIL
makeMainSelectorActivity
Intent email = new Intent(Intent.ACTION_MAIN);
email.addCategory(Intent.CATEGORY_APP_EMAIL);
startActivity(email);
You can open Android default e-mail client using this:
Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
emailIntent.setType("text/plain");
emailIntent.setClassName("com.android.email", "com.android.email.activity.Welcome");
emailIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(emailIntent);

Share application "link" in Android

I want my application user to be able to share/recommend my app to other users. I use the ACTION_SEND intent. I add plain text saying something along the lines of: install this cool application. But I can't find a way to enable users to directly go to the install screen of market place for instance. All I can provide them with is a web link or some text.
In other words I am looking for a very direct way for android users to have my app installed.
Thanks for any help/pointers,
Thomas
This will let you choose from email, whatsapp or whatever.
try {
Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.setType("text/plain");
shareIntent.putExtra(Intent.EXTRA_SUBJECT, "My application name");
String shareMessage= "\nLet me recommend you this application\n\n";
shareMessage = shareMessage + "https://play.google.com/store/apps/details?id=" + BuildConfig.APPLICATION_ID +"\n\n";
shareIntent.putExtra(Intent.EXTRA_TEXT, shareMessage);
startActivity(Intent.createChooser(shareIntent, "choose one"));
} catch(Exception e) {
//e.toString();
}
You can use also ShareCompat class from support library.
ShareCompat.IntentBuilder.from(activity)
.setType("text/plain")
.setChooserTitle("Chooser title")
.setText("http://play.google.com/store/apps/details?id=" + activity.getPackageName())
.startChooser();
https://developer.android.com/reference/android/support/v4/app/ShareCompat.html
Thomas,
You would want to provide your users with a market:// link which will bring them directly to the details page of your app. The following is from developer.android.com:
Loading an application's Details page
In Android Market, every application
has a Details page that provides an
overview of the application for users.
For example, the page includes a short
description of the app and screen
shots of it in use, if supplied by the
developer, as well as feedback from
users and information about the
developer. The Details page also
includes an "Install" button that lets
the user trigger the download/purchase
of the application.
If you want to refer the user to a
specific application, your
application can take the user directly
to the application's Details page. To
do so, your application sends an
ACTION_VIEW Intent that includes a URI
and query parameter in this format:
market://details?id=
In this case, the packagename
parameter is target application's
fully qualified package name, as
declared in the package attribute of
the manifest element in the
application's manifest file. For
example:
market://details?id=com.example.android.jetboy
Source: http://developer.android.com/guide/publishing/publishing.html
Call this method:
public static void shareApp(Context context)
{
final String appPackageName = context.getPackageName();
Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_TEXT, "Check out the App at: https://play.google.com/store/apps/details?id=" + appPackageName);
sendIntent.setType("text/plain");
context.startActivity(sendIntent);
}
To be more exact
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse("https://play.google.com/store/apps/details?id=com.android.example"));
startActivity(intent);
or if you want to share your other apps from your dev. account you can do something like this
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse("https://play.google.com/store/apps/developer?id=Your_Publisher_Name"));
startActivity(intent);
To automatically fill in the application name and application id you could use this:
int applicationNameId = context.getApplicationInfo().labelRes;
final String appPackageName = context.getPackageName();
Intent i = new Intent(Intent.ACTION_SEND);
i.setType("text/plain");
i.putExtra(Intent.EXTRA_SUBJECT, activity.getString(applicationNameId));
String text = "Install this cool application: ";
String link = "https://play.google.com/store/apps/details?id=" + appPackageName;
i.putExtra(Intent.EXTRA_TEXT, text + " " + link);
startActivity(Intent.createChooser(i, "Share link:"));
Share application with title is you app_name, content is your application link
fun shareApp(context: Context) {
val appPackageName = BuildConfig.APPLICATION_ID
val appName = context.getString(R.string.app_name)
val shareBodyText = "https://play.google.com/store/apps/details?id=$appPackageName"
val sendIntent = Intent(Intent.ACTION_SEND).apply {
type = "text/plain"
putExtra(Intent.EXTRA_TITLE, appName)
putExtra(Intent.EXTRA_TEXT, shareBodyText)
}
context.startActivity(Intent.createChooser(sendIntent, null))
}
I know this question has been answered, but I would like to share an alternate solution:
Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.setType("text/plain");
String shareSubText = "WhatsApp - The Great Chat App";
String shareBodyText = "https://play.google.com/store/apps/details?id=com.whatsapp&hl=en";
shareIntent.putExtra(Intent.EXTRA_SUBJECT, shareSubText);
shareIntent.putExtra(Intent.EXTRA_TEXT, shareBodyText);
startActivity(Intent.createChooser(shareIntent, "Share With"));
finally, this code is worked for me to open the email client from android device.
try this snippet.
Intent testIntent = new Intent(Intent.ACTION_VIEW);
Uri data = Uri.parse("mailto:?subject=" + "Feedback" + "&body=" + "Write Feedback here....." + "&to=" + "someone#example.com");
testIntent.setData(data);
startActivity(testIntent);
Kotlin extension for share action. You can share whatever you want e.g. link
fun Context.share(text: String) =
this.startActivity(Intent().apply {
action = Intent.ACTION_SEND
putExtra(Intent.EXTRA_TEXT, text)
type = "text/plain"
})
Usage
context.share("Check https://stackoverflow.com")
According to official docs in 2021 preferred way is
fun shareTextToOtherApps(message: String) {
val sendIntent: Intent = Intent().apply {
action = Intent.ACTION_SEND
putExtra(Intent.EXTRA_TEXT, message)
type = "text/plain"
}
val shareIntent = Intent.createChooser(sendIntent, null)
startActivity(shareIntent)
}
Actually the best way to sheared you app between users , google (firebase) proved new technology Firebase Dynamic Link Through several lines you can make it
this is documentation
https://firebase.google.com/docs/dynamic-links/
and the code is
Uri dynamicLinkUri = dynamicLink.getUri();
Task<ShortDynamicLink> shortLinkTask = FirebaseDynamicLinks.getInstance().createDynamicLink()
.setLink(Uri.parse("https://www.google.jo/"))
.setDynamicLinkDomain("rw4r7.app.goo.gl")
.buildShortDynamicLink()
.addOnCompleteListener(this, new OnCompleteListener<ShortDynamicLink>() {
#Override
public void onComplete(#NonNull Task<ShortDynamicLink> task) {
if (task.isSuccessful()) {
// Short link created
Uri shortLink = task.getResult().getShortLink();
Uri flowchartLink = task.getResult().getPreviewLink();
Intent intent = new Intent();
intent.setAction(Intent.ACTION_SEND);
intent.putExtra(Intent.EXTRA_TEXT, shortLink.toString());
intent.setType("text/plain");
startActivity(intent);
} else {
// Error
// ...
}
}
});
try {
Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.setType("text/plain");
shareIntent.putExtra(Intent.EXTRA_SUBJECT, "Your Application name");
String shareMessage= "\n Your Message \n\n";
shareMessage = shareMessage + "https://play.google.com/store/apps/details?id=" + BuildConfig.APPLICATION_ID +"\n\n";
shareIntent.putExtra(Intent.EXTRA_TEXT, shareMessage);
startActivity(Intent.createChooser(shareIntent, "choose one"));
} catch(Exception e) {
//e.toString();
}
#Linh answer is almost good but causing a crash because of the missing FLAG_ACTIVITY_NEW_TASK, here is what worked for me
public static void shareApp(Context context) {
final String appPackageName = context.getPackageName();
Intent sendIntent = new Intent();
sendIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_TEXT, "Check out the App at: https://play.google.com/store/apps/details?id=" + appPackageName);
sendIntent.setType("text/plain");
context.startActivity(sendIntent);
}

launch sms application with an intent

I have a question about an intent...
I try to launch the sms app...
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.setType("vnd.android-dir/mms-sms");
int flags = Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP |
Intent.FLAG_ACTIVITY_CLEAR_TOP;
intent.setFlags(flags);
intent.setData(Uri.parse("content://sms/inbox"));
context.startActivity(intent);
so, you can see that I put too much things in my intent, but that's because I don't know how I can do...
Thank's
To start launch the sms activity all you need is this:
Intent sendIntent = new Intent(Intent.ACTION_VIEW);
sendIntent.setData(Uri.parse("sms:"));
You can add extras to populate your own message and such like this
sendIntent.putExtra("sms_body", x);
then just startActivity with the intent.
startActivity(sendIntent);
Intent smsIntent = new Intent(Intent.ACTION_VIEW);
smsIntent.setType("vnd.android-dir/mms-sms");
smsIntent.putExtra("address", "12125551212");
smsIntent.putExtra("sms_body","Body of Message");
startActivity(smsIntent);
If android version is Kitkat or above, users can change default sms application. This method will get default sms app and start default sms app.
private void sendSMS() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) // At least KitKat
{
String defaultSmsPackageName = Telephony.Sms.getDefaultSmsPackage(this); // Need to change the build to API 19
Intent sendIntent = new Intent(Intent.ACTION_SEND);
sendIntent.setType("text/plain");
sendIntent.putExtra(Intent.EXTRA_TEXT, "text");
if (defaultSmsPackageName != null)// Can be null in case that there is no default, then the user would be able to choose
// any app that support this intent.
{
sendIntent.setPackage(defaultSmsPackageName);
}
startActivity(sendIntent);
}
else // For early versions, do what worked for you before.
{
Intent smsIntent = new Intent(android.content.Intent.ACTION_VIEW);
smsIntent.setType("vnd.android-dir/mms-sms");
smsIntent.putExtra("address","phoneNumber");
smsIntent.putExtra("sms_body","message");
startActivity(smsIntent);
}
}
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_DEFAULT);
intent.setType("vnd.android-dir/mms-sms");
startActivity(intent);
That's all you need.
If you want to launch SMS Composing activity from some of your other activity and you also have to pass a phone number and SMS text, then use this code:
Uri sms_uri = Uri.parse("smsto:+92xxxxxxxx");
Intent sms_intent = new Intent(Intent.ACTION_SENDTO, sms_uri);
sms_intent.putExtra("sms_body", "Good Morning ! how r U ?");
startActivity(sms_intent);
Note: here the sms_body and smsto: is keys for recognizing the text and phone no at SMS compose activity, so be careful here.
Here is the code that will open the SMS activity pre-populated with the phone number to
which the SMS has to be sent. This works fine on emulator as well as the device.
Intent smsIntent = new Intent(Intent.ACTION_SENDTO);
smsIntent.addCategory(Intent.CATEGORY_DEFAULT);
smsIntent.setType("vnd.android-dir/mms-sms");
smsIntent.setData(Uri.parse("sms:" + phoneNumber));
startActivity(smsIntent);
In kotlin this can be implemented easily as follows:
/**
* If android version is Kitkat or above, users can change default sms application.
* This method will get default sms app and start default sms app.
*/
private fun openSMS() {
val message = "message here"
val phone = "255754......." //255 Tanzania code.
val uri = Uri.parse("smsto:+$phone")
val intent = Intent(Intent.ACTION_SENDTO, uri)
with(intent) {
putExtra("address", "+$phone")
putExtra("sms_body", message)
}
when {
Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT -> {
//Getting the default sms app.
val defaultSmsPackageName = Telephony.Sms.getDefaultSmsPackage(context)
// Can be null in case that there is no default, then the user would be able to choose
// any app that support this intent.
if (defaultSmsPackageName != null) intent.setPackage(defaultSmsPackageName)
startActivity(intent)
}
else -> startActivity(intent)
}
}
This is modified answer of #mustafasevgi
Use
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
intent.setClassName("com.android.mms", "com.android.mms.ui.ConversationList");
I use:
Intent sendIntent = new Intent(Intent.ACTION_MAIN);
sendIntent.putExtra("sms_body", "text");
sendIntent.setType("vnd.android-dir/mms-sms");
startActivity(sendIntent);
Intent eventIntentMessage =getPackageManager()
.getLaunchIntentForPackage(Telephony.Sms.getDefaultSmsPackage(getApplicationContext));
startActivity(eventIntentMessage);
try {
Intent smsIntent = new Intent(Intent.ACTION_VIEW);
smsIntent.setData(Uri.parse("smsto:" + Uri.encode(number)));
smsIntent.putExtra("address", number);
smsIntent.putExtra("sms_body", message);
PackageManager pm = activity.getPackageManager();
List<ResolveInfo> resInfo = pm.queryIntentActivities(smsIntent, 0);
for (int i = 0; i < resInfo.size(); i++) {
ResolveInfo ri = resInfo.get(i);
String packageName = ri.activityInfo.packageName;
if (packageName.contains("sms")) {
//Log.d("TAG", packageName + " : " + ri.activityInfo.name);
smsIntent.setComponent(new ComponentName(packageName, ri.activityInfo.name));
}
}
activity.startActivity(smsIntent);
} catch (Exception e) {
// Handle Error
}
Best way of doing this.
You can open default sms App and pass details as below :
Note : If u want to send to many numbers, separate each number with ";" inside string
String mblNumVar = "9876543210;9123456789";
Intent smsMsgAppVar = new Intent(Intent.ACTION_VIEW);
smsMsgAppVar.setData(Uri.parse("sms:" + mblNumVar));
smsMsgAppVar.putExtra("sms_body", "Hello Msg Tst Txt");
startActivity(smsMsgAppVar);
|Or| Use this function :
void openSmsMsgAppFnc(String mblNumVar, String smsMsgVar)
{
Intent smsMsgAppVar = new Intent(Intent.ACTION_VIEW);
smsMsgAppVar.setData(Uri.parse("sms:" + mblNumVar));
smsMsgAppVar.putExtra("sms_body", smsMsgVar);
startActivity(smsMsgAppVar);
}
Intent sendIntent = new Intent(Intent.ACTION_SEND);
//CHANGE YOUR MESSAGING ACTIVITY HERE IF REQUIRED
sendIntent.setClassName("com.android.mms", "com.android.mms.ui.ComposeMessageActivity");
sendIntent.putExtra("sms_body",msgbody);
sendIntent.putExtra("address",phonenumber);
//FOR MMS
sendIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file:///sdcard/mms.png"));
sendIntent.setType("image/png");
startActivity(sendIntent);
The best code that works with Default SMS app is.
Uri SMS_URI = Uri.parse("smsto:+92324502****"); //Replace the phone number
Intent sms = new Intent(Intent.ACTION_VIEW,SMS_URI);
sms.putExtra("sms_body","This is test message"); //Replace the message witha a vairable
startActivity(sms);
Compose SMS :
Uri smsUri = Uri.parse("tel:" + to);
Intent intent = new Intent(Intent.ACTION_VIEW, smsUri);
intent.putExtra("address", to);
intent.putExtra("sms_body", message);
intent.setType("vnd.android-dir/mms-sms");
startActivity(intent);
The below code works on android 6.0.
It will open the search activity in the default messaging application with the conversations related to specific string provided.
Intent smsIntent = new Intent(Intent.ACTION_MAIN);
smsIntent.addCategory(Intent.CATEGORY_LAUNCHER);
smsIntent.setClassName("com.android.mms", "com.android.mms.ui.SearchActivity");
smsIntent.putExtra("intent_extra_data_key", "string_to_search_for");
startActivity(smsIntent);
You can start the search activity with an intent. This will open the search activity of the default messaging application.
Now, to show a list of specific conversations in the search activity, you can provide the search string as string extra with the key as
"intent_extra_data_key"
as is shown in the onCreate of this class
String searchStringParameter = getIntent().getStringExtra(SearchManager.QUERY);
if (searchStringParameter == null) {
searchStringParameter = getIntent().getStringExtra("intent_extra_data_key" /*SearchManager.SUGGEST_COLUMN_INTENT_EXTRA_DATA*/);
}
final String searchString = searchStringParameter != null ? searchStringParameter.trim() : searchStringParameter;
You can also pass the SENDER_ADDRESS of the sms as string extra, which will list out all the conversations with that specific sender address.
Check com.android.mms.ui.SearchActivity for more information
You can also check this answer
For Kotlin simply do that:
val messageIntent = Intent(Intent.ACTION_SENDTO, Uri.parse("smsto:03xxxxxxxxx"))
messageIntent.putExtra("sms_body", "Enter your body here")
startActivity(messageIntent)
on emulator this work for me
Intent i = new Intent(Intent.ACTION_VIEW, Uri.fromParts("sms", number, null));
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
i.putExtra("sms_body", remindingReason);
startActivity(i);
Sms Intent :
Intent intent = new Intent("android.intent.action.VIEW");
/** creates an sms uri */
Uri data = Uri.parse("sms:");
intent.setData(data);
Either you can use this ,both are working fine on android above 4.4
Intent smsIntent = new Intent(Intent.ACTION_VIEW);
smsIntent.setType("vnd.android-dir/mms-sms");
smsIntent.putExtra("address","9212107320");
smsIntent.putExtra("sms_body","Body of Message");
startActivity(smsIntent);
or
Intent smsIntent = new Intent(Intent.ACTION_VIEW);
smsIntent.putExtra("sms_body","Body of Message");
smsIntent.setData(Uri.parse("sms:9212107320"));
startActivity(smsIntent);
You can use the following code snippet to achieve your goal:
Intent smsIntent = new Intent(Intent.ACTION_SENDTO);
smsIntent.setData(Uri.parse("smsto:"+model.getPhoneNo().trim()));
smsIntent.addCategory(Intent.CATEGORY_DEFAULT);
smsIntent.putExtra("sms_body","Hello this is dummy text");
startActivity(smsIntent);
If you don't want any text then remove the sms_body key.
Intent smsIntent = new Intent(Intent.ACTION_SENDTO);
smsIntent.setData(Uri.parse("smsto:"+shopkepperDataModel.getPhoneNo().trim()));
smsIntent.addCategory(Intent.CATEGORY_DEFAULT);
startActivity(smsIntent);
Works up to Android 11. (Supports all supported SDKs)
btnMessageTo.setOnClickListener {
val phoneNumber = "+8801234567890"
val intent = Intent("android.intent.action.SENDTO")
intent.data =
Uri.parse("smsto:" + Uri.encode(phoneNumber ))
startActivity(intent)
}

Categories

Resources