我正在尝试制作一个聊天应用程序。我有一个SlidingDrawer活动,它有很多片段,其中ChatFragment是一个片段。现在当我向朋友发送消息时,我有一个Chatting Window,如果有消息来自GCM服务,我在ChatFragment中,然后此消息将转到该片段并更新列表视图,因为我想在聊天时收到任何消息时更新chatWindow现在,我尝试执行以下操作。
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()中启动广播接收器。

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类,如果有任何消息成功通过onReceive()方法接收,现在我想将此消息发送到Fragment。我尝试了一下,我向Fragment注册了Fragment并尝试获取相同的数据在onReceive()ChatFragment处。但是它没有调用。我试图通过登录查看。

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()?

最佳答案

注册广播您正在使用:

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


因此,您将仅在ChatBroadCastReceiver类上接收广播。因为mReceiver是ChatBroadCastReceiver类的实例。

现在要在ChatFragment中创建的匿名类上接收广播消息,您需要像下面的代码一样进行注册。其中mReceiver是您在ChatFragment类中实现的匿名BroadcastReceiver类的实例。

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"))


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

09-26 11:43