Задача сделать чат
Соединение с сервером происходит в сервисе
При реализации возник ряд вопросов
1. Значит отправка из сервиса в активности происходит
Код: Выделить всё
@Override
public void onMessage(String message) {
Log.d("chat", String.format("WebSocket Message %s", message));
sendBroadcast(BroadCastIntent.putExtra("message", message));
}
2. Как оправлять данные в сервис
Код: Выделить всё
getActivity().startService(new Intent(getActivity(), WebSocketService.class).putExtra("message", json.toString()));
3. Есть MainActivity и есть фрагменты
Как передавать данные в фрагменты?
У меня в каждом фрагменте такая канструкция
Код: Выделить всё
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
Log.d("chat", "FindFragment onCreateView");
View vView = inflater.inflate(R.layout.fragment_find, container, false);
TextView text = (TextView) vView.findViewById(R.id.tvText);
BroadCastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
text.setText(intent.getStringExtra("message"));
}
};
return vView;
}
4. У меня есть сообщения которые должны передаваться в MainActivity при запуске фрагментов
Нужно(обязательно) ли останавливать бродкаст?
Код: Выделить всё
@Override
public void onPause() {
super.onPause();
unregisterReceiver(BroadCastReceiver);
}