Android Question How to do Intent.SetData in B4A?

JohnC

Expert
Licensed User
Longtime User
Google's docs has this example:

B4X:
public void composeMmsMessage(String message, Uri attachment) {
    Intent intent = new Intent(Intent.ACTION_SEND);
    intent.setData(Uri.parse("smsto:"));  // This ensures only SMS apps respond
    intent.putExtra("sms_body", message);
    intent.putExtra(Intent.EXTRA_STREAM, attachment);
    if (intent.resolveActivity(getPackageManager()) != null) {
        startActivity(intent);
    }
}

Put, the B4A Intent object doesn't have a .setData method. So, how would I be able to do the .setData line in B4A?
 

JohnC

Expert
Licensed User
Longtime User
Hi NJDude,

I appreciate the help. But the problem is that some users use google's "Messenger" app as their default MMS app, and for some reason, when I include the TelNum (the "To") line:

B4X:
iIntent.PutExtra("address", "1234")

The attachment will NOT be attached to the message when Messenger launches. If I remove the above line, then Messenger will first prompt for the recipient, but then the attachment WILL be properly attached to the message.

So, since this problem was occurring when I added the above line, I noticed other code examples that would embed the TelNum differently:

B4X:
iIntent.Initialize("android.intent.action.SEND", "smsto:" & TelNum)

But when I try to send with this line, I get the below error:

B4X:
android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.SEND dat=smsto:xxxxxxxxx-xxxx typ=image/* flg=0x20001 (has clip) (has extras) }


So that's why I wanted to see if that third method of inserting the telnum would work, but I didn't know how to do a "Intent.setData" with B4A's Intent object.
 
Last edited:
Upvote 0

JohnC

Expert
Licensed User
Longtime User
The original post has the full code of the routine that Google posted:

https://developer.android.com/guide/components/intents-common.html#Messaging)

"Uri" is used twice, but they are different - one is the URI of the attachment, the other is using the method ".Parse" of Uri to parse "smsto:".

All I would like to know is how to do the below line in B4A:

B4X:
   intent.setData(Uri.parse("smsto:"));

*** Also, B4A's Intent.initialization expects a String for the second parameter (Uri), so I can't do a:

B4X:
iIntent.Initialize("android.intent.action.SEND", Uri.Parse("smsto:1234567890"))

Because B4A's is expecting a string. And if I simply put "smsto:1234567890" as a second parameter:

B4X:
iIntent.Initialize("android.intent.action.SEND", "smsto:1234567890")

Then it will give me this error:

B4X:
android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.SEND dat=smsto:xxxxxxxxxx typ=image/* flg=0x20001 (has clip) (has extras) }


But if I provide "" as the second parameter it will not throw that error:

B4X:
iIntent.Initialize("android.intent.action.SEND", "")

So, I am hoping that maybe I can embed the telephone number using the .setData method that won't throw an error.
 
Last edited:
Upvote 0

JohnC

Expert
Licensed User
Longtime User
Darn. Thanks for confirming this.
 
Upvote 0
Top