Android Open Source - AndroidSIP Incoming Call Receiver






From Project

Back to project page AndroidSIP.

License

The source code is released under:

Apache License

If you think the Android project AndroidSIP listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package net.gongmingqm10.sipsample.receiver;
//  w  w w  .  j  a  v a  2  s  .  co m
import android.app.AlertDialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.sip.SipAudioCall;
import android.net.sip.SipException;
import android.net.sip.SipProfile;

import net.gongmingqm10.sipsample.activity.MainActivity;

public class IncomingCallReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {

        final MainActivity mainActivity = (MainActivity) context;
        SipAudioCall incomingCall = null;

        SipAudioCall.Listener listener = new SipAudioCall.Listener(){
            @Override
            public void onRinging(SipAudioCall call, SipProfile caller) {
                try {
                    call.answerCall(30);
                } catch (SipException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onCalling(SipAudioCall call) {
                super.onCalling(call);
                mainActivity.updateStatus(MainActivity.STATE_CALLING + " " + call.getPeerProfile().getUserName());
            }

            @Override
            public void onCallEnded(SipAudioCall call) {
                super.onCallEnded(call);
                mainActivity.updateStatus(MainActivity.STATE_CONNECTED);
            }
        };

        try {
            incomingCall = mainActivity.sipManager.takeAudioCall(intent, listener);
        } catch (SipException e) {
            e.printStackTrace();
            return;
        }
        final SipAudioCall call = incomingCall;
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        builder.setTitle("New Call")
                .setMessage("From " + incomingCall.getPeerProfile().getUserName())
                .setPositiveButton("Accept", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        try {
                            call.answerCall(30);
                            call.startAudio();
                            call.setSpeakerMode(true);
                            if (call.isMuted()) call.toggleMute();
                        } catch (SipException e) {
                            e.printStackTrace();
                        }
                    }
                })
                .setNegativeButton("Deny", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        try {
                            call.endCall();
                            call.close();
                        } catch (SipException e) {
                            e.printStackTrace();
                        }
                    }
                });
        builder.create().show();
    }


}




Java Source Code List

net.gongmingqm10.sipsample.ApplicationTest.java
net.gongmingqm10.sipsample.SipApplication.java
net.gongmingqm10.sipsample.activity.BaseActivity.java
net.gongmingqm10.sipsample.activity.LoginActivity.java
net.gongmingqm10.sipsample.activity.MainActivity.java
net.gongmingqm10.sipsample.activity.ProfileActivity.java
net.gongmingqm10.sipsample.data.Account.java
net.gongmingqm10.sipsample.receiver.IncomingCallReceiver.java