时间:2021-05-20
1.消息推送机制
服务器器端需要变被动为主动,通知客户一些开发商认为重要的信息,无论应用程序是否正在运行或者关闭。
我想到了一句话:don't call me,i will call you!
qq今天在右下角弹出了一个对话框:"奥巴马宣布本拉登挂了...",正是如此。
自作聪明,就会带点小聪明,有人喜欢就有人讨厌。
2.独立进程
无论程序是否正在运行,我们都要能通知到客户,我们需要一个独立进程的后台服务。
我们需要一个独立进程的后台服务。
在androidmanifest.xml中注册service时,有一个android:process属性,如果这个属性以"."开头,则为此服务开启一个
全局的独立进程,如果以":"开头则为此服务开启一个为此应用私有的独立进程。举个具体的例子吧,我们新建了一个
application,创建了主进程com.cnblogs.tianxia,那么:
复制代码 代码如下:
<!--下面会创建一个全局的com.cnblogs.tianxia.message的独立进程-->
<service android:name=".service.messageservice" android:label="消息推送" android:process=".message" />
<!--或者-->
<!--下面会创建一个应用私有的com.cnblogs.tianxia:message的独立进程-->
<service android:name=".service.messageservice" android:label="消息推送" android:process=":message" />
我们没必要建立一个全局的,本文选择第二种方案,创建一个当前应用私有的独立进程。
3.通知用户和点击查看
public class messageservice extends service {
//获取消息线程
private messagethread messagethread = null;
//点击查看
private intent messageintent = null;
private pendingintent messagependingintent = null;
//通知栏消息
private int messagenotificationid = 1000;
private notification messagenotification = null;
private notificationmanager messagenotificatiomanager = null;
public ibinder onbind(intent intent) {
return null;
}
@override
public int onstartcommand(intent intent, int flags, int startid) {
//初始化
messagenotification = new notification();
messagenotification.icon = r.drawable.icon;
messagenotification.tickertext = "新消息";
messagenotification.defaults = notification.default_sound;
messagenotificatiomanager = (notificationmanager)getsystemservice(context.notification_service);
messageintent = new intent(this, messageactivity.class);
messagependingintent = pendingintent.getactivity(this,0,messageintent,0);
//开启线程
messagethread = new messagethread();
messagethread.isrunning = true;
messagethread.start();
return super.onstartcommand(intent, flags, startid);
}
/**
* 从服务器端获取消息
*
*/
class messagethread extends thread{
//运行状态,下一步骤有大用
public boolean isrunning = true;
public void run() {
while(isrunning){
try {
//休息10分钟
thread.sleep(600000);
//获取服务器消息
string servermessage = getservermessage();
if(servermessage!=null&&!"".equals(servermessage)){
//更新通知栏
messagenotification.setlatesteventinfo(messageservice.this,"新消息","奥巴马宣布,本拉
登兄弟挂了!"+servermessage,messagependingintent);
messagenotificatiomanager.notify(messagenotificationid, messagenotification);
//每次通知完,通知id递增一下,避免消息覆盖掉
messagenotificationid++;
}
} catch (interruptedexception e) {
e.printstacktrace();
}
}
}
}
/**
* 这里以此方法为服务器demo,仅作示例
* @return 返回服务器要推送的消息,否则如果为空的话,不推送
*/
public string getservermessage(){
return "yes!";
}
}
其中messageactivity是点击跳转的activity,负责处理查看详细信息。
我们在其他activity中调用一下:
复制代码 代码如下:
boolean ismessagepush = true;//不开启就设置为false;
...
if(ismessagepush){
startservice(new intent(this, messageservice.class))
};
运行一下:
4.停止服务
1 stopservice(new intent(myactivity.this,messageservice.class));
2 setmessagepush(false);//设置配置文件或数据库中flag为false
运行一下,停止服务后,却出乎意料的并没有停下来,怎么回事?是不是代码写错了?
代码没有错,错在我们停止了服务,却没有停止进程,退出线程。
5.退出线程
实践证明,thread的stop()方法并不可靠。但是我们有其他的办法。
在代码面前,程序员就是上帝。
退出线程有两种方法。
第一种方法,强制退出。
//杀死该线程所在的进程,自然就退出了
2 system.exit(0);
第二种方法,设置isrunning为false。
view sourceprint?1 //前面说到了isrunning这个标志,设置为false后,线程的执行就从while循环中跳出来了,然后自然结束
掉了
2 messagethread.isrunning = false;
综合一下,我们在messageservice中重载ondestroy()方法如下:
复制代码 代码如下:
@override
public void ondestroy() {
system.exit(0);
//或者,二选一,推荐使用system.exit(0),这样进程退出的更干净
//messagethread.isrunning = false;
super.ondestroy();
}
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
一、引言IOS中消息的推送有两种方式,分别是本地推送和远程推送,本地推送在这篇博客中有详细的介绍,这里主要讨论远程推送的流程与配置过程。二、远程推送机制的原理1
此外,「订阅消息」的调整是微信在唤醒机制上的一次尝试,上线“一次订阅有多次推送机会、实现小程序的精细化运营、开发者可以随时推送服务通知”等功能,一切以用户价值为
本文实例讲述了Android编程中的消息机制。分享给大家供大家参考,具体如下:在分析Android消息机制之前,我们先来看一段代码:publicclassMai
在前面都写到用AsyncTask来获取网络中的图片。其实利用消息机制也能获取网络中的图片,而且本人感觉用消息机制还是挺简单的。消息机制的图解:下面就用看代码来理
本文实例讲述了Android基于广播事件机制实现简单定时提醒功能代码。分享给大家供大家参考,具体如下:1.Android广播事件机制Android的广播事件处理