Android Question Open Telegram as intent

uniplan

Active Member
Licensed User
Longtime User
I would like to open Telegram as intent and pass a text to be sent.

I have seen some posts but I could not find a solution.

What is the right way to do this?

Thanks.
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Try this:
B4X:
Sub ShareWithIntent(msg As String)
   Dim in As Intent
   in.Initialize(in.ACTION_SEND, "")
   Dim jo As JavaObject = in
   jo.RunMethod("setPackage", Array("org.telegram.messanger"))
   in.PutExtra("android.intent.extra.TEXT", msg)
   in.SetType("text/plain")
   StartActivity(in)
End Sub
 
Last edited:
Upvote 0

uniplan

Active Member
Licensed User
Longtime User
I have this error:

B4X:
** Service (starter) Create **
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
Error occurred on line: 80 (Main)
android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.SEND flg=0x20001 pkg=org.telegram.messanger (has clip) (has extras) }
    at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1765)
    at android.app.Instrumentation.execStartActivity(Instrumentation.java:1485)
    at android.app.Activity.startActivityForResult(Activity.java:3824)
    at android.app.Activity.startActivityForResult(Activity.java:3785)
    at android.app.Activity.startActivity(Activity.java:4095)
    at android.app.Activity.startActivity(Activity.java:4063)
    at anywheresoftware.b4a.keywords.Common.StartActivity(Common.java:698)
    at java.lang.reflect.Method.invoke(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:372)
    at anywheresoftware.b4a.shell.Shell.runVoidMethod(Shell.java:747)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:342)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:246)
    at java.lang.reflect.Method.invoke(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:372)
    at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:134)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:157)
    at anywheresoftware.b4a.BA.raiseEvent(BA.java:153)
    at anywheresoftware.b4a.objects.ViewWrapper$1.onClick(ViewWrapper.java:78)
    at android.view.View.performClick(View.java:4764)
    at android.view.View$PerformClick.run(View.java:19844)
    at android.os.Handler.handleCallback(Handler.java:739)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:135)
    at android.app.ActivityThread.main(ActivityThread.java:5376)
    at java.lang.reflect.Method.invoke(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:372)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:908)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:703)
 
Upvote 0

lemonisdead

Well-Known Member
Licensed User
Longtime User
Perhaps a typo in the package's name?

=> org.telegram.messenger
 
Upvote 0

uniplan

Active Member
Licensed User
Longtime User
I have corrected "org.telegram.messanger" in "org.telegram.messenger" but I have the same error.
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
I am not so expert, you still need Erel's help; moreover, I have not Telegram installed.

I found this code (http://stackoverflow.com)
B4X:
<Omissis>

Intent myIntent = new Intent(Intent.ACTION_SEND);
myIntent.setType("text/plain");
myIntent.setPackage(appName);
myIntent.putExtra(Intent.EXTRA_TEXT, msg);//
mUIActivity.startActivity(Intent.createChooser(myIntent, "Share with"));

...

I think the createChooser part is needed.

I am far from certain, waiting Erel, I would try so:
B4X:
Sub ShareWithIntent(msg As String)
       Dim in As Intent
       in.Initialize(in.ACTION_SEND, "")
       Dim jo As JavaObject = in
       jo.RunMethod("setPackage", Array("org.telegram.messenger"))
       in.SetType("text/plain")
       in.PutExtra("android.intent.extra.TEXT", msg)
       StartActivity(jo.RunMethod("createChooser", Array As Object(in, "Share with")))
End Sub

Given that I added also:
in.SetType("text/plain")

perhaps only this was lacking; if so, and the code above does not work, try running directly:
StartActivity(in)
instead of:
StartActivity(jo.RunMethod("createChooser", Array As Object(in, "Share with")))
 
Last edited:
Upvote 0

uniplan

Active Member
Licensed User
Longtime User
I tried with the additions suggested by you.
I have added:
B4X:
in.SetType("text/plain")

and

B4X:
StartActivity(jo.RunMethod("createChooser", Array As Object(in, "Share with")))

It works properly.
Thank you.
 
Upvote 0

KY Leng

Active Member
Licensed User
Longtime User
I have tried the Erel solution and working properly. Thanks to both.
With Erel code:
StartActivity(in)
=> I got error,
with:
StartActivity(jo.RunMethod("createChooser", ArrayAs Object(in, "Share with")))
=> No error but I see only a white panel with text in the middle as "No apps can perform this action."

So, can anyone help me please ? In my phone, i already have Telegram installed and has create many chart room/group...
To pass the text to an existing group, how to do it ?
 
Upvote 0

KY Leng

Active Member
Licensed User
Longtime User
When I use Erel code:

Sub ShareWithIntent(msg AsString)
Dim in As Intent
in.Initialize(in.ACTION_SEND, "")
Dim jo As JavaObject = in
jo.RunMethod("setPackage", Array("org.telegram.messanger"))
in.PutExtra("android.intent.extra.TEXT", msg)
in.SetType("text/plain")
StartActivity(in)
End Sub


I got this error. Please see below log data.


** Service (starter) Create **
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
> Read Parameters
** Activity (main) Resume **

Error occurred on line: 1064 (Main)
android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.SEND typ=text/plain flg=0x20001 pkg=org.telegram.messanger VirtualScreenParam=Params{mDisplayId=-1, null, mFlags=0x00000000)} clip={text/plain T:Test send text to Telegram} (has extras) }
at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1878)
at android.app.Instrumentation.execStartActivity(Instrumentation.java:1545)
at android.app.Activity.startActivityForResult(Activity.java:4283)
at android.app.Activity.startActivityForResult(Activity.java:4230)
at android.app.Activity.startActivity(Activity.java:4567)
at android.app.Activity.startActivity(Activity.java:4535)
at anywheresoftware.b4a.keywords.Common.StartActivity(Common.java:698)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.shell.Shell.runVoidMethod(Shell.java:747)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:342)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:246)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:134)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:157)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:153)
at anywheresoftware.b4a.objects.ViewWrapper$1.onClick(ViewWrapper.java:78)
at android.view.View.performClick(View.java:5697)
at android.widget.TextView.performClick(TextView.java:10814)
at android.view.View$PerformClick.run(View.java:22526)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:158)
at android.app.ActivityThread.main(ActivityThread.java:7229)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…