Android Question Launch a Playstore App from B4A App [Resolved]

Similar threads

B4A Library WebSocket Client Library
B4A Code Snippet Add pinned shortcut
B4A Code Snippet Parsing your apps logs
B4A Question Launch another activity
B4A Tutorial Automatic Foreground Mode

Roger Daley

Well-Known Member
Licensed User
Hi All,
I am trying to start two Apps (Not mine) from my App. The aim, start a "video recorder" and an "upload to Google drive" with a button push.
I have extracted the code I am trying as "stand alone code". I have tried the "Package Name" and the "Launcher Name", same result.
All it does is bring up the settings menu. Neither APP is started.

Obviously I am missing something, any hints gratefully accepted.

B4X:
    #Region  Project Attributes
    #ApplicationLabel: RECORD
    #VersionCode: 1
    #VersionName:
    'SupportedOrientations possible values: unspecified, landscape or portrait.
    #SupportedOrientations: unspecified
    #CanInstallToExternalStorage: False
    #BridgeLogger: True

#Region  Activity Attributes
    #FullScreen: False
    #IncludeTitle: True
#End Region

Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
    Private timer1 As Timer
    Private myIntent1 As Intent
    Private myIntent2 As Intent
    Private jo As JavaObject
End Sub

Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.
End Sub

Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    'Activity.LoadLayout("Layout1")
   
    If FirstTime Then
        StartCamera
        StartUpload
        timer1.Initialize("Timer1", 500)
        timer1.Enabled = True
    End If

End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub


Sub StartCamera
    myIntent1.Initialize(myIntent1.ACTION_MAIN,"")
    myIntent1.SetComponent("com.kimcy929.secretvideorecorder")      'From Package Does not work
    'myIntent1.SetComponent("com.kimcy929.secretvideorecorder.VideoRecorderActivity_1")  'From Launcher Does not work
    StartActivity(myIntent1)
   
'    app:Background Video Recorder
'    package:com.kimcy929.secretvideorecorder
'    Launcher:com.kimcy929.secretvideorecorder.VideoRecorderActivity_1
End Sub

Sub StartUpload
    myIntent2.Initialize(myIntent1.ACTION_MAIN,"")
    myIntent2.SetComponent("com.icecoldapps.synchronizeultimate")  'From Package Does not work
    'myIntent2.SetComponent("com.icecoldapps.synchronizeultimate")  'From Launcher  Does not work
    StartActivity(myIntent2)
 
'    app:Synchronize Ultimate
'    package:com.icecoldapps.synchronizeultimate
'    Launcher:com.icecoldapps.synchronizeultimate.viewStart1
End Sub

Sub CloseActivities
    jo.InitializeContext
    jo.RunMethod("finishAffinity", Null)
End Sub

Sub Timer1_Tick
    timer1.Enabled = False                ' Stop Timer
    CloseActivities
End Sub
 

Myr0n

Active Member
Licensed User
Try this to launch the app, I installed the app and works, maybe there is another option but this works for me with almost any app.
B4X:
Sub Button2_Click
    RunAppByPackage("com.kimcy929.secretvideorecorder",True)
End Sub

#Region LaunchOtherApp
'Usage: Launch another app, inside of your app meaning that when you click on recent apps button
'will show your app name and not the other app that you are launching,
'or as external app meaning that when you press the recent apps button will show your app name and
'the app that you are launching.
'Parameters:
'packageName as String = this is the package name of the app that you want to launch, could be the partial package name too.
'LaunchUsingAlternativeMode as boolean = True for Launch inside of your app, False as external.
'Example:
'<code>
    'RunAppByPackage("package.name.that.you.wantToLaunch",False)
    'RunAppByPackage("com.sec.android.app.clockpackage/.AlarmActivity",True)
    'RunAppByPackage("com.sec.android.app",True)
    'RunAppByPackage("com.android.settings/.wifi.WifiPickerActivity",True)
'</code>
Sub RunAppByPackage(packageName As String, LaunchUsingAlternativeMode As Boolean)
    Dim Pm As PackageManager
    Dim Inte As Intent
    Dim st As String = packageName
    If packageName.IndexOf("/")=-1 Then
        Dim Packages As List
        Packages = Pm.GetInstalledPackages
        For i = 0 To Packages.size - 1
            st=Packages.Get(i)
            If st.ToLowerCase.Contains(packageName.ToLowerCase) = True Then
                Inte=Pm.GetApplicationIntent(st)
                Log(st)
                If Not(Inte.IsInitialized) Or LaunchUsingAlternativeMode Then
                    Inte.Initialize(Inte.ACTION_MAIN, "")
                    Dim packageNamePlusActivityName As String = GetIntentActivityFromAPackage(st)
                    If packageNamePlusActivityName.Length>0 Then Inte.SetComponent(packageNamePlusActivityName)
                End If
                Exit '<---Will pick the first package on the list
            End If
        Next
    Else
        Inte.Initialize(Inte.ACTION_MAIN, "")
        Inte.SetComponent(st)
    End If
    Try
        If Inte.IsInitialized Then StartActivity(Inte)
    Catch
        Log("failed launching "&st)
    End Try
End Sub

'Usage : Return the first activity that found with the package name
Sub GetIntentActivityFromAPackage(packageName As String) As String
    Dim i As Int =0
    Dim pm As PackageManager
    Dim FoundFirstActivityName As String = ""
    Dim Intent1 As Intent =pm.GetApplicationIntent(packageName)
    For Each cn As String In pm.QueryIntentActivities(Intent1)
        Log(cn)
        i=i+1
        If i=1 Then FoundFirstActivityName=cn
    Next
    Return FoundFirstActivityName
End Sub
#End Region LaunchOtherApp
 

Roger Daley

Well-Known Member
Licensed User
MyrOn

Thanks for the code it works well for launching either APP. Unfortunately when I use it to start both APPS they appear to clash and only one works. It depends on which order they are started.
I will keep trying.

Regards Roger
 
Top