//发送广播的activity
int nSrcPID ;
public final static int BROADCAST_CHAT_MESSAGE = 1; // 广播聊天信息
public Handler myHandler = new Handler() {
@Override
public void handleMessage(Message message) {
switch (message.what) {
case BROADCAST_CHAT_MESSAGE:// 广播聊天信息
ActivityManager am = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
ComponentName cn = am.getRunningTasks(1).get(0).topActivity;
String topActivityName = cn.getClassName() ;
System.out.println("topActivityName=="+name);
System.out
.println("-------------------handleMessage-- BROADCAST_CHAT_MESSAGE --------------");
Intent intent = new Intent () ;
intent.putExtra("nSrcPID", nSrcPID);
intent.putExtra("broadcastType", 0);//广播有好友消息,所有注册了该广播接收器的activity都会接收到
// intent.setAction(topActivityName);
intent.setAction("com.oneshow.chatmessage.broadcast");
sendBroadcast(intent);
break ;
}
super.handleMessage(message);
}
};
//接收广播的activity
MessageSplashView messageSplashView;// 闪动VIEW
DataReceiver dataReceiver;// 接收器
int chatfriend;// 发来信息的好友id
@Override
protected void onStart() {
System.out.println("OnStart();");
regBroadcast();
super.onStart();
}
private void regBroadcast() {
dataReceiver = new DataReceiver();
IntentFilter filter = new IntentFilter();// 创建IntentFilter对象
filter.addAction("com.oneshow.chatmessage.broadcast");
// filter.addAction("com.oneshow.HallActivity");
registerReceiver(dataReceiver, filter);// 注册Broadcast Receiver
}
public class DataReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
int broadcastType = intent.getExtras().getInt("broadcastType");
Log.i("HallActivity", "DataReceiver onReceive broadcastType="+broadcastType);
switch(broadcastType)
{
case 0://添加广播消息
chatfriend = intent.getExtras().getInt("nSrcPID");
messageSplashView.isshow = true;
messageSplashView.invalidate();
break ;
case 1://移除广播消息
messageSplashView.isshow = false;
break ;
}
}
}
// /
messageSplashView = (MessageSplashView) findViewById(R.id.messageSplashView);
messageSplashView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (messageSplashView.isshow) {
messageSplashView.isshow = false;
// 发送一个移除消息的广播,改变其他接收到广播消息的activity状态
Intent intent = new Intent();
intent.putExtra("broadcastType", 1);
intent.setAction("com.oneshow.chatmessage.broadcast");
sendBroadcast(intent);
if (queue.size() > 0) {
Stuserinfo stUserInfo = (Stuserinfo) queue.poll();
myFriendVec.addElement(stUserInfo);
Message msg = new Message();
msg.what = UPDATE_MY_FRIEND_ON_LINE;
myHandler.sendMessage(msg);
curChatID = stUserInfo.nPID;
}
}
}
});
// //
@Override
protected void onDestroy() {
Log.i("Activity", "onDestroy");
unregisterReceiver(dataReceiver);// 销毁时需要unregisterReceiver
super.onDestroy();
}