This code is good. But can I put it to AndroidManifest.xml? In case the receiver has not run, I think AndroidManifest.xml is the way to register the receiver class.
Thanks, Kenny On Apr 2, 10:19 pm, Henning Schaefer <henning.schae...@gmail.com> wrote: > Sure... you need to implement two classes. One, derived from > BroadcastReceiver, which actually captures the broadcast. And, of > course, the service that keeps the receiver alive all the time. The > service is the easy part. You only need to override two methods: > > @Override > public void onCreate() { > /* register receiver */ > apr = new APReceiver(); /* replace this by your receiver class */ > IntentFilter inf = new IntentFilter(); > inf.addAction("android.intent.action.HEADSET_PLUG"); > registerReceiver(apr, inf); > } > > @Override > public void onDestroy() { > unregisterReceiver(apr); > } > > Of course, apr (type of your receiver class) must be a private field > of your service class. > > Then, you derive your receiver class from BroadcastReceiver and fill > in the onReceive method: > > public void onReceive(Context context, Intent intent) { > if (intent.getExtras().getInt("state") == 0) { > /*HEADSETIS OR HAS BEEN DISCONNECTED */ > }else{ > /*HEADSETIS OR HAS BEEN CONNECTED */ > } > } > > However, you need to be aware that HEADSET_PLUG is a "sticky" event, > every BroadcastReceiver subscribed to that event will receive it upon > construction; I don't know if there is a possibility to determine if > the event has captured at the exact time or been "sticked" for a > while. > > I hope, this short excerpt helps ;) > > On 26 Mrz., 18:37, Gerby <stephan.gerb...@googlemail.com> wrote: > > > > > hi henning > > > do you have some example code for me, how to create a service to > > detect theheadset, please? > > > Cheers > > > On 16 Feb., 17:21, zero <zeroo...@googlemail.com> wrote: > > > > on a side note, i recommend the logcat app for such > > > momentshttp://code.google.com/p/android-random/ > > > > On Feb 16, 5:04 pm, Henning Schaefer <henning.schae...@gmail.com> > > > wrote: > > > > > OK, after digging into the android system code, I think I figured out > > > > what's wrong here... the class "HeadsetObserver" from the base system > > > > actually sends out this intent with its FLAG_RECEIVER_REGISTERED_ONLY > > > > flag set... obviously, there's no way to receive this intent without > > > > implementing a service. > > > > > On 15 Feb., 12:59, Henning Schaefer <henning.schae...@gmail.com> > > > > wrote: > > > > > > Hi all, > > > > > > yesterday, I spent several hours trying to figure out how to capture > > > > > the HEADSET_PLUG intent with a BroadcastReceiver. The manifest defines > > > > > the <receiver>, with its intent-filter set to <action > > > > > android:name="android.intent.action.HEADSET_PLUG" />. > > > > > > The receiver is working correctly (I verified that by changing the > > > > > intent filter to capture things like SMS_RECEIVED including the proper > > > > > permissions and the receiver fired as expected on receiving an SMS), > > > > > but when listening for HEADSET_PLUG, it never fires. Debugging the > > > > > whole thing is a bit complicated, as the emulator doesn't seem to > > > > > support headsets and I have to disconnect my target device (a HTC > > > > > Dream) from USB in order to attach aheadset(so there's no logfile > > > > > viewing). Are there any special permissions required for applications > > > > > to capture the HEADSET_PLUG intent (and if so, which? I've tried > > > > > several, to no avail)? > > > > > > Maybe the Dream doesn't fire such an intent at all (that'd be bad luck > > > > > for me), but I guess that it must be an issue related to application > > > > > permissions. The documentation says extremely little (well, actually: > > > > > nothing) about the single permissions needed to perform all the > > > > > actions described in the API docs, so this is absolutely something to > > > > > be improved. > > > > > > What I want to do: Upon detecting aheadset, I want to re-route the > > > > > ringtones from the speakers to theheadset, so as not to annoy people > > > > > in my vicinity by a harsh ringing sound (I'm used to this behaviour > > > > > from my old phone) - by default, the speaker just keeps on ringing, no > > > > > matter whether aheadsetis attached or not. This part is already > > > > > working, but now I need to know how to detectheadsetattachment/ > > > > > detachment events. > > > > > > Thanks in advance, > > > > > > Henning- Hide quoted text - > > - Show quoted text - --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Android Developers" group. To post to this group, send email to android-developers@googlegroups.com To unsubscribe from this group, send email to android-developers-unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-developers?hl=en -~----------~----~----~----~------~----~------~--~---