android 如何将数据从 BroadcastReceiver 发送到 Fragment

How to send data from BroadcastReceiver to Fragment in android

我正在尝试让聊天 app.I 有一个 SlidingDrawer Activity 并且它有很多片段,其中 ChatFragment 是 one.Now 当我正在向我的朋友发送消息,我有一个 Chatting Window,如果任何消息来自 GCM 服务并且我在 ChatFragment,那么这条消息将转到该片段并更新列表视图,因为我想要在聊天时收到任何消息时更新聊天窗口现在我尝试像下面那样做。

GcmIntentService :

public class GcmIntentService extends IntentService {

    public GcmIntentService() {
        super("GcmIntentService");
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        Bundle extras = intent.getExtras();
        GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this);
        // The getMessageType() intent parameter must be the intent you received
        // in your BroadcastReceiver.
        String messageType = gcm.getMessageType(intent);

        if (extras != null && !extras.isEmpty()) {  // has effect of unparcelling Bundle
            // Since we're not using two way messaging, this is all we really to check for
            if (GoogleCloudMessaging.MESSAGE_TYPE_MESSAGE.equals(messageType)) {
                Logger.getLogger("GCM_RECEIVED").log(Level.INFO, extras.toString());

                showToast(extras.getString("message"));
                Intent sendData = new Intent("chatupdater");
                sendData.putExtra("msg", extras.getString("message"));
                LocalBroadcastManager.getInstance(this).sendBroadcast(sendData);
                Log.i("chat","i am in GCMIntentService");


            }
        }
        GcmBroadcastReceiver.completeWakefulIntent(intent);
    }

这里我在 onHandleIntent() 中启动一个 broadcastreceiver。

ChatBroadCastReceiver:

public class ChatBroadCastReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        Log.i("chat","I am in ChatBroadCastReceiver");
        String msg = intent.getStringExtra("msg");
        Log.i("chat",msg);



        //Intent data = new Intent("chatupdater");
        //Intent data = new Intent("chatupdater");
        //data.putExtra("key","data");
        //data.putInt("fragmentno",1); // Pass the unique id of fragment we talked abt earlier
        //context.sendBroadcast(intent);


    }
}

这是我的 ChatBroadCastReceiver class,如果有任何消息成功收到 onReceive() method.Now 我想将此消息发送到 Fragment.What 我试过了,我用它注册了片段,并试图在 ChatFragmentonReceive() 处获取相同的数据。但它没有 call.I 试图通过日志记录来查看。

public class ChatFragment extends Fragment {

    //ChatBroadCastReceiver mReceiver;
    private EditText et_chat;
    Bundle mailData;
    String caller_mail;
    private ListView chatListview;
    private ChatWindowAdapter chatWindowAdapter;
    private List<ChatSession>PreviousChatSession;
    private List<ChatInfo> chatListItems;
    Button chat_send;
    public ChatFragment() {

    }

    public BroadcastReceiver mReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            Log.i("chat","I am in BroadCastReceiver");
            String msg = intent.getStringExtra("msg");
            Toast.makeText(getActivity(),msg,Toast.LENGTH_LONG).show();
        }
    };

    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        mReceiver = new ChatBroadCastReceiver();
        //getActivity().registerReceiver(new ChatBroadCastReceiver(),new IntentFilter("chatupdater"));
        LocalBroadcastManager.getInstance(getActivity()).registerReceiver(new ChatBroadCastReceiver(),new IntentFilter("chatupdater"));
        Log.i("chat", "I am in onCreate");

    }

现在我如何才能将我在 Broadcastreceiver 中收到的消息发送到 ChatFragment 的 onReceive()?

试试这个

  • 创建一个接口,该接口将具有这样的方法。

    public interface DemoListener { public void receiveMessage(String message); }

现在在您的片段中实现它并在 BroadcastReceiver 中注册侦听器,现在一旦您的广播接收器收到任何消息,您的片段就可以通过此侦听器使用它。
希望如此有帮助。

要注册您的广播您正在使用:

mReceiver = new ChatBroadCastReceiver();
LocalBroadcastManager.getInstance(getActivity()).registerReceiver(mReceiver ,new IntentFilter("chatupdater"));

所以您只会在 ChatBroadCastReceiver class 收到广播。因为 mReceiver 是 ChatBroadCastReceiver class.

的实例

现在要接收您在 ChatFragment 中创建的关于匿名 class 的广播消息,您需要像下面的代码一样注册。其中 mReceiver 是您在 ChatFragment class.

中实现的匿名 BroadcastReceiver class 的实例
LocalBroadcastManager.getInstance(getActivity()).registerReceiver(mReceiver ,new IntentFilter("chatupdater"))

要在 ChatFragment 和 ChatBroadCastReceiver 上接收广播消息,您需要注册广播接收器两次。

LocalBroadcastManager.getInstance(getActivity()).registerReceiver(mReceiver ,new IntentFilter("chatupdater"))

ChatBroadCastReceiver mChatBroadCastReceiver = new ChatBroadCastReceiver();
LocalBroadcastManager.getInstance(getActivity()).registerReceiver(mChatBroadCastReceiver ,new IntentFilter("chatupdater"))

希望这个回答能帮助您理解您的问题。