Android Tutorial [TUTORIAL] Inter-app Communication with Intents

Federico956

Member
Licensed User
Longtime User
Sure @tdocs2, I've done it!
Hi all,
I have a problem. I'm using a TC55 with its barcode scanner and its scanning app (DataWedge). In DataWedge there are some settings that allow you to use intent (Action, Category and delivery type), so when you scan a code you can have the code "into your app" and you can manipulate it.
I wrote a little code to try it and it works great, so I've tried to implement the code into my app and I noticed a strange behaviour: the first little app has only one activity and it works correctly, my app has lots of activity but I have to use the barcode scanner only in one of them.
When I scan a barcode, instead of execute the code written in Activity_Resume, it is called the activity create of the main activity.
I know it is something strange to explain, I hope you'll understand.

Here's the code of the Activity_Resume:

B4X:
Sub Activity_Resume
   Dim myintent As Intent
   myintent = Activity.GetStartingIntent
   If myintent.GetExtra("com.motorolasolutions.emdk.datawedge.data_string") <> Null Then
     Dim dwstring As String = "com.motorolasolutions.emdk.datawedge.data_string"
     Dim read As String = myintent.GetExtra(dwstring)
     EditBC.Text = read
   End If
End Sub

Thanks
 

Federico956

Member
Licensed User
Longtime User
Here it is the manifest:

B4X:
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$")
AddActivityText(Main, <intent-filter>
  <action android:name="com.IntentProvider.REQUEST" />
  <category android:name="android.intent.category.DEFAULT" />
</intent-filter>)

It is the same in the small app I've tried and in my app.

EDIT: AddActivityText(Main, <intent-filter>
I have found the stupid but big error, I applied the intent-filter only to Main activity, right?
 
Last edited:

Federico956

Member
Licensed User
Longtime User
I've done it, now it works perfectly, thank you so much!
I've another (maybe the last ) question: is there a way, in a Start Activity intent, to "abort" the intent after receiving data?
 

thedesolatesoul

Expert
Licensed User
Longtime User
I've done it, now it works perfectly, thank you so much!
I've another (maybe the last ) question: is there a way, in a Start Activity intent, to "abort" the intent after receiving data?
After you took the data you can call Activity.Finish to shut down the current activity. This will though create a small flicker of your activity showing and disappearing. If you want to do it in the background, you can also use a Service for this.
 

imbault

Well-Known Member
Licensed User
Longtime User

Thank you @thedesolatesoul ,

Very interesting, when you need APK communications, and I need a solution right now...

Just a simple question, does an App can be as well Provider and Requester?

Do we have others alternatives on Android (except file semaphores or SQLite solutions)?

Many thanks.

Patrick
 

johndb

Active Member
Licensed User
Longtime User
I'm using the information presented in this tutorial as a basis for my inter-app communications and it works very well with SDK 19 and below. Communicating with services using implicit intents fail in apps targeted for apps using SDK above 19 due to increased android security. How do I change the information in the tutorial to use explicit intents for service communications?

Thank you in advance.

[SOLVED]

To make the intent directed to a service "explicit" simply add the following:

Intent.SetComponent("<package name>/.<service>")


John
 
Last edited:

Rusty

Well-Known Member
Licensed User
Longtime User
Does anyone have a working simple example both provider and requestor?
Thanks,
Rusty
 

Rusty

Well-Known Member
Licensed User
Longtime 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:
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:
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
Longtime 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
Longtime User
What wearable device are you using? (brand, model, Android O/S)
Rusty
 
Cookies are required to use this site. You must accept them to continue using the site. Learn more…