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();
}
}
}
}