-
Android中利用App实现消息推送机制的代码实例
1.消息推送机制
服务器器端需要变被动为主动,通知客户一些开发商认为重要的信息,无论应用程序是否正在运行或者关闭。
我想到了一句话:don't call me,i will call you!
qq今天在右下角弹出了一个对话框:"奥巴马宣布本拉登挂了...",正是如此。
自作聪明,就会带点小聪明,有人喜欢就有人讨厌。
2.独立进程
无论程序是否正在运行,我们都要能通知到客户,我们需要一个独立进程的后台服务。
我们需要一个独立进程的后台服务。
在androidmanifest.xml中注册service时,有一个android:process属性,如果这个属性以"."开头,则为此服务开启一个
全局的独立进程,如果以":"开头则为此服务开启一个为此应用私有的独立进程。举个具体的例子吧,我们新建了一个
application,创建了主进程com.cnblogs.tianxia,那么:
view sourceprint?1 <!--下面会创建一个全局的com.cnblogs.tianxia.message的独立进程-->
2 <service android:name=".service.messageservice" android:label="消息推送" android:process=".message"></service>
3 <!--或者-->
4 <!--下面会创建一个应用私有的com.cnblogs.tianxia:message的独立进程-->
5 <service android:name=".service.messageservice" android:label="消息推送" android:process=":message"></service>
我们没必要建立一个全局的,本文选择第二种方案,创建一个当前应用私有的独立进程。
3.通知用户和点击查看
view sourceprint?01 public class messageservice extends service {
02
03 //获取消息线程
04 private messagethread messagethread = null;
05
06 //点击查看
07 private intent messageintent = null;
08 private pendingintent messagependingintent = null;
09
10 //通知栏消息
11 private int messagenotificationid = 1000;
12 private notification messagenotification = null;
13 private notificationmanager messagenotificatiomanager = null;
14
15 public ibinder onbind(intent intent) {
16 return null;
17 }
18
19 @override
20 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);
27
28 messageintent = new intent(this, messageactivity.class);
29 messagependingintent = pendingintent.getactivity(this,0,messageintent,0);
30
31 //开启线程
32 messagethread = new messagethread();
33 messagethread.isrunning = true;
34 messagethread.start();
35
36 return super.onstartcommand(intent, flags, startid);
37 }
38
39 /**
40 * 从服务器端获取消息
41 *
42 */
43 class messagethread extends thread{
44 //运行状态,www.3ppt.com下一步骤有大用
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 }
66
67 /**
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()方法如下:
view sourceprint?1 @override
2 public void ondestroy() {
3 system.exit(0);
4 //或者,二选一,推荐使用system.exit(0),这样进程退出的更干净
5 //messagethread.isrunning = false;
6 super.ondestroy();
7 }
分享到:
相关推荐
Android中利用App实现消息推送机制的代码实例.pdf
1.消息推送机制 服务器器端需要变被动为主动,通知客户一些开发商认为重要的信息,无论应用程序是否正在运行或者关闭。 我想到了一句话:don’t call me,i will call you! qq今天在右下角弹出了一个对话框:”奥巴马...
activemq向android推送消息实例,测试环境activemq5.9.1版,别忘了修改你的主机地址
c#调用极光推送往android终端发送消息的实例。 极光推送的ApiKey和APIMasterSecret请在app.config设置。
Android应用源码基于百度云推送的聊天工具源码.rar Android应用源码安卓多边形布局例子.rar Android应用源码安卓拍照上传实现代码附带php端.rar Android应用源码实现动态交叉布局.rar Android应用源码小说翻页效果...
使用JobScheduler,我们可以在用户一段时间没有使用我们的app的情况下,推送本地通知来提高app的用户留存率。废话不多说,上代码: 先在app的MainActivity启动时用JobScheduler来schedule一个job。注意在onCreate中...
11.2.2 推送服务 254 第12章 Android 5.X新特性详解 257 12.1 Android 5.X UI设计初步 258 12.1.1 材料的形态模拟 258 12.1.2 更加真实的动画 258 12.1.3 大色块的使用 259 12.2 Material Design主题 260 ...
Android 一种基于Linux的自由及开放源代码的操作系统,主要使用于移动设备,如智能手机和平板电脑,由Google公司和开放手机联盟领导开发 IOS 由苹果公司开发的移动操作系统 Webkit 一个开源的浏览器引擎,在手机上的...
主要是为开发者提供一套TAE在无线端开放的代码实例,主要完成使用Native的方式实现淘宝商品的选择、购买和支付等交易链路环节,解决了之前导购类App只能通过H5的方式跳转到淘宝无线端的H5页面,打通了在一个App中...
主要介绍了Android开发之Notification通知用法,结合实例形式较为详细的分析了Notification通知的功能、参数、定义及使用方法,需要的朋友可以参考下
HABot HABot是针对openHAB的聊天机器人,它在...在这种情况下,它甚至可以作为Progressive Web App添加到Android主屏幕,并添加仅适用于“安全来源”的功能,例如推送通知,语音识别和资源缓存。 它包括:机器学习
A 新增 云控制台支持全局开启/关闭云函数消息推送 A 新增 项目重命名功能 反馈详情 A 新增 编译模式记录通过二维码编译的条件 U 优化 再次打开项目时的首次编译速度 U 优化 GPU 加速默认打开 U 优化 增加 ...