Android Question Start another app from service

imgsimonebiliato

Well-Known Member
Licensed User
Longtime User
I need to open the "Main" activity of my project "it.android.imgspa.and_StompClient", from a service of another app ""it.android.imgspa.and_StompClientService".

This is my service code:


B4X:
#Region  Service Attributes
    #StartAtBoot: True
    #StartCommandReturnValue: android.app.Service.START_STICKY
#End Region

Sub Process_Globals
End Sub

Sub Service_Create
End Sub

Sub Service_Start (StartingIntent As Intent)
Try
    If StartingIntent.HasExtra("android.intent.extra.ALARM_COUNT") Then
        StartServiceAt("", TimeToNextSeconds (60), False)
    Else
        StartServiceAt("", TimeToNextSeconds (5), False)
    End If

Catch
    StartActivity(Main)
End Try

DateTime.DateFormat  = "dd/MM/yyyy HH:MM:ss"
Log(DateTime.Date(DateTime.Now) & " hello")

StartStompClient
End Sub

Sub TimeToNextSeconds (aSeconds As Int)As Long
Dim ret As Long

Log("Service stars on " & aSeconds)

ret = DateTime.Now + DateTime.TicksPerSecond * aSeconds
ret = ret - (ret Mod DateTime.TicksPerSecond)
Return ret
End Sub

Private Sub StartStompClient
Dim Intent1 As Intent
Intent1.Initialize(Intent1.ACTION_MAIN, "")
Intent1.SetComponent("it.android.imgspa.and_StompClient/.Main")
Intent1.Flags = 33554432
StartActivity(Intent1)
End Sub

The intent start activity gets me this:

B4X:
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
** Service (stompclientservice) Create **
** Service (stompclientservice) Start **
Service stars on 5
22/10/2018 13:10:34 hello
android.content.ActivityNotFoundException: Unable to find explicit activity class {it.android.imgspa.and_StompClient/it.android.imgspa.and_StompClient.Main}; have you declared this activity in your AndroidManifest.xml?
    at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1634)
    at android.app.Instrumentation.execStartActivity(Instrumentation.java:1430)
    at android.app.ContextImpl.startActivity(ContextImpl.java:1173)
    at android.app.ContextImpl.startActivity(ContextImpl.java:1148)
    at android.content.ContextWrapper.startActivity(ContextWrapper.java:311)
    at anywheresoftware.b4a.keywords.Common.StartActivity(Common.java:686)
    at it.android.imgspa.and_Stomp_Client_Service.stompclientservice._startstompclient(stompclientservice.java:155)
    at it.android.imgspa.and_Stomp_Client_Service.stompclientservice._service_start(stompclientservice.java:139)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:515)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:175)
    at anywheresoftware.b4a.BA.raiseEvent(BA.java:159)
    at it.android.imgspa.and_Stomp_Client_Service.stompclientservice.handleStart(stompclientservice.java:68)
    at it.android.imgspa.and_Stomp_Client_Service.stompclientservice.onStartCommand(stompclientservice.java:53)
    at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2893)
    at android.app.ActivityThread.access$2100(ActivityThread.java:151)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1439)
    at android.os.Handler.dispatchMessage(Handler.java:110)
    at android.os.Looper.loop(Looper.java:193)
    at android.app.ActivityThread.main(ActivityThread.java:5322)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:515)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:829)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:645)
    at dalvik.system.NativeStart.main(Native Method)

What activity i have to declare? Where? How?
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Dim Intent1 As Intent
Intent1.Initialize(Intent1.ACTION_MAIN, "")
Intent1.SetComponent(
"it.android.imgspa.and_StompClient/.Main")
Intent1.Flags =
33554432
StartActivity(Intent1)
Where does this code come from?

Why aren't you using StartActivity(Main) ?

Note that sticky services are no longer recommended. You should use a foreground service if you want your app to keep running.
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Upvote 0

imgsimonebiliato

Well-Known Member
Licensed User
Longtime User
Where does this code come from?

Why aren't you using StartActivity(Main) ?

Note that sticky services are no longer recommended. You should use a foreground service if you want your app to keep running.
I want to open a different app
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
I want to open a different app
try using Packagemanager (i tried it with this code (for sure with the real packagename of my app) to start a 2nd app from the app running this code. It is working fine for me.

B4X:
    Dim in As Intent = pm.GetApplicationIntent("it.android.imgspa.and_StompClient") ' Maybe packagename isnt allowed to have an underscore or maybe it shoud all be lowercased
    Log(in)
    StartActivity(in)
 
Last edited:
Upvote 0

imgsimonebiliato

Well-Known Member
Licensed User
Longtime User
try using Packagemanager (i tried it with this code (for sure with the real packagename of my app) to start a 2nd app from the app running this code. It is working fine for me.

B4X:
    Dim in As Intent = pm.GetApplicationIntent("it.android.imgspa.and_StompClient") ' Maybe packagename isnt allowed to have an underscore or maybe it shoud all be lowercased
    Log(in)
    StartActivity(in)
Useful for other application not b4a
 
Upvote 0
Top