服务单独开进程实现离线消息推送
2015-08-14 07:02
375 查看
http://blog.csdn.net/zhang6622056/article/details/8833178
概述:
首先简单阐述一下我对于消息推送的理解,这里拿QQ来举例吧,当我们手机端的QQ离线了,并且退出了QQ应用,但是这时候如果别人给我们发了信息,我们没有上线。服务器会将发送者发送的信息推送过来然后我们发布通知来显示通知我们的用户
原理简单阐述:
通过以上概述,我们基本了解我们需要一个独立进程的后台服务,在AndroidManifest
.xml中注册Service时,有一个android:process属性这个属性有2种情况,即为.和:两种,其中.代表为此服务开启一个全局的独立进程,如果以:开头则为此服务开启一个为此应用私有的独立进程
编码实现:
ServerPushService文件:
[java] view
plaincopy
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
public class ServerPushService extends Service{
//获取消息线程
private MessageThread messageThread = null;
//点击查看
private Intent messageIntent = null;
private PendingIntent messagePendingIntent = null;
//通知栏消息
private int messageNotificationID = 1000;
private Notification messageNotification = null;
private NotificationManager messageNotificationManager = null;
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
//初始化
messageNotification = new Notification();
messageNotification.icon = R.drawable.ic_launcher; //通知图片
messageNotification.tickerText = "新消息"; //通知标题
messageNotification.defaults = Notification.DEFAULT_SOUND;
messageNotificationManager = (NotificationManager) getSystemService(this.NOTIFICATION_SERVICE);
//点击查看
messageIntent = new Intent(this,MessageActivity.class);
messagePendingIntent = PendingIntent.getActivity(this, 0, messageIntent, 0);
//开启线程
MessageThread thread = new MessageThread();
thread.isRunning = true;
thread.start();
return super.onStartCommand(intent, flags, startId);
}
/***
* 从服务端获取消息
* @author zhanglei
*
*/
class MessageThread extends Thread{
//运行状态
public boolean isRunning = true;
@Override
public void run() {
while(isRunning){
try {
//休息10秒
Thread.sleep(10000);
if(getServerMessage().equals("yes")){
//设置消息内容和标题
messageNotification.setLatestEventInfo(ServerPushService.this, "您有新消息!", "这是一条新的测试消息", messagePendingIntent);
//发布消息
messageNotificationManager.notify(messageNotificationID, messageNotification);
//避免覆盖消息,采取ID自增
messageNotificationID++;
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
/***
* 模拟了服务端的消息。实际应用中应该去服务器拿到message
* @return
*/
public String getServerMessage(){
return "yes";
}
}
注册该service在一个单独的进程中
[html] view
plaincopy
<!-- 为此应用私有的独立进程 -->
<service
android:name="com.jay.serverpush.ServerPushService"
android:process=":message"
>
</service>
说明:该文件编写了一个service用于后台运行,在manifest里面将该service声明成progress为:开头的,这样在一个单独的进程里面运行,以实现在程序关闭之后达到进程不关闭的目的以此来实现离线推送的目的,编码中的注释很明确,扫描服务器、判断逻辑发布通知等,注释很明确在此不在阐述
开启服务的方式:
[java] view
plaincopy
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.startService(new Intent(this,ServerPushService.class));
}
this.startService(new Intent(this,ServerPushService.class));
通过这句话在第一次进入oncreate方法就开启了单独进程的服务
概述:
首先简单阐述一下我对于消息推送的理解,这里拿QQ来举例吧,当我们手机端的QQ离线了,并且退出了QQ应用,但是这时候如果别人给我们发了信息,我们没有上线。服务器会将发送者发送的信息推送过来然后我们发布通知来显示通知我们的用户
原理简单阐述:
通过以上概述,我们基本了解我们需要一个独立进程的后台服务,在AndroidManifest
.xml中注册Service时,有一个android:process属性这个属性有2种情况,即为.和:两种,其中.代表为此服务开启一个全局的独立进程,如果以:开头则为此服务开启一个为此应用私有的独立进程
编码实现:
ServerPushService文件:
[java] view
plaincopy
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
public class ServerPushService extends Service{
//获取消息线程
private MessageThread messageThread = null;
//点击查看
private Intent messageIntent = null;
private PendingIntent messagePendingIntent = null;
//通知栏消息
private int messageNotificationID = 1000;
private Notification messageNotification = null;
private NotificationManager messageNotificationManager = null;
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
//初始化
messageNotification = new Notification();
messageNotification.icon = R.drawable.ic_launcher; //通知图片
messageNotification.tickerText = "新消息"; //通知标题
messageNotification.defaults = Notification.DEFAULT_SOUND;
messageNotificationManager = (NotificationManager) getSystemService(this.NOTIFICATION_SERVICE);
//点击查看
messageIntent = new Intent(this,MessageActivity.class);
messagePendingIntent = PendingIntent.getActivity(this, 0, messageIntent, 0);
//开启线程
MessageThread thread = new MessageThread();
thread.isRunning = true;
thread.start();
return super.onStartCommand(intent, flags, startId);
}
/***
* 从服务端获取消息
* @author zhanglei
*
*/
class MessageThread extends Thread{
//运行状态
public boolean isRunning = true;
@Override
public void run() {
while(isRunning){
try {
//休息10秒
Thread.sleep(10000);
if(getServerMessage().equals("yes")){
//设置消息内容和标题
messageNotification.setLatestEventInfo(ServerPushService.this, "您有新消息!", "这是一条新的测试消息", messagePendingIntent);
//发布消息
messageNotificationManager.notify(messageNotificationID, messageNotification);
//避免覆盖消息,采取ID自增
messageNotificationID++;
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
/***
* 模拟了服务端的消息。实际应用中应该去服务器拿到message
* @return
*/
public String getServerMessage(){
return "yes";
}
}
注册该service在一个单独的进程中
[html] view
plaincopy
<!-- 为此应用私有的独立进程 -->
<service
android:name="com.jay.serverpush.ServerPushService"
android:process=":message"
>
</service>
说明:该文件编写了一个service用于后台运行,在manifest里面将该service声明成progress为:开头的,这样在一个单独的进程里面运行,以实现在程序关闭之后达到进程不关闭的目的以此来实现离线推送的目的,编码中的注释很明确,扫描服务器、判断逻辑发布通知等,注释很明确在此不在阐述
开启服务的方式:
[java] view
plaincopy
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.startService(new Intent(this,ServerPushService.class));
}
this.startService(new Intent(this,ServerPushService.class));
通过这句话在第一次进入oncreate方法就开启了单独进程的服务
相关文章推荐
- cf 570 C. Replacement (暴力)
- cf 570B B. Simple Game(构造)
- Node.js开发入门—Angular简单示例
- extern与头文件(*.h)的区别和联系
- cf 570 A. Elections
- 程序员求职简历要点《经典》
- Linux设备模型理解
- 做私活的教训,一堆吐槽的,让我想起了那句话——学会拒绝别人
- 从别人到自己
- platform_device驱动模型用到的结构体
- Android 主要目录及文件简介
- linux驱动模型开发——linux platform总线机制讲解与实例开发
- ds18b20驱动第五天
- oralce高级查询
- 【LeetCode-面试算法经典-Java实现】【125-Valid Palindrome(回文字验证)】
- 【LeetCode-面试算法经典-Java实现】【121-Best Time to Buy and Sell Stock(最佳买卖股票的时间)】
- 【LeetCode-面试算法经典-Java实现】【120-Triangle(三角形)】
- iOS开发-Instruments性能调优
- C语言的局部变量和外部变量
- poj1195 二维线段树