Android changes in the phone status will send the action to Android.intent.action.PHONE_STATE broadcast,
When you make a call, a broadcast with action Android.intent.action.NEW_OUTGOING_CALL is sent
Android in the music player to monitor the status of the phone, such as: playing music, call (power off) pause the music, hang up the phone continues to play music.
First, the telephone activity listens to the broadcast
public class Phonereceiver extends Broadcastreceiver {
Private Context Mcontext;
Private String maction;
Private Intent mintent;
Public Phonereceiver () {}
Public Phonereceiver (context context) {
Super ();
Mcontext = context;
}
@Override
public void onreceive (context context, Intent Intent) {
Maction = intent.getaction ( );
if (maction.equals (Intent.action_new_outgoing_call)) {
//power-off
Mintent = NEW Intent ();
Mintent.setaction (Broadcastflag.music_stop);
Mcontext.sendbroadcast (mintent);
}else{
//Call
Telephonymanager Manager = (Telephonymanager) context.getsystemservice (Service.telep Hony_service);
Manager.listen (Statelistener, phonestatelistener.listen_call_state);
}
}
Phonestatelistener Statelistener = new Phonestatelistener () {
public void oncallstatechanged (int state, String incomingnumber) {
Super.oncallstatechanged (state, Incomingnumber);
Switch (state) {
Case Telephonymanager.call_state_idle:
Hanging off
Mintent = new Intent ();
Mintent.setaction (Broadcastflag.music_start);
Mcontext.sendbroadcast (mintent);
Break
Case Telephonymanager.call_state_offhook:
Answer
Case telephonymanager.call_state_ringing:
Bell
Mintent = new Intent ();
Mintent.setaction (Broadcastflag.music_stop);
Mcontext.sendbroadcast (mintent);
Break
}
};
};
}
Second, register the broadcast in the Music Broadcasting Service (MUSICPLAYSERVICE)
Third, add permissions in the Androidmanifest.xml file
<uses-permission android:name= "Android.permission.READ_PHONE_STATE" ></uses-permission>
<uses-permission android:name= "Android.permission.PROCESS_OUTGOING_CALLS" ></uses-permission>
Ok!
Andriod Phone Status Monitor-call to power