Android Music Player Monitor phone status implementation code _android

Source: Internet
Author: User
Tags stub

The following code is the state of listening to the phone, the code simply do not introduce direct look at the code:

Copy Code code as follows:

Private Boolean mresumeaftercall = false;
Private Phonestatelistener Mphonestatelistener = new Phonestatelistener () {
@Override
public void oncallstatechanged (int state, String incomingnumber) {
if (state = = telephonymanager.call_state_ringing) {
Audiomanager Audiomanager = (audiomanager) getsystemservice (Context.audio_service);
int ringvolume = Audiomanager
. Getstreamvolume (audiomanager.stream_ring);
if (Ringvolume > 0) {
Mresumeaftercall = (player.isplaying () | | mresumeaftercall);
try {
Stub.pause ();
catch (RemoteException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
}
else if (state = = Telephonymanager.call_state_offhook) {
Pause the music while a conversation was in progress
Mresumeaftercall = (player.isplaying () | | mresumeaftercall);
try {
Stub.pause ();
catch (RemoteException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
else if (state = = Telephonymanager.call_state_idle) {
Start playing again
if (Mresumeaftercall) {
Resume playback only if music is playing
When the call was answered
try {
Stub.resume ();
catch (RemoteException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
Mresumeaftercall = false;
}
}
}
};

When it is used, it is added in the oncreate of the service, as follows:
Copy Code code as follows:

public void OnCreate () {
Player = new MediaPlayer ();
Super.oncreate ();
Telephonymanager tmgr = (telephonymanager) getsystemservice (Context.telephony_service);
Tmgr.listen (Mphonestatelistener, phonestatelistener.listen_call_state);
Intentfilter filter = new Intentfilter ();

Filter.addaction (Servicecmd);
Filter.addaction (pause_action);
Receiver = new Musicservicereceiver ();
Registerreceiver (receiver, filter);
}

You can delete it in the OnDestroy.
Copy Code code as follows:

public void OnDestroy () {
LOG.E (TAG, "OnDestroy" +player.isplaying ());
System.out.println ("OnDestroy");
Unregisterreceiver (receiver);
Player.release ();
Player = null;

Telephonymanager tmgr = (telephonymanager) getsystemservice (Context.telephony_service);
Tmgr.listen (Mphonestatelistener, 0);
Super.ondestroy ();
}

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.