从android中的服务调用BroadCastReceiver以更新片段中的UI?

从android中的服务调用BroadCastReceiver以更新片段中的UI?,android,android-fragments,broadcastreceiver,android-service,google-cloud-messaging,Android,Android Fragments,Broadcastreceiver,Android Service,Google Cloud Messaging,我想从服务更新片段的UI。我正在使用GCM发送消息。我有这个类用于GCM: public class GcmIntentService extends IntentService { public GcmIntentService() { super("GcmIntentService"); } @Override protected void onHandleIntent(Intent inte

我想从
服务
更新
片段
UI
。我正在使用
GCM
发送消息。我有这个类用于
GCM

    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"));
                    sendBroadcast(sendData);


                }
            }
   GcmBroadcastReceiver.completeWakefulIntent(intent);
        }
onHandleIntent()中
方法我将消息广播到一个片段,该片段注册到
broadcastedreceiver
。我有一个
SlidingDrawerActivity
,它有许多片段,
ChatFragment
就是其中之一。我想将此消息从
gcminentservice
发送到
ChatFragmen
t.

我的聊天片段类:

public class ChatFragment extends Fragment {

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

    }

    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        getActivity().registerReceiver(new ChatBroadCastReceiver(),new IntentFilter("chatupdater"));

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.chat_window, null, false);
        et_chat = (EditText) v.findViewById(R.id.et_chat);
        chat_send = (Button) v.findViewById(R.id.bt_chat_send);
        chatListview = (ListView)v.findViewById(R.id.chat_listView);
        chatListItems = new ArrayList<ChatInfo>();
        chatWindowAdapter = new ChatWindowAdapter(getActivity(),chatListItems);
        chatListview.setAdapter(chatWindowAdapter);
        mailData=getArguments();
        caller_mail = mailData.getString(PropertyNames.Userinfo_Mail.getProperty());

        retrieveChats();

        chat_send.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ChatInfo chatInfo= new ChatInfo();
                chatInfo.setChat_text(et_chat.getText().toString());
                chatInfo.setDirection("right");
                chatListItems.add(chatInfo);
                chatWindowAdapter.notifyDataSetChanged();

                fetchID(caller_mail);
            }
        });

        return v;
    }

    private final BroadcastReceiver receiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
             String msg = intent.getStringExtra("msg");
             Toast.makeText(getActivity(),msg,Toast.LENGTH_LONG).show();
        }
    };
public class ChatBroadCastReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {

    }
}  
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        getActivity().registerReceiver(this.broadCastNewMessage, new IntentFilter("bcNewMessage"));
        ...
    }
BroadcastReceiver broadCastNewMessage = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
         // here you can update your db with new messages and update the ui (chat message list)
         msgAdapter.notifyDataSetChanged();
         mListView.requestLayout();
         ...
    }
}
@Override
    public void onDestroy() {
        super.onDestroy();
        getActivity().unregisterReceiver(broadCastNewMessage);
    }
我在
sendBroadcast(sendData)
中有意发送消息,并在
chatFragment
中接收消息,在
onReceive()
方法中,我希望在Toast中查看消息。但是Toast没有显示。
我是否为片段内部服务正确实现了BroadcastReceiver??

编辑:我已经调试过,发现下面的部分没有调用

private final BroadcastReceiver receiver = 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();
    }
};

我是否必须在menifest中包含关于接收器的任何内容

在片段类中使用以下片段:

1。注册广播:

public class ChatFragment extends Fragment {

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

    }

    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        getActivity().registerReceiver(new ChatBroadCastReceiver(),new IntentFilter("chatupdater"));

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.chat_window, null, false);
        et_chat = (EditText) v.findViewById(R.id.et_chat);
        chat_send = (Button) v.findViewById(R.id.bt_chat_send);
        chatListview = (ListView)v.findViewById(R.id.chat_listView);
        chatListItems = new ArrayList<ChatInfo>();
        chatWindowAdapter = new ChatWindowAdapter(getActivity(),chatListItems);
        chatListview.setAdapter(chatWindowAdapter);
        mailData=getArguments();
        caller_mail = mailData.getString(PropertyNames.Userinfo_Mail.getProperty());

        retrieveChats();

        chat_send.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ChatInfo chatInfo= new ChatInfo();
                chatInfo.setChat_text(et_chat.getText().toString());
                chatInfo.setDirection("right");
                chatListItems.add(chatInfo);
                chatWindowAdapter.notifyDataSetChanged();

                fetchID(caller_mail);
            }
        });

        return v;
    }

    private final BroadcastReceiver receiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
             String msg = intent.getStringExtra("msg");
             Toast.makeText(getActivity(),msg,Toast.LENGTH_LONG).show();
        }
    };
public class ChatBroadCastReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {

    }
}  
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        getActivity().registerReceiver(this.broadCastNewMessage, new IntentFilter("bcNewMessage"));
        ...
    }
BroadcastReceiver broadCastNewMessage = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
         // here you can update your db with new messages and update the ui (chat message list)
         msgAdapter.notifyDataSetChanged();
         mListView.requestLayout();
         ...
    }
}
@Override
    public void onDestroy() {
        super.onDestroy();
        getActivity().unregisterReceiver(broadCastNewMessage);
    }
2。宣布您的广播:

public class ChatFragment extends Fragment {

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

    }

    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        getActivity().registerReceiver(new ChatBroadCastReceiver(),new IntentFilter("chatupdater"));

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.chat_window, null, false);
        et_chat = (EditText) v.findViewById(R.id.et_chat);
        chat_send = (Button) v.findViewById(R.id.bt_chat_send);
        chatListview = (ListView)v.findViewById(R.id.chat_listView);
        chatListItems = new ArrayList<ChatInfo>();
        chatWindowAdapter = new ChatWindowAdapter(getActivity(),chatListItems);
        chatListview.setAdapter(chatWindowAdapter);
        mailData=getArguments();
        caller_mail = mailData.getString(PropertyNames.Userinfo_Mail.getProperty());

        retrieveChats();

        chat_send.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ChatInfo chatInfo= new ChatInfo();
                chatInfo.setChat_text(et_chat.getText().toString());
                chatInfo.setDirection("right");
                chatListItems.add(chatInfo);
                chatWindowAdapter.notifyDataSetChanged();

                fetchID(caller_mail);
            }
        });

        return v;
    }

    private final BroadcastReceiver receiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
             String msg = intent.getStringExtra("msg");
             Toast.makeText(getActivity(),msg,Toast.LENGTH_LONG).show();
        }
    };
public class ChatBroadCastReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {

    }
}  
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        getActivity().registerReceiver(this.broadCastNewMessage, new IntentFilter("bcNewMessage"));
        ...
    }
BroadcastReceiver broadCastNewMessage = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
         // here you can update your db with new messages and update the ui (chat message list)
         msgAdapter.notifyDataSetChanged();
         mListView.requestLayout();
         ...
    }
}
@Override
    public void onDestroy() {
        super.onDestroy();
        getActivity().unregisterReceiver(broadCastNewMessage);
    }
3。注销您的广播:

public class ChatFragment extends Fragment {

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

    }

    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        getActivity().registerReceiver(new ChatBroadCastReceiver(),new IntentFilter("chatupdater"));

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.chat_window, null, false);
        et_chat = (EditText) v.findViewById(R.id.et_chat);
        chat_send = (Button) v.findViewById(R.id.bt_chat_send);
        chatListview = (ListView)v.findViewById(R.id.chat_listView);
        chatListItems = new ArrayList<ChatInfo>();
        chatWindowAdapter = new ChatWindowAdapter(getActivity(),chatListItems);
        chatListview.setAdapter(chatWindowAdapter);
        mailData=getArguments();
        caller_mail = mailData.getString(PropertyNames.Userinfo_Mail.getProperty());

        retrieveChats();

        chat_send.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ChatInfo chatInfo= new ChatInfo();
                chatInfo.setChat_text(et_chat.getText().toString());
                chatInfo.setDirection("right");
                chatListItems.add(chatInfo);
                chatWindowAdapter.notifyDataSetChanged();

                fetchID(caller_mail);
            }
        });

        return v;
    }

    private final BroadcastReceiver receiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
             String msg = intent.getStringExtra("msg");
             Toast.makeText(getActivity(),msg,Toast.LENGTH_LONG).show();
        }
    };
public class ChatBroadCastReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {

    }
}  
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        getActivity().registerReceiver(this.broadCastNewMessage, new IntentFilter("bcNewMessage"));
        ...
    }
BroadcastReceiver broadCastNewMessage = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
         // here you can update your db with new messages and update the ui (chat message list)
         msgAdapter.notifyDataSetChanged();
         mListView.requestLayout();
         ...
    }
}
@Override
    public void onDestroy() {
        super.onDestroy();
        getActivity().unregisterReceiver(broadCastNewMessage);
    }
然后在服务中调用sendBroadcast()方法:

this.sendBroadcast(new Intent().setAction("bcNewMessage"));
享受吧