Вызов BroadCastReceiver из службы в Android для обновления пользовательского интерфейса во фрагменте?
Я хочу обновитьUI
изfragment
отservice
.Я использую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()
Метод, который я передал сообщение фрагменту, который зарегистрирован вBroadCastReceiver
.У меня есть такойSlidingDrawerActivity
и у него есть много фрагментов иChatFragment
является одним из них. Я хочу отправить это сообщение отGCMIntentService
вChatFragmen
т.
Мой класс ChatFragment:
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(Con,text context, Intent intent) {
String msg = intent.getStringExtra("msg");
Toast.makeText(getActivity(),msg,Toast.LENGTH_LONG).show();
}
};
Мой класс ChatBroadCastReceiver для chatFragment:
public class ChatBroadCastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
}
}
Я отправляю сообщение с намерением в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();
}
};
Должен ли я включить что-нибудь в список о получателе ??