Android Fragment - BroadcastReceiver调用片段方法

时间:2016-03-14 20:01:58

标签: android android-fragments broadcastreceiver android-broadcastreceiver

我有一个BroadcastReceiver接收发送给Fragment的广播。 我正在接收广播,但我如何从Fragment本身调用一个方法? 一旦广播到达,我基本上需要更新列表,List& update方法是Fragment的一部分。

public class FragmentReceiver extends BroadcastReceiver
{
    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        if(action != null && action.equals("AllTasksFragmentUpdate"))
        {
            //
        }
    }
}

注册接收方:

    @Override
public void onCreate(final Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    getActivity().registerReceiver(new FragmentReceiver(), new IntentFilter("AllTasksFragmentUpdate"));
}

如何从片段中调用方法?

4 个答案:

答案 0 :(得分:11)

您可以通过以下方式实施广播接收器:

import android.app.Fragment;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.content.LocalBroadcastManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.adobe.libs.connectors.R;

public class YourFragment extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater,
                                @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);
        //Start listening for refresh local file list   LocalBroadcastManager.getInstance(getActivity()).registerReceiver(mYourBroadcastReceiver,
                new IntentFilter(<YOUR INTENT FILTER>));

        return inflater.inflate(R.layout.your_fragment_layout, null, true);
    }

    @Override
    public void onDestroyView() {
        LocalBroadcastManager.getInstance(getActivity()).unregisterReceiver(mYourBroadcastReceiver);
    }

    private final BroadcastReceiver mYourBroadcastReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            // Now you can call all your fragments method here
        }
    };
}

答案 1 :(得分:2)

在onReceive()方法中,您可以通过其标记名称(用于实例化片段的名称)找到您的片段,并调用其公共方法。

<FeatureSet> ... </FeatureSet>

希望这有帮助!

答案 2 :(得分:0)

假设您有一个名为MyActivity的Activity,并且在代码中的某个位置,您希望通过名为MyFragment的Fragment接收广播。你可以这样做:

在MyActivity中,使用LocalBroadcastManager通过过滤器进行广播:

String filter = "thisIsForMyFragment";
Intent intent = new Intent(filter); //If you need extra, add: intent.putExtra("extra","something");
LocalBroadcastManager.getInstance(this).sendBroadcast(intent);

在MyFragment中,在onCreateView()内添加此项以注册接收器:

LocalBroadcastManager.getInstance(getActivity()).registerReceiver(myBroadcastReceiver,
        new IntentFilter("thisIsForMyFragment"));

以下,在MyFragment中(在onCreateView()之外)创建接收方法以接收广播:

private final BroadcastReceiver myBroadcastReceiver = new BroadcastReceiver()
{
    @Override
    public void onReceive(Context context, android.content.Intent intent) {
        Toast.makeText(getActivity(), "Broadcast received!", Toast.LENGTH_SHORT).show();//Do what you want when the broadcast is received...

    }
};

最后,在MyFragment中添加此项以取消注册接收器onDestroyView:

@Override
public void onDestroyView()
{
    super.onDestroyView();
    LocalBroadcastManager.getInstance(getActivity()).unregisterReceiver(myBroadcastReceiver);
}

答案 3 :(得分:0)

activity!!.registerReceiver(receiver, IntentFilter(DataKeys.STATUS))