Android Question How to start a Service in another app with an explicit intent?

JordiCP

Expert
Licensed User
Longtime User
Perhaps it is obvious but after several hours can't see it clear... I had used 'other' intents in previous apps , but not in this exact scenario.

I have 2 apps, A and B, both 'controlled' by me, running in a device.

My purpose is to send some data from app A to a Service in app B, and I want to use intents
If I follow the tutorial and examples HERE, it works correctly sending and receiving data with implicit intents between activities.
But as what I want to call is a Service in app B, the first error that I receive is that the intent should be explicit.

Any idea of what I am missing?


Just in case someone is interested, here there are different tests and partial code....none of the combinations worked, sometimes simply doing nothing and other times throwing runtime error
REQUESTER (app A: thisapp.b4a.intentrequestor)

' None of these tests worked for me
B4X:
Sub test1 
    Dim jo As JavaObject = Me
    Dim jc As JavaObject
    jc.InitializeContext
    jo.RunMethod("sendit",Array(jc))
    Return
End Sub    
   
#if JAVA
import android.content.Context;
import android.content.Intent;
import android.content.ComponentName;

public void sendit(Context cx){
   Intent intent = new Intent("thisapp.b4a.intentprovider.mservice");
   intent.setPackage("thisapp.b4a.intentprovider");
   startService(intent);
}
#End If

'**** another test *****

Sub explicitIntent
    Dim in2 As Intent
    in2.Initialize("thisapp.b4a.intentprovider.mservice","")
    Dim jo As JavaObject = in2
    jo.RunMethod("setPackage", Array("thisapp.b4a.intentprovider")) 
    in2.SetComponent("thisapp.b4a.intentprovider.mservice")
    StartService(in2)   
End Sub


PROVIDER (app B: thisapp.b4a.intentprovider)

mService code
B4X:
' This is where I want to arrive....
Sub Service_Start (StartingIntent As Intent)   
    Log("PROVIDER (SERVICE) CALLED!!!!!!******************************")
   '...
End Sub

Manifest
B4X:
AddApplicationText(
<service
        android:enabled="true"
        android:name=".mservice"
        android:exported="true"
        android:label="My Service" >
    </service>)

' Also have tested intent-filters for the implicit intents, which later discarded ...
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Upvote 0

JordiCP

Expert
Licensed User
Longtime User
Thanks
#2 worked perfectly. The wall against which I was hitting my head was that I was using
B4X:
in.SetComponent("thisapp.b4a.intentprovider.mservice")
instead of
B4X:
in.SetComponent("thisapp.b4a.intentprovider/.mservice")

Just curious, in which sense is it better? Is there any reason (version compatibility or whatever), to prefer the custom action option better than the explicit intent?
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…