AndroidPN
https://github.com/dannytiehui/androidpn
郭霖 Android消息推送教学视频
http://www.imooc.com/learn/223
http://www.imooc.com/learn/358
Asmack
https://github.com/Flowdalic/asmack(官网提示asmack废弃换成新的smack)
客户端如何与服务器交互?
开启Service
开启NotificationService服务,后台与服务器交互
ServiceManager serviceManager = new ServiceManager(this);
serviceManager.setNotificationIcon(R.drawable.notification);
serviceManager.startService();
NotificationService onCreate方法
...
xmppManager = new XmppManager(this);
taskSubmitter.submit(new Runnable() {
//TaskSubmitter开启SingleThreadExcutor线程池
public void run() {
NotificationService.this.start();
}
});
NotificationService start方法
private void start() {
Log.d(LOGTAG, "start()...");
registerNotificationReceiver();//注册通知广播
registerConnectivityReceiver();//注册网络广播
xmppManager.connect();//xmppmanger开启连接
}
添加执行Task
XmppManager conncet等方法
public void connect() {
Log.d(LOGTAG, "connect()...");
submitLoginTask();
}
private void submitConnectTask() {
Log.d(LOGTAG, "submitConnectTask()...");
addTask(new ConnectTask());
}
private void submitRegisterTask() {
Log.d(LOGTAG, "submitRegisterTask()...");
submitConnectTask();
addTask(new RegisterTask());
}
private void submitLoginTask() {
Log.d(LOGTAG, "submitLoginTask()...");
submitRegisterTask();
addTask(new LoginTask());
}
Task添加顺序ConnectTask->RegisterTask->LoginTask,执行顺序也是如此
addTask方法
private void addTask(Runnable runnable) {
Log.d(LOGTAG, "addTask(runnable)...");
taskTracker.increase();//Task追踪器(计数Task,add task则increase; submit task则decrease)
synchronized (taskList) {
if (taskList.isEmpty() && !running) {
running = true;
futureTask = taskSubmitter.submit(runnable);//提交到singleThread线程池中执行
if (futureTask == null) {
taskTracker.decrease();
}
} else {
taskList.add(runnable);//如果队列不空且有运行的task则将新的task加入到队列中
}
}
Log.d(LOGTAG, "addTask(runnable)... done");
}
与之比较runTask方法
public void runTask() {
Log.d(LOGTAG, "runTask()...");
synchronized (taskList) {
running = false;//将runing标识为false
futureTask = null;
if (!taskList.isEmpty()) {
Runnable runnable = (Runnable) taskList.get(0);//获取task
taskList.remove(0);//将该task移除task队列
running = true;//将running标识为true
futureTask = taskSubmitter.submit(runnable);//提交到singleThread线程池中执行
if (futureTask == null) {
taskTracker.decrease();
}
}
}
taskTracker.decrease();
Log.d(LOGTAG, "runTask()...done");
}
addTask添加Task,如果当前tasks列表中无Task且无运行的Task,则执行该Task,如果tasks列表有Task或者有运行的Task,则将Task加入到tasks等待执行
runTask执行Task,tasks列表中有Task则执行tasks.get(0)的Task,且移除tasks列表中该Task
Task之ConnectTask
ConnectTask进行客户端和服务器进行连接
如果未连接且未认证,则进行socket连接以及socket流初始化,packetwriter以及packetreader初始化和启动
如果已连接和认证,则登录(包括匿名登录)
public void run() {
Log.i(LOGTAG, "ConnectTask.run()...");
if (!xmppManager.isConnected()) {
// Create the configuration for this new connection
// 配置连接ConnectionConfiguration
ConnectionConfiguration connConfig = new ConnectionConfiguration(
xmppHost, xmppPort);
// connConfig.setSecurityMode(SecurityMode.disabled);
connConfig.setSecurityMode(SecurityMode.required);
connConfig.setSASLAuthenticationEnabled(false);
connConfig.setCompressionEnabled(false);
XMPPConnection connection = new XMPPConnection(connConfig);
xmppManager.setConnection(connection);
try {
// Connect to the server
connection.connect();//连接到服务器
// Enhance[2015/10/19]
xmppManager.runTask();//执行task
Log.i(LOGTAG, "XMPP connected successfully");
// packet provider
ProviderManager.getInstance().addIQProvider("notification",
"androidpn:iq:notification",
new NotificationIQProvider());//添加NotificationIQProvider,继承IQProvider,用来解析IQ数据包为NotificationIQ
} catch (XMPPException e) {
Log.e(LOGTAG, "XMPP connection failed", e);
// Enhance[2015/10/19]
xmppManager.dropTask(2);
xmppManager.runTask();
xmppManager.startReconnectionThread();
}
// xmppManager.runTask();
} else {
Log.i(LOGTAG, "XMPP conn