Androidpn学习与使用6
来源:互联网 发布:appium python ios 编辑:程序博客网 时间:2024/04/27 22:56
看看TaskSubmitter这个类。
/** * Class for summiting a new runnable task. */public class TaskSubmitter {final NotificationService notificationService;public TaskSubmitter(NotificationService notificationService) {this.notificationService = notificationService;}@SuppressWarnings("unchecked")public Future submit(Runnable task) {Future result = null;if (!notificationService.getExecutorService().isTerminated()&& !notificationService.getExecutorService().isShutdown()&& task != null) {result = notificationService.getExecutorService().submit(task);}return result;}}由代码可以看出,这个类由一个NotificationService来构成,并且只有一个submit方法,返回一个Future对象
http://blog.csdn.net/yangyan19870319/article/details/6093481这篇文章介绍Future,不错。
其实通俗的讲,TaskSubmitter的subit方法就是,在连接正常的情况下,在线程池中跑一个线程task.
我猜测这个task其实就是用来进行socket通信用的。。。
回到NotificationService里面的connet方法,
public void connect() {Log.d(LOGTAG, "connect()...");taskSubmitter.submit(new Runnable() {public void run() {NotificationService.this.getXmppManager().connect();}});}应该是XmppManager起作用了,开始进行网络通信了。
在onCreate方法中,XmppManager进行了初始化。
.getXmppManager().connect();是重点,等下看,先看看NotificationService的onCreate方法:
public void onCreate() {Log.d(LOGTAG, "onCreate()...");telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);// wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);// connectivityManager = (ConnectivityManager)// getSystemService(Context.CONNECTIVITY_SERVICE);sharedPrefs = getSharedPreferences(Constants.SHARED_PREFERENCE_NAME,Context.MODE_PRIVATE);// Get deviceIddeviceId = telephonyManager.getDeviceId();// Log.d(LOGTAG, "deviceId=" + deviceId);Editor editor = sharedPrefs.edit();editor.putString(Constants.DEVICE_ID, deviceId);editor.commit();//以上得到一些手机信息// If running on an emulatorif (deviceId == null || deviceId.trim().length() == 0//模拟器|| deviceId.matches("0+")) {if (sharedPrefs.contains("EMULATOR_DEVICE_ID")) {deviceId = sharedPrefs.getString(Constants.EMULATOR_DEVICE_ID,"");} else {deviceId = (new StringBuilder("EMU")).append((new Random(System.currentTimeMillis())).nextLong()).toString();editor.putString(Constants.EMULATOR_DEVICE_ID, deviceId);editor.commit();}}Log.d(LOGTAG, "deviceId=" + deviceId);// 以上都是一些配置信息xmppManager = new XmppManager(this);//初始化XmppManagertaskSubmitter.submit(new Runnable() {public void run() {NotificationService.this.start();// 开启推送服务}});}初始化XmppManager和开启推送服务是重点。
先看看XmppManager的初始化代码
public XmppManager(NotificationService notificationService) {context = notificationService;//服务名taskSubmitter = notificationService.getTaskSubmitter();//得到TaskSubmittertaskTracker = notificationService.getTaskTracker();//得到TaskTracker,这个跟踪器就是用来统计一种NotificationService的行为次数的,具体还没研究sharedPrefs = notificationService.getSharedPreferences();//xmppHost = sharedPrefs.getString(Constants.XMPP_HOST, "localhost");xmppPort = sharedPrefs.getInt(Constants.XMPP_PORT, 5222);username = sharedPrefs.getString(Constants.XMPP_USERNAME, "aaa");password = sharedPrefs.getString(Constants.XMPP_PASSWORD, "a ");// 拿到一些参数connectionListener = new PersistentConnectionListener(this);notificationPacketListener = new NotificationPacketListener(this);handler = new Handler();taskList = new ArrayList<Runnable>();reconnection = new ReconnectionThread(this);}
下面看看PersisitentConnectionListener,
这个类名字上看,就是给现有的连接进行监听的类
public PersistentConnectionListener(XmppManager xmppManager) { this.xmppManager = xmppManager; }从构造方法上看,这个类监听的是XmppManager,剩下一些方法,都是监听连接情况的Log日志。
下面是NotificationPacketListener类,和PersistentConnectionListener结构差不多,主要看他的processPacket方法:
public void processPacket(Packet packet) { Log.d(LOGTAG, "NotificationPacketListener.processPacket()..."); Log.d(LOGTAG, "packet.toXML()=" + packet.toXML()); if (packet instanceof NotificationIQ) { NotificationIQ notification = (NotificationIQ) packet; if (notification.getChildElementXML().contains( "androidpn:iq:notification")) { String notificationId = notification.getId(); String notificationApiKey = notification.getApiKey(); String notificationTitle = notification.getTitle(); String notificationMessage = notification.getMessage(); // String notificationTicker = notification.getTicker(); String notificationUri = notification.getUri(); Intent intent = new Intent(Constants.ACTION_SHOW_NOTIFICATION); intent.putExtra(Constants.NOTIFICATION_ID, notificationId); intent.putExtra(Constants.NOTIFICATION_API_KEY, notificationApiKey); intent .putExtra(Constants.NOTIFICATION_TITLE, notificationTitle); intent.putExtra(Constants.NOTIFICATION_MESSAGE, notificationMessage); intent.putExtra(Constants.NOTIFICATION_URI, notificationUri); // intent.setData(Uri.parse((new StringBuilder( // "notif://notification.androidpn.org/")).append( // notificationApiKey).append("/").append( // System.currentTimeMillis()).toString())); xmppManager.getContext().sendBroadcast(intent); } }ReconnectionThread类:
这个是一个负责重连的线程类
private final XmppManager xmppManager; private int waiting;//等待时间 ReconnectionThread(XmppManager xmppManager) { this.xmppManager = xmppManager; this.waiting = 0;//初始化等待时间是0 }
public void run() { try { while (!isInterrupted()) {// Log.d(LOGTAG, "Trying to reconnect in " + waiting() + " seconds"); Thread.sleep((long) waiting() * 1000L); xmppManager.connect(); waiting++; } } catch (final InterruptedException e) { xmppManager.getHandler().post(new Runnable() { public void run() { xmppManager.getConnectionListener().reconnectionFailed(e); } }); } }
- Androidpn学习与使用6
- Androidpn学习与使用1
- Androidpn学习与使用2
- Androidpn学习与使用3
- Androidpn学习与使用4
- Androidpn学习与使用5
- androidpn学习笔记.与客户端的通信
- androidpn和smack的关系与使用上的差别
- androidpn学习 (一) androidpn server简单介绍
- androidpn——学习
- Androidpn 学习、应用
- androidpn 学习记录
- 整合androidpn与SSH
- 使用androidpn(一)
- 使用androidpn(二)
- Androidpn的使用
- androidpn
- Androidpn
- NMAP使用实例(六)
- Oracle常用命令——工作记录
- poj 2409
- 数据结构与算法系列-树-二叉树的定义与性质
- hdoj-1002 大数加法
- Androidpn学习与使用6
- Sencha Touch 2学习笔记(一)---环境搭建和开发工具配置
- 定义一个函数,其参数为年、月、日的值,返回这一天为该年的第几天
- discuz收录
- 理解指针
- JQuery练习——伸缩的导航
- NMAP使用实例(七)
- MySql安装问题——startService时未响应
- 算法入门5:贪心算法