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
Spoiler: Code
Code:
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
Spoiler: Android Training's code
Code:
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.