So I'm creating app that includes ForegoundService with is sending some data to the backend.
I've already made service part. In activity i have two buttons, like start and stop.
Pushing start is starting the service and pushing stop needs to stop him. The problem is that when I reopen the app (from icon, or from notification) the service reference is null and I can't stop service working.
Is it possible to reopen that exact activity event when app was already closed?
I was thinking about making service singleton, but i wish there is some proper way to do this.
Related
I'm trying to write simple app to be location aware.
The thing is I wrote location requests to be received directly in activity, but that brings up the problem, because I wanna get the results even if the activity is in background. So I know that I need to have foreground service running to be able to do any processing whenever my activity is in background.
I need this service to be started from this activity (like button click) and be then visible across activities and when my app goes into background.
So my idea is as follows.
Start the service from activity on button press. I guess this service should be sticky. And then I should bind to this service from that activity if I want to receive location data. When app goes into background I should then promote that service into foreground service so my app stays in background and Oreo restrictions don't apply.
Is my thought process fine? Or are there better ways to achieve my requirements?Like a broadcast receiver maybe.
Thanks in advance.
I still don't get how the Application (not Activity) lifecycle is,
It is pretty obvius that Application's onCreate method is called when you start the GUI.
But, is it started in ANY or ALL of the following cases?
App Widget is visible
Broadcast receiver receives something
Push notification arrives to device and show message
Push notification is clicked after the app has been closed (like from Notification Center)
Service is started
And how long will the Application process will be kept alive?
Right now I have a problem that I see that the application (process) is restarted after I close/kill the app. However there is nothing implemented so to have this behavior.
But, is it started in ANY or ALL of the following cases?
Your Application instance is created as part of starting up your process.
App Widget is visible
Simply being visible has nothing to do with your app and its process. Your app and its process will get involved for populating the app widget, when it is created and when it is updated. If, for example, updatePeriodMillis is triggering updates, and when the time comes around, you do not have a process, then an Application instance is created as part of starting up the process, before the AppWidgetProvider is called with onUpdate().
Broadcast receiver receives something
If your process already existed, your Application instance already existed. If your process did not exist, then an Application instance is created as part of starting up the process, before the BroadcastReceiver is called with onReceive().
Push notification arrives to device and show message
If you mean GCM, since this comes in as a broadcast, see above.
Push notification is clicked after the app has been closed
I have no idea what you mean by this.
Service is started
If your code is starting the service, then your process was already running and you already have an Application. If some other process is starting your service, and your process is not running, then an Application is created, before the Service, as part of creating your process.
And how long will the Application process will be kept alive?
If by "Application process", you mean "process", your process will be around for somewhere between a millisecond and a millennium, roughly speaking. It will be around until Android terminates it to free up system RAM for other apps, or until something specifically gets rid of it (e.g., "task killer", force-stop in Settings).
Application onCreate() is called when the application was dead, and it was started.
For example:
You start your app when it is not running (first time running it in a session or you start it after force stopping it)
You quit every activity for a long time (it is not killed immediately!) and Android decides to close your app and you restart it
You put the app in background, load Chrome, load some stuff, then Android decides that your app should perish and murders it (process com.example.acme.helloworld has died.) and the application itself is murdered along with every static variable, and your app is recreated from scratch but your activities load from the Activity Stack and the onSaveInstanceState -bundle
Considering the push notification receiver service is most likely in a different process, I would assume that can also start your application instance from scratch.
As the title, I need that an Android Service - that periodically calls a web service - does the job only when the app is closed. When the app is running (don't matter if in background or foreground) the Service must be stopped.
How I can achieve this?
Thank you.
If you have just one Activity as an entry point for your app it would be very easy. This Activity will be the destroyed at end when user leave your app, so overriding onDestroy() and launching Service from there will do the job. When the same Activity starts you can request stopping your Service inside onCreate() of the Activity.
I am implementing a Service which will start when MainActivity starts. This service checks for and maintains a network connection.
I have not bound the Service to the Activity because I want the service to continue running even if the activity isn't available. The Service will receive messages from the server that it is connected to.
I am struggling to choose the best logic to do the following when the service receives a message.
Check if MainActivity is currently open and in front of the user
If it is call some methods in the activity to interact with the UI
If there is no activity update the notification are.
My question is;
How do I correctly check if the activity is running in the UI from my service? I know that I could bind the service but I wouldn't want to unbind it if the activity is closed. Would that be a problem?
I will somehow need to send messages from the service to the activity. Given the above scenario what would be the best way to do this?
Do it differently.
If your Service does not run in a separate process from your Activities, then your Service can provide a synchronized (multithread-safe) list of messages via a subcalssed Application object, where your Activity can look it up. However, this would only be best if the polling occurs on certain other events.
If you want to sort of "push" the message to your Activity, your Activity should register with your service upon finding out that it runs, not the other way round. In this scenario, your Activity should implement an interface through which the Service can notify your Activity of new messages in a Thread-safe way.
Of course you could also go straightforward and simply post notifications which open an Activity, but as I understood it, you want to provide a more smooth integration.
An idea would be to let your Service send status bar notifications when a new update is available.
Implement the notification such that when clicked, to open MainActivity.
Then, when the user will click on the notification, if the MainActivity is already running then it will be brought to front, otherwise it will be started.
So this basically eliminates the need to check if MainActivity is currently open in front of the user (which I see as a bad practice).
How do I keep a service running, when all activities of an application are viewable. and close the service only when I leave the app?
Is there a way to start a service in one activity of an app and stop that same service in another activity of that same application?
I don`t want that service to continue running when the user leaves the application.
There is really no concept of 'application' and leaving it. What happens if open a link from one of your activities, launch the browser to view it, then come back via the back button? Did you really leave the app?
What does your service do? Do activities bind to it? If so, it will be automatically shut down after the last client unbinds. If not, it should shut itself when it has finished doing it's work (Cf. IntentService). If it doesn't fit either of those patterns, maybe you don't need a service at all, just some background thread(s)?
Edit (based on comments below):
For a service running a media player, the usual way is to have an ongoing notification for the service that lets the user bring up an activity to control the service. Or have buttons on the notification in JB to achieve something similar. Additionally, if you make the service a foreground one, that will give it higher priority and it is less likely to be killed if resources are low.