简体   繁体   中英

Background Service Android with Event Trigger

I want to design a service for Android which can run in the background at all times and can fire events which can be then handled by apps running on the phone(like the OnFling Event can be listened by any app). How can this be accomplished on Android?

I have read the http://www.mikenimer.com/?p=671 and http://android.kgmoney.net/2010/05/08/creating-a-simple-android-service-for-background-processing/

Can someone guide me on this?

It is not possible for you to have a service "which can run in the background at all times". The user can and will get rid of your service whenever the user wants to, and Android itself can and will get rid of your service whenever its algorithms decide to. While you can use stuff like startForeground() to slow down Android getting rid of your service, it will not run forever, and the user has free will to nuke you from orbit via the Settings app or any number of third-party task managers.

Moreover, for the vast majority of apps, having a service that tries to run forever is an inferior design. There are reasons why users get rid of such services -- they take up RAM that could be devoted to apps that the user values more at present. Poorly-written services can also consume more CPU, battery, bandwidth, etc. than is warranted. Since you declined to actually explain what it is you are trying to accomplish, I can neither tell you whether users might think your service is justified or suggest alternative implementations that would avoid the "everlasting service".

A service can certainly send broadcast Intents that other apps could pick up if they so choose. Whether that fits your one demand ("I fire a XYZ event and any app can listen for this event and perform some task"), I cannot say.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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