Andriod Phone Status Monitor-call to power

Source: Internet
Author: User

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

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.