package com.way.chat.activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.widget.RemoteViews;
import android.widget.Toast;
import com.way.Constants;
import com.way.chat.common.bean.ChatMsgEntity;
import com.way.chat.common.bean.TextMessage;
import com.way.chat.common.bean.User;
import com.way.chat.common.tran.bean.TranObject;
import com.way.chat.common.tran.bean.TranObjectType;
import com.way.client.Client;
import com.way.client.ClientInputThread;
import com.way.client.ClientOutputThread;
import com.way.client.MessageListener;
import com.way.util.MessageDB;
import com.way.util.MyDate;
import com.way.util.SharePreferenceUtil;
/**
* 收取消息服务
*
* @author way
*
*/
public class GetMsgService extends Service {
private static final int MSG = 0x001;
public static Client client = new Client();
public static NotificationManager mNotificationManager;
public static boolean isStart = false;//是否与服务器连接上
public static int newMsgNum = 0;// 新消息条数
private Notification mNotification;
private Context mContext = this;
private SharePreferenceUtil util;
private MessageDB messageDB;
private BroadcastReceiver backKeyReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
Toast.makeText(context, "QQ进入后台运行", 0).show();
setMsgNotification();
}
};
// 用来更新通知栏消息的handler
private Handler handler = new Handler() {
@SuppressWarnings("unchecked")
public void handleMessage(Message msg) {
switch (msg.what) {
case MSG:
newMsgNum++;// 每收到一次消息,自增一次
TranObject<TextMessage> textObject = (TranObject<TextMessage>) msg
.getData().getSerializable("msg");
System.out.println(textObject);
if (textObject != null) {
int form = textObject.getFromUser();// 消息从哪里来
String content = textObject.getObject().getMessage();// 消息内容
ChatMsgEntity entity = new ChatMsgEntity("",
MyDate.getDateEN(), content, -1, true);// 收到的消息
messageDB.saveMsg(form, entity);// 保存到数据库
// 更新通知栏
int icon = R.drawable.notify_newmessage;
CharSequence tickerText = form + ":" + content;
long when = System.currentTimeMillis();
mNotification = new Notification(icon, tickerText, when);
// RemoteViews view = mNotification.contentView;
// view.setTextViewText(R.id.notify_num, "("
// + newMsgNum + "条新消息)");
mNotification.flags = Notification.FLAG_NO_CLEAR;
// 设置默认声音
mNotification.defaults |= Notification.DEFAULT_SOUND;
// 设定震动(需加VIBRATE权限)
mNotification.defaults |= Notification.DEFAULT_VIBRATE;
mNotification.contentView = null;
Intent intent = new Intent(mContext,
FriendListActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(
mContext, 0, intent, 0);
mNotification.setLatestEventInfo(mContext, util.getName()
+ " (" + newMsgNum + "条新消息)", content,
contentIntent);
}
mNotificationManager.notify(Constants.NOTIFY_ID, mNotification);
break;
default:
break;
}
}
};
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {// 在onCreate方法里面注册广播接收者
// TODO Auto-generated method stub
super.onCreate();
messageDB = new MessageDB(this);
IntentFilter filter = new IntentFilter();
filter.addAction(Constants.BACKKEY_ACTION);
registerReceiver(backKeyReceiver, filter);
mNotificationManager = (NotificationManager) getSystemService(android.content.Context.NOTIFICATION_SERVICE);
}
@Override
public void onStart(Intent intent, int startId) {
super.onStart(intent, startId);
util = new SharePreferenceUtil(getApplicationContext(),
Constants.SAVE_USER);
isStart = client.start();
System.out.println("service:" + isStart);
if (isStart) {
ClientInputThread in = client.getClientInputThread();
in.setMessageListener(new MessageListener() {
@SuppressWarnings("rawtypes")
@Override
public void Message(TranObject msg) {
// System.out.println("GetMsgService:" + msg);
if (util.getIsStart()) {// 如果 是在后台运行,就更新通知栏,否则就发送广播给Activity
if (msg.getType() == TranObjectType.MESSAGE) {// 只处理文本消息类型
// System.out.println("收到新消息");
// 把消息对象发送到handler去处理
Message message = handler.obtainMessage();
message.what = MSG;
message.getData().putSerializable("msg", msg);
handler.sendMessage(message);
}
} else {
Intent broadCast = new Intent();
broadCast.setAction(Constants.ACTION);
broadCast.putExtra(Constants.MSGKEY, msg);
sendBroadcast(broadCast);// 把收到的消息已广播的形式发送出去
}
}
});
}
}
@Override
// 在服务被摧毁时,做一些事情
public void onDestroy() {
super.onDestroy();
messageDB.close();
unregisterReceiver(backKeyReceiver);
mNotificationManager.cancel(Constants.NOTIFY_ID);
// 给服务器发送下线消息
if (isStart) {
ClientOutputThread out = client.getClientOutputThread();
TranObject<User> o = new TranObject<User>(TranObjectType.LOGOUT);
User u = new User();
u.setId(Integer.parseInt(util.getId()));
o.setObject(u);
out.setMsg(o);
// 发送完之后,关闭client
out.setStart(false);
client.getClientInputThread().setStart(false);
}
// Intent intent = new Intent(this, GetMsgService.class);
// startService(intent);
}
/**
* 创建通知
*/
private void setMsgNotification() {
int icon = R.drawable.notify;
CharSequence tickerText = "";
long when = System.currentTimeMillis();
mNotification = new Notification(icon, tickerText, when);
// 放置在"正在运行"栏目中
mNotification.flags = Notification.FLAG_ONGOING_EVENT;
RemoteViews contentView = new RemoteViews(mContext.getPackageName(),
R.layout.notify_view);
contentView.setTextViewText(R.id.notify_name, util.getName());
contentView.setTextViewText(R.id.notify_msg, "手机QQ正在后台运行");
contentView.setTextViewText(R.id.notify_time, MyDate.getDateEN());
// 指定个性化视图
mNotification.contentView = contentView;
Intent intent = new Intent(this, FriendListActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(mContext, 0,
intent, PendingIntent.FLAG_UPDATE_CURRENT);
// 指定内容意图
mNotification.contentIntent = contentIntent;
mNotificationManager.notify(Constants.NOTIFY_ID, mNotification);
}
}