I want to create a service that runs in the background,
service upload files to a server .
meanwhile you can close the app .
the service continues to work ,
at the end and, if the app is open must update the UI of the app .
as you update the UI of the app if it's open ?
I have to make a check of the type : if app open ui update ?and how to do this ?
Use an event bus, such as greenrobot's EventBus or LocalBroadcastManager. The service can post a message to the bus, announcing any state changes. The activity — if it exists — can subscribe for messages from the bus and update the UI upon receipt of this state-change message.
I have sample apps demonstrating this for greenrobot's EventBus and for LocalBroadcastManager. I also have one for Square's Otto, though it is a bit more complicated due to threading issues.
Related
I want to create a notification in my app , when data downloaded from API changes.
Example:
I want to create a notification when a Twitch streamer start
streaming.
I want to create a notification when a Twitter user posts a new
tweet.
I want to create a notification when a YouTube user uploads a new
video.
How can I do this? Periodically download data and check changes? How do I create this task if the application is destroyed or the device is restarted? How do I do it so I do not discharge the device's battery too much?
I know how to download data and how create a notification, but I don't now how to create the described task.
the best practice here is using push notification.
all you have to do is registering you app with one of the well known providers of the service like FireBase Cloud Messaging and then configure your app's server to send notifications to the Cloud Messaging service which will handle the delivering of your notification
and you won't have to worry about battery usage because they use google play services to keep a connection open and they even display the notification themselves.
they provide many other features you can use and they are optimized in the best possible way.
you don't have to reinvent the wheel
I have an REST-endpoint(A) that periodically sends data to an REST-endpoint (B) via POST.
I'm working on B. This endpoint has to subscribe at A and then show the latest data in a component (I would prefer a swing GUI).
The subscribtion should be triggered by the user.
So I'm thinking of a way to start a background process for my REST service that holds the GUI-component for subscribtion and show the data.
But WebServices don't have an "entry-point" for starting services.How can I start processes that run while the stateless requests are beeing processed?
What is the common way to design such problems/tasks?
I have an application where I want to upload some data as soon as the wifi is turned-on even if the application is not running. I think this can be done using android background services. Please help me uploading the data using background services when the app is not running or suggest me some other way to achieve this.
Thanks in advance!!
To be more eloborate, implement following
Create a Started service (service which is started using startService() instead of bindService().
Implement onStartCommand() callback of this service. In this callback method check if there is any data to be uploaded (if this data is created by an activity, it is wise to use a DB table to queue the data to be uploaded and service picks up data from that DB). Check WIFI connectivity and if available upload the data.
Now you need to trigger this service whenever WIFI is available. Here you 2 ways to imeplemnt
Method 1 : register in your application manifest file to be notified of WIFI connectivity changes refer this link . registered broadcast receiver would be called when WIFI state changes. from the broadcast receiver start your service if WIFI is available
Method 2 : using AlarmManager launch you service periodically (say every 10 minutes). refer this link. This method would be beneficial if data to be uploaded gets generated once in a while.
Hope this is helpful. Let me know if you have any questions.
You must use an unbounded service, who is completely independent for the activity (for more details check this ). After starting service monitor your internet connection using ConnectivityManager. Use this like example. Have Fun.
My main app starts 2 services Input-service and Save-service. My Input-service is generating input which i am able to show in my Main app and my both services are starting successfully. I am facing problem in calling one service from another. Means how can i send data of Input-service to Save-service. Is there any example which shows communication between two services. Or can anyone help me regarding this.
Why not use BroadcastReceivers? Register them in onCreate, de-register in onDestroy and send data through intents ...
In my Android app I have a service that is started via AlarmManager. I want to have an activity that will display some progress/status info for this service. Since I already have 90% of the code done, and the service is working fine as-is, I'm wondering what is the easiest way (if possible) to just have the service report progress info to the activity, only if the activity is running.
I saw "binding" but I think that might only work if I want to start the service from my activity and bind to it. Possibly a broadcast receiver?
I would like to be able to report status updates fairly often, maybe even several per second in some cases, so I don't want to do something like use PreferenceManager.
Does my service and activity run in the same application scope? If so, maybe I can create a singleton static object to share status info? The status info can just be a string, that is all I really need.
You can use a Broadcast to "emit" data from your Service. Then in your Activity have a BroadcastReceiver for this same event. When no Activities are running that respond to that broadcast then nothing will receive them.