简体   繁体   中英

How to start a audio song when the Broadcast Receiver gets the state of offhook and stop it when the dial closes

How to start a audio song when the Broadcast Receiver gets to call a number or the state of OFFHOOK state and stop it when IDLE or the call ends.

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.telephony.TelephonyManager;
import android.widget.Toast;
import android.media.MediaPlayer;

public class Ringing extends BroadcastReceiver{
    Context context;
    public static MediaPlayer ob=null;
    @Override
    public void onReceive(Context context, Intent intent){
        String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);

        if(state.equals(TelephonyManager.EXTRA_STATE_OFFHOOK)){
           // Toast.makeText(context, "Call Recieved", Toast.LENGTH_LONG).show();
            ob = MediaPlayer.create(context,R.raw.trouble);
            ob.start();
        }
        if(state.equals(TelephonyManager.EXTRA_STATE_IDLE)){
           if(ob.isPlaying()) {
               ob.stop();
                ob.destroy();
           }
        }
    }
}

Declare class variables as shown below in the broadcastreceiver code:

public class PhonecallReceiver extends BroadcastReceiver {
    static String phoneNumber;
    private static boolean ringing, received;
    .......................

Next in the onReceive method put the below code:

When a phone number is received the states are: IDLE>RINGING>OFFHOOK>IDLE

When a phone number is not received the states are: IDLE>RINGING>IDLE

When state is RINGING set the ringing variable as true, then when the state is OFFHOOK check if variable ringing is true then the call is picked and start the audio and also set ringing as false and received as true. Next when the state is IDLE check if variable received is true, then call that was picked was disconnected, now start the music.

@Override
    public void onReceive(Context context, Intent intent) {
        Bundle bundle = intent.getExtras();
        String state = bundle.getString(TelephonyManager.EXTRA_STATE);
        String temp = bundle.getString(TelephonyManager.EXTRA_INCOMING_NUMBER);
        if (!(state == null) && state.equals(TelephonyManager.EXTRA_STATE_RINGING)) {
            ringing = true;
            received = false;
        } else if (!(state == null) && state.equals(TelephonyManager.EXTRA_STATE_OFFHOOK) && ringing) {
            received = true;
            ringing = false;
            //add code to start music
        } else if (!(state == null) && state.equals(TelephonyManager.EXTRA_STATE_IDLE) && received) {
            //add code to stop music
            received = false;
            ringing = false;
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM