Android Tutorial [TUTORIAL] Inter-app Communication with Intents

Rusty

Well-Known Member
Licensed User
OK, after following desolatesoul's advice and messing around with some of the manifests, I've made this work.
Since there is no sample code, I'm uploading my test code for anyone who wishes to use this feature.

REQUESTOR: Build configuration Package Name: thisapp.b4a.intentrequestor
Manifest:
AddManifestText(
<uses-sdk android:minSdkVersion="4" android:targetSdkVersion="14"/>
<supports-screens android:largeScreens="true"
android:normalScreens="true"
android:smallScreens="true"
android:anyDensity="true"/>)
SetApplicationAttribute(android:icon, "@drawable/icon")
SetApplicationAttribute(android:label, "$LABEL$")
'End of default text.
AddActivityText(Main, <intent-filter>
<action android:name="thisapp.b4a.intentrequestor.CALLBACK" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>)
Code:
B4X:
#Region  Project Attributes
    #ApplicationLabel: IntentRequestor
    #VersionCode: 1
    #VersionName:
    'SupportedOrientations possible values: unspecified, landscape or portrait.
    #SupportedOrientations: unspecified
    #CanInstallToExternalStorage: False
#End Region

#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.

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.
    Dim Button1 As Button
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("1")
    Activity.Title="IntentRequester"
End Sub

Sub Activity_Resume
    Dim in As Intent
    in = Activity.GetStartingIntent
    If in <> Null Then
        If in.HasExtra("Settings") Then
            Dim xtra As String = in.ExtrasToString
            LogColor("            REQUESTOR "  & xtra, Colors.Blue)
        End If
    End If
End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub Button1_Click
    Dim in As Intent
    in.Initialize("thisapp.b4a.intentprovider.REQUEST","")
    in.AddCategory("android.intent.category.DEFAULT")
    in.PutExtra("Callback","thisapp.b4a.intentrequestor.CALLBACK")
    in.PutExtra("GetSettings","True")
    StartActivity(in) 'Or StartService(returnIntent)
End Sub
PROVIDER: Build configuration Package Name: thisapp.b4a.intentprovider
Manifest:
AddManifestText(
<uses-sdk android:minSdkVersion="11" android:targetSdkVersion="14"/>
<supports-screens android:largeScreens="true"
android:anyDensity="true"/>)
AddActivityText(Main, <intent-filter>
<action android:name="thisapp.b4a.intentprovider.REQUEST" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>)
SetApplicationAttribute(android:icon, "@drawable/icon")
SetApplicationAttribute(android:label, "$LABEL$")

'End of default text.
Code:
B4X:
#Region  Project Attributes
    #ApplicationLabel: IntentProvider
    #VersionCode: 1
    #VersionName:
    'SupportedOrientations possible values: unspecified, landscape or portrait.
    #SupportedOrientations: unspecified
    #CanInstallToExternalStorage: False
#End Region

#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.

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.

    Private Label1 As Label
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("1")
    Activity.Title="IntentProvider"
    Label1.Text=DateTime.Time(DateTime.now)

End Sub

Sub Activity_Resume
Log("PROVIDER ******************************")  
    Dim in As Intent
     in = Activity.GetStartingIntent
    If in <> Null Then
        If in.HasExtra("GetSettings") Then
            Dim callback As String = in.GetExtra("Callback")
LogColor("            PROVIDER: GetStartingIntent", Colors.Red)
LogColor("                came from: " & callback, Colors.red)
           SendSettings(in)
           Activity.Finish
           Return
        End If
     End If
End Sub

Sub SendSettings(in As Intent)
    Dim returnIntent As Intent
    returnIntent.Initialize(in.GetExtra("Callback"),"")
    returnIntent.AddCategory("android.intent.category.DEFAULT")
    returnIntent.PutExtra("Settings", "Hello")
    returnIntent.PutExtra("Successful","True")
    StartActivity(returnIntent) 'Or StartService(returnIntent)
    LogColor("            PROVIDER "  & returnIntent.ExtrasToString, Colors.red)
  
End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub
Compile and install both programs on a device. The Requestor has a button, touch it to test the code. Watch the Logging for results.
I hope this helps someone
Rusty
 

wenzelww

Member
Licensed User
Hi Rusty,
thank's for putting this together it helps me a lot. I tried to use this example with a wearable device. The mobile device is the IntendRequestor, the wearable is the IntentProvider it compiles but an error occurs when sending. the Request intent
(No Activity found to handle intent).
Do you know how to do it with wearable?

Wilhelm
 

Rusty

Well-Known Member
Licensed User
What wearable device are you using? (brand, model, Android O/S)
Rusty
 

wenzelww

Member
Licensed User
Just wanted to let you know that I could find a working solution inside the Wearable Datalayer threat:

static - More complex but means you can receive messages at any time
Add the following text to the Manifest Editor
<code>AddApplicationText(
<service android:name="barxdroid.wearabledatalayer.ListenerService"
android:label="Wearable Listener">
<intent-filter>
<action android:name="com.google.android.gms.wearable.BIND_LISTENER" />
</intent-filter>
</service>)
</code>
Then add a service called 'WearListenerService to your project and in the service module, use code like this.
<code>
Sub Process_Globals
Dim WL As WearableListener
End Sub
Sub Service_Start (StartingIntent As Intent)
WL.Initialize("WL")
WL.HandleIntent(StartingIntent)
End Sub
Sub WL_MessageReceived(SourceNodeID As String, RequestID As Int, msgPath As String, Data As String)
ToastMessageShow(Data, False)
End Sub
</code>


Wilhelm
 

brunnlechner

Member
Licensed User
Hello,
if I change to android:targetSdkVersion="26", I get the error:

java.lang.IllegalArgumentException: Service Intent must be explicit: Intent....


B4X:
    Dim in As Intent
    in.Initialize("thisapp.b4a.intentprovider.REQUEST","")
    StartService(in)

Franz
 
Last edited:

brunnlechner

Member
Licensed User
B4X:
in.SetPackage("provider.app")
'assuming that this is the target app package name

Thanks Erel, you're the best - that's how it works perfectly again.
 
Last edited:
Top