从服务的Andr​​oid调用BroadcastReceiver的片段中更新UI? [英] Calling BroadCastReceiver from Service in android to Update UI in Fragment?

查看:284
本文介绍了从服务的Andr​​oid调用BroadcastReceiver的片段中更新UI?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要更新片段 UI 服务。我现在用 GCM 发送message.I有这个类 GCM

I want to update the UI of a fragment from service.I am using GCM for sending message.I have this class for 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()方法我广播消息到注册到广播接收器片段。我有有一个 SlidingDrawerActivity 键,它有许多片段和 ChatFragment 是想从<$ C发送这封邮件them.I之一$ C> GCMIntentService 到 ChatFragmen T。结果

In the onHandleIntent() method i broadcasted the message to a fragment which is registered to BroadCastReceiver.I have one SlidingDrawerActivity and it has many Fragments and ChatFragment is one of them.I want to send this message from GCMIntentService to ChatFragment.

我的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(Context 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(送出数据)和接受它里面 chatFragment 的onReceive()方法,我希望看到Toast.But吐司消息没有显示。结果的我有没有实现的BroadcastReceiver正确的内部服务片段??

I am sending the message with intent in sendBroadcast(sendData) and receiving it inside chatFragment and in onReceive() method i want to see the message in Toast.But Toast is not showing.
Did i implement BroadcastReceiver correctly for fragment inside service??

编辑:我调试,看见下面部分并没有叫

I have debugged and saw that below portion is not calling

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有关接收任何?

Do I have to include anything in the menifest about the receiver??

推荐答案

在您的片段类使用这段代码:

In your fragment class use this snippet:

1。注册播报:

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

        getActivity().registerReceiver(this.broadCastNewMessage, new IntentFilter("bcNewMessage"));
        ...
    }

2。声明您播报:

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();
         ...
    }
}

3。注销您播报:

@Override
    public void onDestroy() {
        super.onDestroy();
        getActivity().unregisterReceiver(broadCastNewMessage);
    }

然后在你的服务拨打 sendBroadcast()方式:

this.sendBroadcast(new Intent().setAction("bcNewMessage"));

享受!

这篇关于从服务的Andr​​oid调用BroadcastReceiver的片段中更新UI?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆