如何显示来自后台服务的通知? [英] How to show notification from background service?

查看:22
本文介绍了如何显示来自后台服务的通知?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在处理的项目有两个不同的应用程序,一个是服务器,另一个是客户端.服务器应用程序有 1 个服务类,它在 onStartCommand() 函数中启动服务器线程.服务器线程的类在服务类本身中定义.每当服务器从客户端接收任何数据时,它都会存储在数据库中.所以问题是,我想通过通知或吐司消息尽可能通知用户新数据已经到达.还告诉我应该在哪里编写通知代码,是应该在服务类或线程类中编写,还是在启动服务器服务的 firstActivity 类中编写.谢谢.

The project I am working on has two different apps, 1 is server and other is client. Server app has 1 service class, which initiates server thread in onStartCommand() function. Class for server thread is defined in service class itself. Whenever server receives any data from client it stores in the database. So problem is, I want to notify user that new data has been arrived through notification or toast message whichever possible. Also tell me where should I write code for notification, whether I should write in service class or thread class or firstActivity class from which i am starting server service. Thank you.

推荐答案

我读错了吗?您的服务器应用和客户端应用都在同一台 Android 设备上?

Am I reading this incorrectly? Both your server app and your client app are both on the same Android device?

无论如何,请查看 ApiDemos 以获取通过服务进行通知的代码.

In any case, take a look at the ApiDemos for code for notifications through a service.

而且由于您可能要处理远程服务器(而不是本地服务器)以及与网络相关的所有延迟问题,我认为您最好将您的服务包装在 AsyncTask 中.

And since you're probably going to be dealing with a remote server (and not a local one) and all the latency issues associated with the network, I think you better wrap your service in an AsyncTask.

同时了解广播接收器,注册的广播接收器可以成为触发服务/活动的好方法,而无需无限循环地进行自己的服务轮询.

Also learn about Broadcast Receivers, registered Broadcast Receivers can be great way to trigger Services/Activities without the need to have your own Service polling in a loop indefinitely.

这篇关于如何显示来自后台服务的通知?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆