转:android 线控按钮编程
?
为google到此的同学行个方便。
BTW:如果安装了子午播放器,可能receiver接收不到这个MEDIA_BUTTON的动作,具体的可以用DDMS连上去看。我也没分析清楚子午播放器是在哪里注册的。可能是在service里吧。
Many Android devices come with the Music application used to play audio files stored on the device. Some devices ship with a wired headset that features transport control buttons, so users can for instance conveniently pause and restart music playback, directly from the headset.
But a user might use one application for music listening, and another for listening to podcasts, both of which should be controlled by the headset remote control.
If your media playback application creates a media playback service, just like Music, that responds to the media button events, how will the user know where those events are going to? Music, or your new application?
In this article,?we’ll see how to handle this properly in Android 2.2. We’ll first see how to set up intents to receive?“MEDIA_BUTTON”intents. We’ll then describe how your application can appropriately become the preferred media button responder in?Android 2.2. Since this feature relies on a new API, we’ll revisit the use of reflection to prepare your app to take advantage of Android 2.2, without restricting it to?API level 8 (Android 2.2).
In our?AndroidManifest.xml?for this package we declare the class RemoteControlReceiver to receive MEDIA_BUTTON intents:
?
?
?
?
Our class to handle those intents can look something like this:?
?
?
We are now ready to use our two new methods,?registerRemoteControl()and?unregisterRemoteControl()?in a project that runs on devices supporting API level 1, while still taking advantage of the features found in devices running?Android 2.2.
Original article written by?Jean-Michel Trivi. You can find it?here.
Comments and suggestions are welcome!!!..See you…?
?