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) {
/* HEADSET IS OR HAS BEEN DISCONNECTED */
}else{
/* HEADSET IS 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 <[email protected]> wrote:
> hi henning
>
> do you have some example code for me, how to create a service to
> detect the headset, please?
>
> Cheers
>
> On 16 Feb., 17:21, zero <[email protected]> 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 <[email protected]>
> > 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 <[email protected]>
> > > 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 a headset (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 a headset, I want to re-route the
> > > > ringtones from the speakers to the headset, 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 a headset is attached or not. This part is already
> > > > working, but now I need to know how to detect headset attachment/
> > > > detachment events.
>
> > > > Thanks in advance,
>
> > > > Henning
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
-~----------~----~----~----~------~----~------~--~---