简体   繁体   English

在后台运行的android服务

[英]android service running in the background

I would like to make a service that runs in the background and monitors the quality. 我想提供一个在后台运行并监视质量的服务。 I have thought of a separate service that exposes an aidl interface to an activity, the aidl would have some monitor related functions that an activity can call. 我想到了一个单独的服务,该服务向活动公开了一个辅助接口,辅助将具有活动可以调用的一些与监视器相关的功能。 To give an example of how my service should work think of the ebuddy application that runs in the background and waits for incoming calls (I believe this is done by a service). 为了举例说明我的服务如何工作,请考虑运行在后台并等待传入​​呼叫的​​ebuddy应用程序(我相信这是由服务完成的)。

  1. Is this a correct approuch to my problem? 这是解决我问题的正确方法吗?
  2. I am currently able to create my service and use the aidl interface but when i close the activity(when I press the back button) the service is destroyed too! 我目前能够创建我的服务并使用aidl界面,但是当我关闭活动(当我按下“后退”按钮时)该服务也被破坏了! How to avoid that? 如何避免呢?
  3. I would like to make a persistent notification much like ebuddy, how to do it? 我想像ebuddy一样发出持久通知,该怎么做? I have tried with notification manager but no luck, I have only managed to add at the notification section. 我尝试过使用通知管理器,但是没有运气,我只设法在通知部分添加了内容。

Ok so to make the notification you simply have to do 好的,只需发出通知即可

 mNotification = new Notification( icon, text, when );
 mNotification.flags |= Notification.FLAG_ONGOING_EVENT;

and notify using notification manager. 并使用通知管理器进行通知。

If someone needs to run something on the background (not a background thread) then you have to extend service class,you have to bind to the service and get a stub, you can then call methods that the stub exposes through it's aidl file. 如果有人需要在后台运行某些内容(而不是后台线程),则必须扩展服务类,必须绑定到该服务并获取一个存根,然后可以调用该存根通过其aidl文件公开的方法。 for more information you can check out the Remote service example that comes with the SDK, its located in:/src/com.example.android.apis/app 有关更多信息,您可以查看SDK随附的远程服务示例,该示例位于:/src/com.example.android.apis/app

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM