Set*_*sak 2 android broadcastreceiver android-service android-fragments google-cloud-messaging
我想更新UI一个fragmentfrom 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);
}
Run Code Online (Sandbox Code Playgroud)
在onHandleIntent()方法I中广播的消息发送到被注册到一个片段BroadCastReceiver.I具有一个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(Context context, Intent intent) {
String msg = intent.getStringExtra("msg");
Toast.makeText(getActivity(),msg,Toast.LENGTH_LONG).show();
}
};
Run Code Online (Sandbox Code Playgroud)
我用于chatFragment的ChatBroadCastReceiver类:
public class ChatBroadCastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
}
}
Run Code Online (Sandbox Code Playgroud)
我正在意图发送消息并在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();
}
};
Run Code Online (Sandbox Code Playgroud)
我是否必须在接收器中包含任何内容?
小智 9
在您的片段类中使用以下代码段:
注册广播:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getActivity().registerReceiver(this.broadCastNewMessage, new IntentFilter("bcNewMessage"));
...
}
Run Code Online (Sandbox Code Playgroud)
宣布你的广播:
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();
...
}
}
Run Code Online (Sandbox Code Playgroud)
3.取消注册您的广播:
@Override
public void onDestroy() {
super.onDestroy();
getActivity().unregisterReceiver(broadCastNewMessage);
}
Run Code Online (Sandbox Code Playgroud)
然后在你的服务中调用sendBroadcast()方法:
this.sendBroadcast(new Intent().setAction("bcNewMessage"));
Run Code Online (Sandbox Code Playgroud)
请享用!
| 归档时间: |
|
| 查看次数: |
2886 次 |
| 最近记录: |