gpt4 book ai didi

android - 从 Android 中的服务调用 BroadCastReceiver 以更新 Fragment 中的 UI?

转载 作者:太空狗 更新时间:2023-10-29 15:45:46 24 4
gpt4 key购买 nike

我想从 service 更新 fragmentUI。我正在使用 GCM 发送消息.我为 GCM 开设了这个类(class):

    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 的 fragment 。我有一个 SlidingDrawerActivity,它有许多 fragment ,ChatFragment 就是其中之一。我想将此消息从 GCMIntentService 发送到 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();
}
};

我的 chatFragment ChatBroadCastReceiver 类:

public class ChatBroadCastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {

}
}

我在 sendBroadcast(sendData) 中发送消息并在 chatFragmentonReceive() 方法中接收它我想查看 Toast 中的消息。但 Toast 未显示。
我是否为服务内的 fragment 正确实现了 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();
}
};

我必须在 list 中包含有关接收者的任何内容吗?

最佳答案

在您的 fragment 类中使用此 fragment :

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

尽情享受吧!

关于android - 从 Android 中的服务调用 BroadCastReceiver 以更新 Fragment 中的 UI?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30035396/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com