Event listening example in Android

Source: Internet
Author: User

Monitor incoming call status
Audiofocus is a feature available only after android2.2. For versions earlier than Android 2.2, another method is to listen to the phone status. At least when you call in, you can pause the music.
The first step to implement this function is to declare the receiver used to receive the phone_state notification in androidmanifest. xml.
<Cycler Android: Name = ". phonestatereceiver">
<Intent-filter>
<Action Android: Name = "android. Intent. Action. phone_state"/>
</Intent-filter>
</Cycler>
Step 2: Define a corresponding phonestatereceiver, Code As follows:
Package lyricplayer. xwg;

Import Android. content. broadcastreceiver;
Import Android. content. context;
Import Android. content. intent;
Import Android. telephony. telephonymanager;

Public class phonestatereceiver extends broadcastreceiver {
@ Override
Public void onreceive (context, intent ){
// If Android. OS. Build. version. sdk_int> = 8 we use audio focus.
If (Android. OS. Build. version. sdk_int <8 ){
Telephonymanager TM = (telephonymanager) Context. getsystemservice (context. telephony_service );
If (TM. getcallstate ()! = Telephonymanager. call_state_idle ){
Context. startservice (new intent (mediaplayerservice. action_pause ));
}
}
}
}
This is enough.
Monitor earphone plug-out
If you unplug the headset during the music playing process, the music will be played out through the speaker. To avoid this embarrassing situation, we will monitor the headset pulling status and pause playing when the headset is pulled out.
First, declare the receiver used to receive audio_becoming_noisy notifications in androidmanifest. xml.
<Cycler Android: Name = ". musicintentreceiver">
<Intent-filter>
<Action Android: Name = "android. Media. audio_becoming_noisy"/>
</Intent-filter>
</Cycler>
Then define the handler used to process notifications. The class name must be the same as that declared in androidmanifest. xml.
Package lyricplayer. xwg;

Import Android. content. broadcastreceiver;
Import Android. content. context;
Import Android. content. intent;

Public class musicintentreceiver extends broadcastreceiver {
@ Override
Public void onreceive (context CTX, intent ){
If (intent. getaction (). Equals (Android. Media. audiomanager. action_audio_becoming_noisy )){
CTX. startservice (new intent (lyricplayerservice. action_pause ));
}
}
}
Media_button Processing
Before discussing the processing method, you must make it clear that the keys belong to media_button? According to my test, media_button seems to be the last button on the line control. The same method can be used on the Internet to obtain the content of the volume key action, but I didn't try it out.
To continue with our topic, we need some preparation to detect media_button.
First, declare the receiver used to receive media_button notifications in androidmanifest. xml.
<Cycler Android: Name = "mediabuttonreceiver">
<Intent-filter>
<Action Android: Name = "android. Intent. Action. media_button"/>
</Intent-filter>
</Cycler>
Of course, you need to define a real handler. The name must be the same as that in androidmanifest. xml.
Package lyricplayer. xwg;

Import Android. content. broadcastreceiver;
Import Android. content. context;
Import Android. content. intent;
Import Android. util. log;
Import Android. View. keyevent;

Public class mediabuttonreceiver extends broadcastreceiver {
Private Static final string tag = new string ("lyricvolumekeyreceiver ");
@ Override
Public void onreceive (context, intent ){
// Musicplaybackservice service = (musicplaybackservice) context;
If (intent. action_media_button.equals (intent. getaction ())){
Keyevent key = (keyevent) intent. getparcelableextra (intent. extra_key_event );
If (key. getaction () = keyevent. action_down ){
Log. I (TAG, "onreceive, getkeycode =" + key. getkeycode ());
Switch (key. getkeycode ()){
Case keyevent. keycode_headsethook:
Context. startservice (new intent (mediaplayerservice. action_play_pause ));
Break;
Case keyevent. keycode_media_previous:
Context. startservice (new intent (mediaplayerservice. action_previous ));
Break;
Case keyevent. keycode_media_next:
Context. startservice (new intent (mediaplayerservice. action_next ));
Break;
}
}
}
}
}
In particular, the key value of the intermediate key is not keycode_play_pause, but keycode_headsethook. Think too. You can also use this key to answer the phone.
The last step of preparation is to report media_button accepted by mediabuttonreceiver to audiomenager. This is a feature available in android2.2 and later versions and requires version determination.
If (Android. OS. Build. version. sdk_int> = 8 ){
Mreceivername = new componentname (getpackagename (), mediabuttonreceiver. Class. getname ());
Maudiomanager = (audiomanager) getsystemservice (context. audio_service );
Maudiomanager. registermediabuttoneventreceiver (mreceivername );
}
Of course, at the end, we will also maintain the good habit of canceling logon.
If (maudiomanager! = NULL & mreceivername! = NULL ){
Maudiomanager. unregistermediabuttoneventreceiver (mreceivername );
}
Notification
Notification indicates that icationicationmanager is obtained first.
Micationicationmanager = (notificationmanager) getsystemservice (notification_service );
Call the shownotification () method when you need to represent it. Code related to the shownotification () method:
Public interface icationicationprovider {
Public notification createnotification (context );
}

Icationicationprovider mnotificationprovider = NULL;

Public void setnotificationprovider (notificationprovider provider ){
Mnotificationprovider = provider;
}

/*** Show a notification while this service is running .*/
Private void shownotification (){
If (mnotificationprovider! = NULL ){
// Send the notification.
Micationicationmanager. Notify (notification, mnotificationprovider. createnotification (this ));
}
}
The method has been used n times. No need to explain it. Of course, it is necessary to look at the implementation-side practices.
Mproxy. setnotificationprovider (New mediaplayerservice. icationicationprovider (){
@ Override
Public notification createnotification (context ){
Notification = new notification (R. drawable. button_blue_play, mproxy. gettitle (), system. currenttimemillis ());
// The pendingintent to launch our activity if the user selects this notification
Pendingintent contentintent = pendingintent. getactivity (context, 0, new intent (context, lyricmain. Class), 0 );
// Set the info for the views that show in the notification panel.
Notification. setlatesteventinfo (context, gettext (R. String. media_player_label), mproxy. gettitle (), contentintent );
Return notification;
}
});


The full text is excerpted from:Http://www.2cto.com/kf/201109/103784.html

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.