1

I am building a music player and i am able to play music in background through music service. But the problem arises when i receive a call service is still running in background. I want to hold my music service during call.

reference

Stopping & Starting music on incoming calls

I already try it by creating a receiver but got error

PhoneReciever.java

public class PhoneReceiver extends BroadcastReceiver{

TelephonyManager telManager;
Context context;

@Override
public void onReceive(Context context, Intent intent) {
    // TODO Auto-generated method stub
    this.context=context;

    telManager = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
    telManager.listen(phoneListener, PhoneStateListener.LISTEN_CALL_STATE);

}

private final PhoneStateListener phoneListener = new PhoneStateListener(){

    @Override
    public void onCallStateChanged(int state, String incomingNumber) {
        // TODO Auto-generated method stub
        try {
            switch (state) {
            case TelephonyManager.CALL_STATE_RINGING: {
                //PAUSE

                break;
            }
            case TelephonyManager.CALL_STATE_OFFHOOK: {

                break;
            }
            case TelephonyManager.CALL_STATE_IDLE: {
                //PLAY

                break;
            }
            default: { }
            }
        } 
        catch (Exception ex) {

        }
    }
};
}

added in Manifest.xml in

 <receiver android:name=".PhoneReceiver" >
    <intent-filter>
        <action android:name="android.intent.action.PHONE_STATE" >
        </action>
    </intent-filter>
</receiver>

Logcat:

enter image description here please help if you noticed any mistake or any other way to do it.

Community
  • 1
  • 1
Mayur Kharche
  • 717
  • 1
  • 8
  • 27

0 Answers0