Android Question Starting an external app

Andrew (Digitwell)

Well-Known Member
Licensed User
Longtime User
I would like to start social apps, Twitter, Facebook, whatsapp etc from my app on API33

Prior to API31, you could use the package manager to check if the app is installed and then start the app.

It is still possible to check if an app exists using the following:

B4X:
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
    Dim pkInfo As JavaObject = ctxt.RunMethodJO("getPackageManager", Null).RunMethod("getPackageInfo", Array(pkname, 0))
    Return True       
    Catch
        Log(LastException.message)
        Return False
    End Try
End Sub

log(isPackageInstalled("com.whatsapp"))

This also requires that the app id is in your manifest:
'https://www.b4x.com/android/forum/threads/alternative-to-check-if-whatsapp-is-installed-without-using-packagemanager-query_all_packages-on-sdk30.135863/post-861640 AddManifestText( ):
'https://www.b4x.com/android/forum/threads/alternative-to-check-if-whatsapp-is-installed-without-using-packagemanager-query_all_packages-on-sdk30.135863/post-861640
AddManifestText(
<queries>
    <package android:name="com.whatsapp"/>
    <package android:name="com.whatsapp.w4b"/>
    <package android:name="com.instagram.android"/>
    <package android:name="com.twitter.android"/>
    <package android:name="com.facebook.katana"/>
</queries>
)

However, the code to start the app does not work:

B4X:
Sub ShareWhatsApp(txt As String)

    Dim i As Intent
    i.Initialize(i.ACTION_SEND, "")
    i.PutExtra("android.intent.extra.TEXT", txt)
    i.PutExtra("android.content.Intent.EXTRA_TITLE", Globals.APPLICATION_TITLE)
    i.SetType("text/plain")
    i.SetPackage("com.whatsapp")
    Try
        StartActivity(i)       
    Catch
        MsgboxAsync("There was a problem starting Whats App.",Globals.APPLICATION_TITLE)
 ' error here is -> java.lang.Exception:  android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.SEND flg=0x20001 pkg=com.whatsapp clip={null {...}} (has extras) }

    End Try
end sub

Does anyone know how to start an external app, specifically, whatsapp, twitter, instagram and facebook in API 33.

Thanks
 

drgottjr

Expert
Licensed User
Longtime User
so, i don't use any of those "services", but - special for you - i downloaded and installed instagram.
i then wrote a simple test to launch it via an intent. i'm running b4a 12.20, android 13, sdk 33.
here is the code:
B4X:
    intent.Initialize(intent.ACTION_VIEW,"")
    intent.SetPackage("com.instagram.android")

    Try
        StartActivity(intent)

below please find a screen cap of my soon-to-be-removed instagram.

note 3 things of interest:
1) i did not add the queries to the manifest, but, rather, set the package name in the intent. instagram was successfully launched.
2) when i did a test after adding the queries to the manifest, it is no longer necessary to specify the package name in the intent. instagram was successfully launched without it.
3) sending text as an extra doesn't seem to work for instagram (i wouldn't know), so i simply tried to launch it as per code above.

sorry, but i couldn't possibly download the others even if you paid me. i can only say, there was no evident problem launching instagram on android 13 and sdk 33. now i must quickly uninstall instagram before something happens.
 

Attachments

  • capture.png
    capture.png
    22.3 KB · Views: 57
  • capture2.png
    capture2.png
    39.8 KB · Views: 61
Upvote 0

Andrew (Digitwell)

Well-Known Member
Licensed User
Longtime User
Hi. Thanks for your analysis.
The queries are needed so that the isPackageInstalled sub works. Without them you cannot check to see if the app is installed.

Yes the action_VIEW works for Instagram (it does not work for WhatsAPP), however, I was trying to use ACTION_SEND so that I could pass some text to the app.

i tried removing all of the parameters but it still does not work.

B4X:
    Dim i As Intent
    i.Initialize(i.ACTION_SEND, "")
'    i.PutExtra("android.intent.extra.TEXT", txt)
'    i.PutExtra("android.content.Intent.EXTRA_TITLE", Globals.APPLICATION_TITLE)
'    i.SetType("text/plain")
    i.SetPackage("com.instagram.android")
    Try
        StartActivity(i)
            
    Catch
        MsgboxAsync("There was a problem starting Instagram. "&LastException.Message,Globals.APPLICATION_TITLE)

    End Try
    
The error returned is:
android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.SEND flg=0x20000 pkg=com.instagram.android }
 
Upvote 0

drgottjr

Expert
Licensed User
Longtime User
there are, at least, 2 ways to launch whatsapp via an intent with parameters.
i have tried both; they both work (albeit to slightly different degrees
of success). i use your code, including the manifest entry and your
isPackageInstalled() and ShareWhatsApp() routines. in
one way, i use your ShareWhatsApp() sub, and in the other way, i
use a slightly different sub. using the second way, whatsapp is
launched and sends a message. regardless of the method used,
the point is: the intent launches whatsapp and instagram. i never
see the activitynotfound error. nor should you.

i can now successfully launch whatsapp and instagram using
your code on an android13 device and targeting sdk33. why you
cannot launch the apps i cannot say. i responded to your post
because it did not sound right that you were unable to launch the
apps. since i can launch at least 2 of the apps on your list using
your code, it should be clear that the code you have posted is
correct, so everything should work on your device as well.
if there is some other unseen code in your app which, somehow,
nullifies the posted code, i cannot say. the posted code should launch
the apps, at the very least.
 
Upvote 0

Andrew (Digitwell)

Well-Known Member
Licensed User
Longtime User
Great to know it does work.

I will try to work out what I am doing wrong. I am running the code an a Pixel 2XL running Android 11, but the code has been compiled with API 33 jar and the target is API 33.

The code is part of a larger app. I will create a sample program and see what happens.

Thanks for looking.
 
Upvote 0

drgottjr

Expert
Licensed User
Longtime User
your posted code is enough. that's what i used.
fyi, the change regarding how to query installed apps started with android 11, not 13. and if you still have android.jar 30, try with that. there could be a problem with android 11 and sdk 33
 
Upvote 0
Top