Android Tutorial [TUTORIAL] Inter-app Communication with Intents

Discussion in 'Tutorials & Examples' started by thedesolatesoul, Jun 28, 2013.

  1. Rusty

    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:
    Code:
    Code:
    #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:
    Code:
    Code:
    #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
     
    JordiCP, Jehoschua and DonManfred like this.
  2. wenzelww

    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
     
  3. Rusty

    Rusty Well-Known Member Licensed User

    What wearable device are you using? (brand, model, Android O/S)
    Rusty
     
  4. wenzelww

    wenzelww Member Licensed User

    Hi Rusty,
    I use a sony smartwatch 3, SWR50, , Andoid 4.x.x Build LCA43
    Wilhelm
     
  5. wenzelww

    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
     
  6. Jehoschua

    Jehoschua Member Licensed User

    @thedesolatesoul: Thank you very much for this Tutorial!, it's a kind of magic :)
     
  7. brunnlechner

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


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

    Franz
     
    Last edited: Feb 19, 2019
  8. DonManfred

    DonManfred Expert Licensed User

  9. brunnlechner

    brunnlechner Member Licensed User

    Code:
    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: Feb 24, 2019
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice