• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Technical questions about Services

Hello everyone

I have few questions about services, i hope someone help me out

1-I know services work in background and they are invisible, my question is :
Do i need to make my App to be only a service or i can make a normal playable game that can also work as a service at the same time?

I need to have a normal game but need to have a service in background always running too. is it possible at all?


2- if its possible, when user close my game, what will happen to my service? it will keep running in background?


3-can i use "android draw on top of other apps" feature of Android from a service?


Any help is greatly appreciated.
Thanks
 
Welcome to Android Forums Q. I moved this to the Application Development forum to get some better eyes on it.:)

Good luck with your app.
 
1) Services can be bound to Activities or can run independently. When bound to an activity, they will die when the activity dies. When running on their own, they will continue to run until you call stopSelf(); OR until killed by the Android OS (if it needs RAM or CPU you can have your service killed at any time if your app is not in the foreground)

One important note. Services still operate on the main thread unless you specifically spawn a thread (or use an IntentService). They only do background work in the sense that they are not visible to the user. However, to perform background processing, it is recommended you not perform it on the main thread as it will slow your UI down. So either spawn a thread in your Service, or let IntentService handle the thread for you.

2) You can have long running services, but there is no garuntee that they will always be running. One trick people use is to periodically have the AlarmManager start your service if you want it to be running when your activity/app is not.

3) I don't know, but I would never grant this permission to a game. It's way too dangerous /invasive. Why do you need it?


Some homework:
Read up on AsyncTask, IntentService, bound vs unbound Services

hope that helps :)


http://developer.android.com/reference/android/app/IntentService.html


IntentService is a base class for Services that handle asynchronous requests (expressed as Intents) on demand. Clients send requests through startService(Intent) calls; the service is started as needed, handles each Intent in turn using a worker thread, and stops itself when it runs out of work.

This "work queue processor" pattern is commonly used to offload tasks from an application's main thread. The IntentService class exists to simplify this pattern and take care of the mechanics. To use it, extend IntentService and implement onHandleIntent(Intent). IntentService will receive the Intents, launch a worker thread, and stop the service as appropriate.

All requests are handled on a single worker thread -- they may take as long as necessary (and will not block the application's main loop), but only one request will be processed at a time.
 
Thank you man! it was really helpful. i appreciate that :)

....
3) I don't know, but I would never grant this permission to a game. It's way too dangerous /invasive. Why do you need it?

Actually i want to show some offers to users when they are browsing web and not playing my game, and they can collect points/credits for game if they tap on that offer. i was planing to show offers from service on top of other activities.

Now i understand its not possible, because my service will get killed when user exit game. unless i have a standalone service and thats almost impossible because how the hell can i convince user to install that service along my game, lol

Is there any better approach in your view?
 
A service is just something you define in your manifest, it is part of your app. if they install your app, they install the service.


However, overlaying advertising or other content over another application is a big no-no and could get you booted from Google Play for violating the developer TOS. Specifically interfering with the function of another app (the browser). This was the same reason Adblock Plus was booted from Google Play.

Personally I would never stand for that kind of invasive advertising, but that's just me. How you relate to your audience is up to you, all I can offer is my opinion and knowledge.

Hope that helps and good luck with your game :)
 
Back
Top Bottom