Android中关于长连接的使用
来源:互联网 发布:淘宝天猫运营 编辑:程序博客网 时间:2024/06/04 19:23
1.首先在AndroidManifest.xml中静态注册用户解锁行为的广播:android.intent.action.USER_PRESENT.
2.用户锁屏解锁后,系统发送广播android.intent.action.USER_PRESENT,然后在AppReceiver.java中接收广播
3.在PushService.java中执行一系列动作
<!-- APP的广播接收器 --><receiver android:name=".receiver.AppReceiver"><span style="white-space:pre"></span><intent-filter><span style="white-space:pre"></span><action android:name="android.intent.action.USER_PRESENT"/><span style="white-space:pre"></span></intent-filter></receiver>
2.用户锁屏解锁后,系统发送广播android.intent.action.USER_PRESENT,然后在AppReceiver.java中接收广播
public class AppReceiver extends BroadcastReceiver {<span style="white-space:pre"></span>private static final String TAG = "AppReceiver";<span style="white-space:pre"></span>@Override<span style="white-space:pre"></span>public void onReceive(Context context, Intent intent) {<span style="white-space:pre"></span>String action = intent.getAction();<span style="white-space:pre"></span>if (Intent.ACTION_USER_PRESENT.equals(action)) { //手机解锁<span style="white-space:pre"></span>// 检查长连接<span style="white-space:pre"></span>PushService.actionCheck(context);<span style="white-space:pre"></span>}<span style="white-space:pre"></span>}}
3.在PushService.java中执行一系列动作
//启动Serviceprivate static final String ACTION_CHECK = "pushservice.check";public static void actionCheck(Context context) {Intent intent = new Intent(context, PushService.class);intent.setAction(ACTION_CHECK);context.startService(intent);}//对Service做处理@Overridepublic int onStartCommand(Intent intent, int flags, int startId) {if (intent != null) {String action = intent.getAction();if (action != null) {action.equals(ACTION_CHECK) {checkPushService();//检查PushService是否运行}}}}//检查PushService的几种情况: //1.用户锁屏解锁后 //2.手机网络情况发生改变 //3.连接断开时 //4.定时检查private void checkPushService() {if (pushClient != null && pushClient.isWorking()) {Log.d(TAG, "长连接正在运行...");} else {Log.d(TAG,"长连接断开,需要重新启动");startPushService();}}//启动长连接//启动PushService,总是重启PushClient,不做状态检查,目前在以下几种情况下会调用该方法://1. 系统重启 2. 用户登录 3. 检查PushService状态不正常时private void startPushService() {Log.d(TAG,"正在启动长连接...");stopKeepPushServiceAlive();startKeepPushServiceAlive();startPushClient(userId);}//停止计时public void stopKeepPushServiceAlive() {Log.d(TAG,"stopKeepPushServiceAlive()");Intent intent = new Intent();intent.setClass(PushService.this, PushService.class);intent.setAction(ACTION_CHECK);PendingIntent pi = PendingIntent.getService(PushService.this, KEEP_ALIVE_ALARM_ID, intent ,PendingIntent.FLAG_UPDATE_CURRENT);AlarmManager alarmMgr = (AlarmManager) PushService.this.getSystemService(ALARM_SERVICE);alarmMgr.cancel(pi);}//重新开始计时public static final int KEEP_ALIVE_ALARM_ID = 2001;private static final long KEEP_ALIVE_INTERVAL = 5 * 60 * 1000;public void startKeepPushServiceAlive() {Intent intent = new Intent();intent.setClass(PushService.this, PushService.class);intent.setAction(ACTION_CHECK);PendingIntent pi = PendingIntent.getService(PushService.this, KEEP_ALIVE_ALARM_ID, intent, PendingIntent.FLAG_UPDATE_CURRENT);AlarmManager alarmMgr = (AlarmManager) PushService.this.getSystemService(ALARM_SERVICE);alarmMgr.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime() + KEEP_ALIVE_INTERVAL, KEEP_ALIVE_INTERVAL, pi);}//private startPushClient(String userId) {if (pushClient != null) {pushClient.stopPushService(true);}if (Utils.isNetworkAvailable(this)) {pushClient = new PushClient(userId, 150);pushClient.asynStartLoop(this);Log.d(TAG, "start push client, userId =" + userId);} else {Log.d(TAG, "network not available when start push client");}}//连接成功的通知@Overridepublic void connectionSuccess() {// 拉取新的消息Log.d(TAG, "connectionSuccess Thread id = " + Thread.currentThread().getId());mHandler.obtainMessage(MSG_CONNECTED_SOCKET).sendToTarget();}//private Callback mPushMessageCallback = new Callback() {@Overridepublic boolean handleMessage(Message msg) {switch (msg.what) {case MSG_CONNECTED_SOCKET:onConnected();break;}}};//当PushClient连接成功后,清空重试次数,加载新消息.private void onConnected() {Log.d(TAG, "push service connected");this.retries.set(0);// 长连接消息的有效时间为5分钟,如果断开时间不到3分钟,不再主动拉取消息long now = System.currentTimeMillis();if (now - lastConnectedTime < TimeUnit.SECONDS.toMillis(180) ||(mLoadingNewMessageTask != null && mLoadingNewMessageTask.getTaskStatus() == Status.RUNNING)) {return;}lastConnectedTime = now;pullNewMessageList();}
0 0
- Android中关于长连接的使用
- 关于http长连接使用的分享
- 关于android长连接及休眠唤醒的几篇文章
- Mina长连接Android使用
- 关于Android开发中如何使用dp表示长宽
- android中对服务端的长连接【socket】
- android中对服务端的长连接【socket】
- android 消息推送时,长连接的使用(websocket)
- 使用mina保持android端和服务端的长连接
- 关于socket长连接的心跳包
- 关于socket长连接的心跳包
- 关于socket长连接的心跳包
- 关于socket长连接的心跳包
- 关于socket长连接的心跳包
- 关于socket长连接的心跳包
- 关于HTTP1.1的长连接
- 关于socket长连接的心跳包
- 关于socket长连接的心跳包
- python基础学习八: 界面设计基础
- 堆跟栈
- 国内外三个不同领域巨头分享的Redis实战经验及使用场景
- ViewPager
- Java设计模式 · 单例模式
- Android中关于长连接的使用
- Elasticsearch实战系列-入门篇
- 韩顺平 javascript教学视频_学习笔记24_dom编程开山篇_dom编程实例(乌龟抓鸡)
- CSpreadSheet类学习,用于操作excel表格。
- (转)作为一个新人,怎样学习嵌入式Linux?(韦东山)
- SQL实现表名更改,列名更改,约束更改
- extjs里(function(){Ext.onReady(function(){});})();为什么要这么写
- Xgboost + python Win7 安装
- <java>使用三种循环方式输出九九乘法表