0

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?

KillerTheLord
  • 167
  • 1
  • 3
  • 9
  • You cannot issue `OnFling` events from a service, except perhaps to your own activities -- certainly not to other apps. Hence, I suggest that you edit your question to provide much more clarification about the sorts of "events" that you are trying to "fire". – CommonsWare Jul 25 '12 at 20:53
  • OnFling was just an example. OnFling is fired by Android System and can be listened by any running app and then the running app can perform some task. What I want to do is that I fire a XYZ event and any app can listen for this event and perform some task. I hope that makes it a bit more clear. – KillerTheLord Jul 26 '12 at 06:03

1 Answers1

1

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.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • The app I am trying to design will be used to handling inputs for System(like we have touchscreen now suppose we add another medium of input) so potentially the BackgroundService should never be closed and let's assume it is never closed by Android(we can deal with it later). I thought that something dealing with firing of events will have to be done. But as you have explained that Intents can do the similar thing. Can you please elaborate on how this can be achieved? – KillerTheLord Jul 26 '12 at 17:54
  • @KillerTheLord: "will be used to handling inputs for System" -- that will work best as part of a custom ROM with a custom Android build with the appropriate Linux device driver. "potentially the BackgroundService should never be closed and let's assume it is never closed by Android" -- that is not possible except as part of a custom ROM with a custom Android build. "Can you please elaborate on how this can be achieved?" -- at most, this is used for occasional events, such as the system broadcasting `ACTION_CAMERA_BUTTON` or `ACTION_MEDIA_BUTTON`. This is not suitable for continuous events. – CommonsWare Jul 26 '12 at 18:56
  • First of all Thanx.Yes the I have the driver on low level(C level and all the required stuff) and about the Custom ROM I agree with you to stop Android from killing the service a custom ROM will be needed. How about firing events? If a background service fires an event can it be listened by any process? As I mentioned above http://www.mikenimer.com/?p=671 this code fires an event and has code for listening to this event, can this listening be done by any app running on Android? – KillerTheLord Jul 26 '12 at 20:26
  • @KillerTheLord: "can this listening be done by any app running on Android?" -- not using that code, it cannot. That is purely within a process. Again, for an input device with just a button or two, using broadcast `Intents` [and a `BroadcastReceiver`](http://developer.android.com/reference/android/content/BroadcastReceiver.html) can work, but that approach is unsuitable for frequent events. – CommonsWare Jul 26 '12 at 20:32
  • Then what approach should be taken for frequent events? Android also fires a lot of events when screen is touched or a fling happens how is this achieved? – KillerTheLord Jul 26 '12 at 21:25
  • @KillerTheLord: "Android also fires a lot of events when screen is touched or a fling happens how is this achieved?" -- that is handled at a low level by the OS. The OS can do things efficiently and effectively that an app cannot. What you want will work best as part of a custom ROM with a custom Android build with the appropriate Linux device driver. – CommonsWare Jul 26 '12 at 21:28
  • Does android spawn a new Gesture Recognizing and reporting module everytime a app launches? – KillerTheLord Jul 26 '12 at 21:34
  • @KillerTheLord: "Does android spawn a new Gesture Recognizing and reporting module everytime a app launches?" -- I have no idea. StackOverflow is principally a resource for developing apps with the Android SDK, not for OS-level questions. – CommonsWare Jul 26 '12 at 21:35
  • I have to say I found this answer a little condescending. – Basic Apr 20 '14 at 00:55