2

I'm building my own VoIP app. Originally use Activity and BroadcastReceiver and it works fine, now I want to convert them to Fragment for my drawer.

Because BroadcastReceiver cannot use by Fragment, so I find some methods on stackoverflow but still not work...

All the code below is in the same file

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        V = inflater.inflate(R.layout.dial, container, false);
        filter = new IntentFilter();
        filter.addAction("android.SipDemo.INCOMING_CALL");
        LocalBroadcastManager.getInstance(getActivity()).registerReceiver(callReceiver, filter);
        getActivity().getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
        initializeManager();

        // Inflate the layout for this fragment
        return V;
    }

private final BroadcastReceiver callReceiver = new BroadcastReceiver()
{
    @Override
    public void onReceive(Context context, Intent intent)
    {
        SipAudioCall incomingCall = null;
        try {

            SipAudioCall.Listener listener = new SipAudioCall.Listener() {
                @Override
                public void onRinging(SipAudioCall call, SipProfile caller) {
                    try {
                        call.answerCall(30);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
                public void onCallEnded(SipAudioCall call) {
                    endMessage();
                }
            };
            incomingCall = manager.takeAudioCall(intent, listener);
            updateStatus("call incoming");
            call = incomingCall;
            call.answerCall(30);
            call.startAudio();
            call.setSpeakerMode(isSpeakerClicked);
        } catch (Exception e) {
            if (incomingCall != null) {
                incomingCall.close();
            }
        }
    }
};
Rahul Singh Chandrabhan
  • 2,531
  • 5
  • 22
  • 33
Jack Kuo
  • 43
  • 5
  • i hope it works. refer to https://stackoverflow.com/questions/16616654/registering-and-unregistering-broadcastreceiver-in-a-fragment – pistolcaffe Jun 19 '18 at 03:21
  • 1
    BroadcastReceiver can be implemented in Fragments too. – Reaz Murshed Jun 19 '18 at 04:03
  • Thanks, finally, I give up using BoradcastReceiver in Fragment. I register BroadcastReceiver in that Fragment's parent Activity then use findFragmentByTag() in Activity's onResume to call the method in Fragment. So BroadcastReceiver is still an Activity. – Jack Kuo Jun 19 '18 at 06:21

1 Answers1

0

In your Fragment :

Call registerBroadcast() in your onCreateView

private void registerBroadcast() {
        LocalBroadcastManager.getInstance(getActivity()).registerReceiver(addCallReceiver,
                new IntentFilter(getString(R.string.broadcast_add_call)));
    }

BroadcastReceiver addCallReceiver= new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        // perform actions
    }
};

From where you want to trigger receiver :

LocalBroadcastManager.getInstance(mContext).sendBroadcast(new Intent(getString(R.string.broadcast_add_call)));
Deep Patel
  • 2,584
  • 2
  • 15
  • 28
  • Thanks. I'm using android native SIP so I don't know where to apply sendBroadcast, it has been done by native service. Now I'm using another method which commented upon. And I found a problem in Fragment BroadcastReceiver that is I cannot keep it running in the background since I can't write something like Service or Receiver in Manifests.xml. – Jack Kuo Jun 19 '18 at 06:30