



全文预览已结束
下载本文档
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
Android中利用App实现消息推送机制的代码实例1.消息推送机制 服务器器端需要变被动为主动,通知客户一些开发商认为重要的信息,无论应用程序是否正在运行或者关闭。 我想到了一句话:dont call me,i will call you! qq今天在右下角弹出了一个对话框:奥巴马宣布本拉登挂了.,正是如此。 自作聪明,就会带点小聪明,有人喜欢就有人讨厌。 2.独立进程 无论程序是否正在运行,我们都要能通知到客户,我们需要一个独立进程的后台服务。 我们需要一个独立进程的后台服务。 在androidmanifest.xml中注册service时,有一个android:process属性,如果这个属性以.开头,则为此服务开启一个全局的独立进程,如果以:开头则为此服务开启一个为此应用私有的独立进程。举个具体的例子吧,我们新建了一个application,创建了主进程blogs.tianxia,那么:view sourceprint?1 2 3 4 5 我们没必要建立一个全局的,本文选择第二种方案,创建一个当前应用私有的独立进程。3.通知用户和点击查看view sourceprint?01 public class messageservice extends service 0203 /获取消息线程04 private messagethread messagethread = null;0506 /点击查看07 private intent messageintent = null;08 private pendingintent messagependingintent = null;0910 /通知栏消息11 private int messagenotificationid = 1000;12 private notification messagenotification = null;13 private notificationmanager messagenotificatiomanager = null;1415 public ibinder onbind(intent intent) 16 return null;17 1819 override20 public int onstartcommand(intent intent, int flags, int startid) 21 /初始化22 messagenotification = new notification();23 messagenotification.icon = r.drawable.icon;24 messagenotification.tickertext = 新消息;25 messagenotification.defaults = notification.default_sound;26 messagenotificatiomanager = (notificationmanager)getsystemservice(context.notification_service);2728 messageintent = new intent(this, messageactivity.class);29 messagependingintent = pendingintent.getactivity(this,0,messageintent,0);3031 /开启线程32 messagethread = new messagethread();33 messagethread.isrunning = true;34 messagethread.start();3536 return super.onstartcommand(intent, flags, startid);37 3839 /*40 * 从服务器端获取消息41 *42 */43 class messagethread extends thread44 /运行状态,下一步骤有大用45 public boolean isrunning = true;46 public void run() 47 while(isrunning)48 try 49 /休息10分钟50 thread.sleep(600000);51 /获取服务器消息52 string servermessage = getservermessage();53 if(servermessage!=null&!.equals(servermessage)54 /更新通知栏55 messagenotification.setlatesteventinfo(messageservice.this,新消息,奥巴马宣布,本拉登兄弟挂了!+servermessage,messagependingintent);56 messagenotificatiomanager.notify(messagenotificationid, messagenotification);57 /每次通知完,通知id递增一下,避免消息覆盖掉58 messagenotificationid+;59 60 catch (interruptedexception e) 61 e.printstacktrace();62 63 64 65 6667 /*68 * 这里以此方法为服务器demo,仅作示例69 * return 返回服务器要推送的消息,否则如果为空的话,不推送70 */71 public string getservermessage()72 return yes!;73 74 其中messageactivity是点击跳转的activity,负责处理查看详细信息。 我们在其他activity中调用一下:view sourceprint?1 boolean ismessagepush = true;/不开启就设置为false;2 .3 if(ismessagepush)4 startservice(new intent(this, messageservice.class)5 ; 运行一下:4.停止服务view sourceprint?1 stopservice(new intent(myactivity.this,messageservice.class);2 setmessagepush(false);/设置配置文件或数据库中flag为false 运行一下,停止服务后,却出乎意料的并没有停下来,怎么回事?是不是代码写错了? 代码没有错,错在我们停止了服务,却没有停止进程,退出线程。5.退出线程 实践证明,thread的stop()方法并不可靠。但是我们有其他的办法。 在代码面前,程序员就是上帝。 退出线程有两种方法。 第一种方法,强制退出。view sourceprint?1 /杀死该线程所在的进程,自然就退出了2 system.exit(0); 第二种方法,设置isrunning为false。view sourceprint?1 /前面说到了isrunning这个标志,设置为false后,线程的执行就从while循环中跳出来了,然后自然结束掉了2 messagethread.isrunning = false; 综合一下,我们在messageservice中重载ondestroy()方法如下:vi
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 公司烟酒库存管理制度
- 公司设备展厅管理制度
- 公司质量内控管理制度
- 广东省茂名市2024~2025学年 高三下册半月考(三)数学试卷附解析
- 模式识别与分类-洞察阐释
- 2024年河北公务员行测(A类)真题及答案
- 郑州市第九人民医院招聘专业技术人才笔试真题2024
- 云浮市罗定市招聘公益性岗位人员笔试真题2024
- 永州市江永县招聘事业单位人员笔试真题2024
- 石大学前儿童保育学课外必读:孩子们最爱听的40句话
- 2025年陕西省新高考语文试卷(含答案解析)
- 2025年江西省高考物理真题
- 饮食与营养试题及答案
- 上海市社区工作者管理办法
- NLP神经语言学培训课件(PPT 164页)
- 脑卒中康复PPT医学课件
- PCB 企业生产工艺及风险点
- Grace评分表、TIMI评分、CRUSAD评分、wells评分等
- 消防安全工作台账-消防台账记录
- 中考数学《分式及分式方程》计算题(附答案)
- 用人单位用工备案花名册
评论
0/150
提交评论