Android 通过Service单独进程模仿离线推送 Server Push

来源:互联网 发布:redshift渲染器mac 编辑:程序博客网 时间:2024/06/05 17:43

概述:

         首先简单阐述一下我对于消息推送的理解,这里拿QQ来举例吧,当我们手机端的QQ离线了,并且退出了QQ应用,但是这时候如果别人给我们发了信息,我们没有上线。服务器会将发送者发送的信息推送过来然后我们发布通知来显示通知我们的用户

 

 

原理简单阐述:

         通过以上概述,我们基本了解我们需要一个独立进程的后台服务,在AndroidManifest

.xml中注册Service时,有一个android:process属性这个属性有2种情况,即为.和:两种,其中.代表为此服务开启一个全局的独立进程,如果以:开头则为此服务开启一个为此应用私有的独立进程

 

 

编码实现:

ServerPushService文件:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
<strong>    importandroid.app.Notification;  
    importandroid.app.NotificationManager;  
    importandroid.app.PendingIntent;  
    importandroid.app.Service;  
    importandroid.content.Intent;  
    importandroid.os.IBinder;  
       
    publicclass ServerPushService extendsService{  
        //获取消息线程  
        privateMessageThread messageThread = null;  
        //点击查看  
        privateIntent messageIntent = null;  
        privatePendingIntent messagePendingIntent = null;  
        //通知栏消息  
        privateint messageNotificationID = 1000;  
        privateNotification messageNotification = null;  
        privateNotificationManager messageNotificationManager = null;  
        @Override 
        publicIBinder onBind(Intent intent) {  
            returnnull;  
        }  
        @Override 
        publicint onStartCommand(Intent intent, intflags, intstartId) {  
            //初始化  
            messageNotification = newNotification();  
            messageNotification.icon = R.drawable.ic_launcher;  //通知图片  
            messageNotification.tickerText = "新消息";         //通知标题  
            messageNotification.defaults = Notification.DEFAULT_SOUND;  
            messageNotificationManager = (NotificationManager) getSystemService(this.NOTIFICATION_SERVICE);  
            //点击查看  
            messageIntent = newIntent(this,MessageActivity.class);  
            messagePendingIntent = PendingIntent.getActivity(this0, messageIntent, 0);  
            //开启线程  
            MessageThread thread = newMessageThread();  
            thread.isRunning = true;  
            thread.start();  
            returnsuper.onStartCommand(intent, flags, startId);  
        }  
       
        /*** 
         * 从服务端获取消息 
         * @author zhanglei 
         
         */ 
        classMessageThread extendsThread{  
            //运行状态  
            publicboolean isRunning = true;  
            @Override 
            publicvoid 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 
         */ 
        publicString getServerMessage(){  
            return"yes";  
        }  
    }  </strong>

注册该service在一个单独的进程中

?
1
2
3
4
5
6
<strong>    <!-- 为此应用私有的独立进程 --> 
            <service  
                    android:name="com.jay.serverpush.ServerPushService" 
                    android:process=":message" 
                >  
    </service>  </strong>

说明:该文件编写了一个service用于后台运行,在manifest里面将该service声明成progress为:开头的,这样在一个单独的进程里面运行,以实现在程序关闭之后达到进程不关闭的目的以此来实现离线推送的目的,编码中的注释很明确,扫描服务器、判断逻辑发布通知等,注释很明确在此不在阐述


开启服务的方式:

?
1
2
3
4
5
6
7
8
9
<strong>    @Override 
        protectedvoid onCreate(Bundle savedInstanceState) {  
            super.onCreate(savedInstanceState);  
            setContentView(R.layout.activity_main);  
            this.startService(newIntent(this,ServerPushService.class));  
        }  
       
       
    this.startService(newIntent(this,ServerPushService.class));  </strong>

通过这句话在第一次进入oncreate方法就开启了单独进程的服务

 

源码下载:

http://pan.baidu.com/share/link?shareid=457896&uk=1997312776

0 0
原创粉丝点击