Reputation: 9334
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"))
Upvotes: 0
Views: 2265
Reputation: 534
you can replace startActivity(Intent.createChooser(i, "Sharing"))
by startActivity(i)
Upvotes: 0
Reputation: 552
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()
.
Upvotes: 1
Reputation: 2626
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")
}
Upvotes: 2
Reputation: 9334
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.
Upvotes: 0