Android Question Really trying to understand JavaObject

lemonisdead

Well-Known Member
Licensed User
Longtime User
Hello,

Summary (for those in a hurry to avoid having to read all): I am trying to understand where to find the information you use to make JavaObject working

Detailed question (for the most patient people)
The need of receiving the MEDIA_BUTTON press intent made me in need to understand the code kindly provided by Erel
B4X:
Sub RegisterForMediaButton (ServiceName As String)
   Dim am As JavaObject = GetContext.RunMethodJO("getSystemService", Array("audio"))
   Dim component As JavaObject
   component.InitializeNewInstance("android.content.ComponentName", Array(Application.PackageName, ServiceName.ToLowerCase))
   am.RunMethod("registerMediaButtonEventReceiver", Array(component))
End Sub
Sub GetContext As JavaObject
   Return GetBA.GetField("context")
End Sub

Sub GetBA As JavaObject
  Dim jo As JavaObject
  Dim cls As String = Me
  cls = cls.SubString("class ".Length)
  jo.InitializeStatic(cls)
  Return jo.GetFieldJO("processBA")
End Sub

In the same time, the Android's documentation tells us to register using the AUDIO_SERVICE
B4X:
AudioManager am = mContext.getSystemService(Context.AUDIO_SERVICE);

...// Start listening for button presses
am.registerMediaButtonEventReceiver(RemoteControlReceiver);

...// Stop listening for button presses
am.unregisterMediaButtonEventReceiver(RemoteControlReceiver);
AudioManager am = mContext.getSystemService(Context.AUDIO_SERVICE);

As I understand the B4A code :
  • GetBA returns our application "package's name" so it is not hard coded and can be used elsewhere
  • GetContext returns the application's context so the system knows which app is acting
  • RegisterForMediaButton registers the service associated with the app to the system. For example: com.b4x.sample/.serviceTest
Now, I am focusing on comparing RegisterForMediaButton's and the Java codes and I am understanding a little because I do find some similarities. And that's all...

Because, I don't understand why we call "audio" instead of "AUDIO_SERVICE", I have read the page about the AudioManager. Do we load all the information about the "audio" package ?
In Java they speak about
Use Context.getSystemService(Context.AUDIO_SERVICE) to get an instance of this class.

More, on the RemoteControlClient's page they specify the method "registerMediaButtonEventReceiver" is deprecated since api 5.0 and they advice to use the MediaSession's class. On that page, I find the new method to use "setMediaButtonReceiver".

And now, I am lost again while trying to replace the "RegisterForMediaButton" by "setMediaButtonReceiver" to test it on the emulator and check if one day I could use JavaObject alone instead of disturbing you everytime I need something sharp to be done. And boom : an error thrown.
 

Robert Valentino

Well-Known Member
Licensed User
Longtime User
Did you every work this out and get everything working.

I do not know if it is my S3 but I cannot get any of this Media Button Receiver stuff to work.

One thing I am curious about is why you take the ServiceName and make it lower case?
 
Upvote 0

thedesolatesoul

Expert
Licensed User
Longtime User
Upvote 0

stevel05

Expert
Licensed User
Longtime User
Context.AUDIO_SERVICE is a Constant defined in the Context class declared as a string, whose value is "audio". No more complex than that.
 
Upvote 0

lemonisdead

Well-Known Member
Licensed User
Longtime User
@thedesolatesoul & @stevel05 : I do sincerely thank you for having cleared the things. I had missed the information. It is clearer now. :)

@Robert Valentino : no, I have not found a solution for the moment. But Stari told me yesterday that he probably had found a solution and he is working on it. In the same time, he provided me with a code posted elsewhere. If you know about making a library, here is Stari's post. And I put the service in lower case because the name of my service is in lower case

Edited
 
Last edited:
Upvote 0

Robert Valentino

Well-Known Member
Licensed User
Longtime User
Well that is interesting. Does CASE matter. My service name is in Mixed Case could this be why it is not working. Will remove this and try.
 
Upvote 0
Top