Android Question [B4X] Threads URLScheme for sharing text

Andrew (Digitwell)

Well-Known Member
Licensed User
Longtime User
Threads is blowing up at the moment.

here is how you can share directly with the app in the same way that is possible with Twitter

B4X:
Sub ShareThreads(txt As String)
#if B4a
    Dim i As Intent
    i.Initialize(i.ACTION_SEND, "")
    i.PutExtra("android.intent.extra.TEXT", txt)
    i.SetType("text/plain")
    i.SetPackage("com.instagram.barcelona")
    Try
        StartActivity(i)
           
    Catch
        MsgboxAsync("There was a problem starting Threads.","error")
    End Try
   
#else if B4I  

    Private s As String
    Private su As StringUtils
       
    s = "barcelona://post/?text="&su.EncodeUrl(txt,"UTF8")
       
    If (Main.App.CanOpenURL(s)) Then
        Main.App.OpenURL(s)
    Else
        Msgbox("Could not start Threads","error")
    End If
#End If
End Sub

to check if Threads is installed.

B4X:
Sub CheckThreads As Boolean
#if b4a
    Return isPackageInstalled("com.whatsapp")
#else
    If (Main.App.CanOpenURL("barcelona://")) Then
        Return True
    Else
        Return False
    End If
#end if      
End Sub

#if b4a
public Sub isPackageInstalled(pkname As String) As Boolean
    'https://stackoverflow.com/questions/18752202/check-if-application-is-installed-android
    Dim ctxt As JavaObject
    ctxt.InitializeContext
    Try
    ctxt.RunMethodJO("getPackageManager", Null).RunMethod("getPackageInfo", Array(pkname, 0))
    Return True      
    Catch
        Log(LastException.message)
        Return False
    End Try
End Sub
#end if

You need to include:
this in your manifest for B4A

B4X:
AddManifestText(
<queries>
    <package android:name="com.instagram.barcelona"/>
</queries>
)

and this in your main for b4i

B4X:
#QueriesSchemes:barcelona

Edited: to replace some of my internal functions
 
Top