Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/cocoa/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android在一个片段中广播_Android_Broadcastreceiver_Fragment_Android Broadcast - Fatal编程技术网

Android在一个片段中广播

Android在一个片段中广播,android,broadcastreceiver,fragment,android-broadcast,Android,Broadcastreceiver,Fragment,Android Broadcast,我正在构建自己的VoIP应用程序。最初使用Activity和BroadcastReceiver,它工作得很好,现在我想将它们转换为我抽屉中的片段 因为BroadcastReceiver不能按片段使用,所以我在stackoverflow上找到了一些方法,但仍然不起作用 下面的所有代码都在同一个文件中 @Override public View onCreateView(LayoutInflater inflater, ViewGroup container,

我正在构建自己的VoIP应用程序。最初使用Activity和BroadcastReceiver,它工作得很好,现在我想将它们转换为我抽屉中的片段

因为BroadcastReceiver不能按片段使用,所以我在stackoverflow上找到了一些方法,但仍然不起作用

下面的所有代码都在同一个文件中

@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();
            }
        }
    }
};
在你的片段中:

在您的
onCreate视图中调用
registerBroadcast()

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
    }
};
从要触发接收器的位置:


我希望它能起作用。参考BroadcastReceiver也可以在片段中实现。谢谢,最后,我放弃在片段中使用BoradcastReceiver。我在片段的父活动中注册BroadcastReceiver,然后在活动的onResume中使用findFragmentByTag()调用片段中的方法。所以广播接收器仍然是一项活动。谢谢。我正在使用android本机SIP,所以我不知道在哪里应用sendBroadcast,它是由本机服务完成的。现在我使用的是另一种方法。我在Fragment BroadcastReceiver中发现了一个问题,那就是我不能让它在后台运行,因为我不能在Manifests.xml中编写类似于Service或Receiver的东西。
LocalBroadcastManager.getInstance(mContext).sendBroadcast(new Intent(getString(R.string.broadcast_add_call)));