Use service StartingIntent to pass values

warwound

Expert
Licensed User
Longtime User
Hi.

Is it possible to pass values to a service using an Intent?

I see the Service_Start Sub receives an Intent StartingIntent and wondered if i could add values as extras to that Intent from my calling code.

The general recommended advice seems to be to use globals to pass values to a service but i thought i'd ask anyway.

Activity code:

B4X:
' i have some values to pass to MyService
StartService(MyService)

Service code:

B4X:
Sub Service_Start (StartingIntent As Intent)
 ' I want StartingIntent to contain those values   
End Sub

Any ideas?

Martin.
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Yes it is possible.
You should create an Intent and add the values:
B4X:
   Dim i As Intent
   i.Initialize("", "")
   i.SetComponent(package & "/.pushservice")
   i.PutExtra("MyRequest", "Register")
   StartService(i)
package - A variable that holds the package name.

In this case we start a service named PushService. Internally the name is lowercased, so you should use the lower case name.
 
Upvote 0

derez

Expert
Licensed User
Longtime User
Hi
I'm using recieved SMS to start an application.
B4X:
Sub Service_Start(startingIntent As Intent)
  If startingIntent.Action = "android.provider.Telephony.SMS_RECEIVED" Then
        StartActivity(Main)
  End If
End Sub
Is it possible to pass a parameter in the sent sms and get it here ?
Of course I could check in main the content of the sms and use it but I wondered if it could be done by the statrtingintent.getextra
 
Upvote 0
Top