当前位置:首页 > 嵌入式 > 嵌入式软件
[导读] 概述:首先简单阐述一下我对于消息推送的理解,这里拿QQ来举例吧,当我们手机端的QQ离线了,并且退出了QQ应用,但是这时候如果别人给我们发了信息,我们没有上线。服务器会

 概述:

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

原理简单阐述:

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

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

编码实现:

ServerPushService文件:

01 <strong>import android.app.Notification; 
02     import android.app.NotificationManager; 
03     import android.app.PendingIntent; 
04     import android.app.Service; 
05     import android.content.Intent; 
06     import android.os.IBinder; 
07        
08     public class ServerPushService extends Service{ 
09         //获取消息线程 
10         private MessageThread messageThread = null
11         //点击查看 
12         private Intent messageIntent = null
13         private PendingIntent messagePendingIntent = null
14         //通知栏消息 
15         private int messageNotificationID = 1000
16         private Notification messageNotification = null
17         private NotificationManager messageNotificationManager = null
18         @Override 
19         public IBinder onBind(Intent intent) { 
20             return null
21         
22         @Override 
23         public int onStartCommand(Intent intent, int flags, int startId) { 
24             //初始化 
25             messageNotification = new Notification(); 
26             messageNotification.icon = R.drawable.ic_launcher;  //通知图片 
27             messageNotification.tickerText = "新消息";         //通知标题 
28             messageNotification.defaults = Notification.DEFAULT_SOUND; 
29             messageNotificationManager = (NotificationManager) getSystemService(this.NOTIFICATION_SERVICE); 
30             //点击查看 
31             messageIntent = new Intent(this,MessageActivity.class); 
32             messagePendingIntent = PendingIntent.getActivity(this0, messageIntent, 0); 
33             //开启线程 
34             MessageThread thread = new MessageThread(); 
35             thread.isRunning = true
36             thread.start(); 
37             return super.onStartCommand(intent, flags, startId); 
38         
39        
40         /***
41          * 从服务端获取消息
42          * @author zhanglei
43          *
44          */ 
45         class MessageThread extends Thread{ 
46             //运行状态 
47             public boolean isRunning = true
48             @Override 
49             public void run() { 
50                 while(isRunning){ 
51                     try 
52                         //休息10秒 
53                         Thread.sleep(10000); 
54                         if(getServerMessage().equals("yes")){ 
55                             //设置消息内容和标题 
56                             messageNotification.setLatestEventInfo(ServerPushService.this"您有新消息!""这是一条新的测试消息", messagePendingIntent); 
57                             //发布消息 
58                             messageNotificationManager.notify(messageNotificationID, messageNotification); 
59                             //避免覆盖消息,采取ID自增 
60                             messageNotificationID++; 
61                         
62                     catch (Exception e) { 
63                         e.printStackTrace(); 
64                     
65                 }    
66             
67         
68         /***
69          * 模拟了服务端的消息。实际应用中应该去服务器拿到message
70          * @return
71          */ 
72         public String getServerMessage(){ 
73             return "yes"
74         
75     }  </strong>

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

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

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

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

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

本站声明: 本文章由作者或相关机构授权发布,目的在于传递更多信息,并不代表本站赞同其观点,本站亦不保证或承诺内容真实性等。需要转载请联系该专栏作者,如若文章内容侵犯您的权益,请及时联系本站删除。
换一批
延伸阅读

一个线程只能属于一个进程,而一个进程可以有多个线程,线程是进程的一部分,就像工人是工厂的一部分。资源是分配给进程的,同一进程的所有线程共享该进程的全部资源,就像工厂里的工人共享工厂的设备和场地。处理机(CPU)则是分给线...

关键字: 进程 线程

DXC内部新成立的Boomi卓越中心(COE),将成为跨行业客户的创新枢纽 DXC与Boomi携手助力客户整合AI智能体,为规模化应用代理式AI做好准备 弗吉尼亚州阿什伯恩2025年8月19日 /美通社/ --名列财...

关键字: 进程 AI BSP 自动化

北京2025年8月15日 /美通社/ -- 亚马逊云科技日前宣布,Amazon DocumentDB Serverless已正式可用,这是Amazon DocumentDB(兼容MongoDB)的一种全新配置,能够根据应...

关键字: DOCUMENT SERVER 亚马逊 数据库管理

助力企业更快、更轻松地迁移VMware工作负载,无需重构应用或变更架构 北京2025年8月11日 /美通社/ -- 亚马逊云科技日前宣布Amazon Elastic VMware Service(Amazon EVS)...

关键字: SERVICE VMWARE 亚马逊 TI

其他电脑(比如安卓手机/平板电脑)的屏幕坏了,你可能想在安排维修之前紧急访问一些东西。你可以使用android的USB OTG功能(是的,几乎每个android都支持这个功能,你可以将鼠标和键盘连接到它)。

关键字: USB 鼠标 Android 树莓派

上海 2025年7月9日 /美通社/ -- 据Gartner最新研究显示,到2025年全球将有75%的企业将生成式AI纳入核心业务流程。这其中,人力资源领域因其天...

关键字: AI BSP 进程 CHINA

天津2025年6月25日 /美通社/ -- 2025年6月24日至26日,世界经济论坛第十六届新领军者年会(夏季达沃斯论坛)在天津举行。本届年会以"新时代企业家精神"为主题,汇聚来自全球1700余位政...

关键字: 人工智能 进程 BSP 微软

北京 2025年6月10日 /美通社/ -- 亚马逊云科技日前宣布,推出开源的Amazon Serverless Model Context Protocol (MCP) Server工具。该工具将AI辅助功...

关键字: SERVER MCP 亚马逊 开源工具

新加坡2025年6月4日 /美通社/ -- THE GROWHUB LIMITED(简称"The GrowHub"或"该公司")是一家总部位于新加坡的公司,利用区块链技术,来提升整...

关键字: 人工智能 进程 AI 供应链

南京 2025年5月30日 /美通社/ -- 日前,国际独立第三方检测、检验和认证机构德国莱茵TÜV大中华区(以下简称"TÜV莱茵"...

关键字: 协作机器人 进程 测试 BSP
关闭