I am developing an Android Application that is supposed to auto-upload images as soon as they appear in the Media Store. For this, I have implemented a Content Observer which is registered inside a Foreground Service to constantly monitor the change in Media Store and there is an upload Service which is triggered onChange() of Content Observer.
I am considering not using the Foreground Service as it wastes a lot of resources, what would be the best way to implement this?
P.S. - I have considered using scheduled Service too, but that wouldn't be of that help.
Thank You.
Related
I need some help regarding fetch bulk data through background service and store it into android local storage,
I don't want to use pagination, I mean when the app activity is launch that time background service is call and its running until fetching is not completed(50k data row).
Thanks in advance.
:-)
This sounds like something that Android's Services are perfect for. They fire off in the background and are designed to do this exact thing.
Essentially, you just subclass the service and write your download logic in that. Once the download is complete, you can then, depending on how you created your service, notify your app of the result. If the service was bound, you can have the activity that bound to the service register a listener. If the service was started (an Android term for started without an explicit bound recipient) then you can fire of an Intent indicating the completion of the task.
I've just been reading about adding a service to my application, and since 7/8 there are now service restrictions to improve phone performance.
I've seen that the recommended approach is to use a job scheduler, but will that not just periodically start a new listener if I did that?
Basically I update my database, and using a snapshot listener I want to update my user in real time. When the app is closed, I'd like to send a notification.
My issues (if I'm correct) are that constantly making a new Firestore request will eat through my request allowance.
Also, if its a job scheduler it won't quite be real time?
I've read that you can use a foreground service, but this doesn't really seem like that task that needs a permanent notification and would annoy the user.
Anyone got any tips on how I'd implement this?
Thanks
Using a listener after an android application is closed
You can use a listener after an android application is closed, by not removing it. Once you are using a listener, you also need to remove it according to the life-cycle of your activity. But this will work only for a shot period of time because Android will stop your service if the app is not in the foreground. It does this to save resources when the app isn't being used. It also might stop your app from doing any networking, or even kill the app process completely. There's nothing you can do to prevent this, other than making it a foreground service, as you already mentioned.
A foreground service is probably not the best thing to do for your case, nor is it the best thing for your users. Read more about limitations on background services.
My recommendation is to use Firebase Cloud Messaging to notify your app when something has changed that it might be interested in. So your users will recieve notification even if they will keep their app closed.
I have some doubts about the properly implementation of services and broadcastreceivers.
I have made an app in which there are novelties. Some novelties are important, so in the DB they have a field in which they store if they are important or not. If they are, the app should check if the last novelty seen by the user is the latest one. This should trigger a notification if there are important novelties that were not seen by the user.
I know how to show notifications in Android, and I have a Method in my Web Service which shows if the user has novelties to read or not. I just need to know how to make my app consume this at random times and without being opened (just like Whatsapp does).
I have read the BroadcastReceiver and Services documentation, but I don't know how to do this in an efficient way.
Do I make a BroadcastReceiver to call a Service at the Phone's Boot? And make this Service to check at random amounts of time?
Thanks a lot!
What you want is a foreground service with a BroadcastReceiver that starts the service on boot. View this answer and make the required changes to use startForeground() instead of startService.
I'm making an app in which i want a process always run in background e.g in facebook we got a notification and it will notify in our app. Kindly text.
Try Services and BroadcastReceiver to do this.
guess you need to explain the function you want in a detailed way.
Usually we will use a Service
or a Intent Service to do what you mentioned. If you want to detect a change in your application or the phone, you may register a broadcast receiver or a Content observer in the service depends on the function and effect you want.
But bare in mind that, service do not have UI so you should avoid to interact with users while using Services.
From my understanding, service can do most of the tasks you want. One example is play music. You can run a service in foreground if you want to ensure that the services is harder to be killed by the system when memory is low.
Intent service is used to handle asynchronous requests (expressed as Intents) on demand one followed by another. One good example is downloading a file
For Content observer, you will observe a content and the observer will react to if when there is any change from the "OnChange" method.
For broadcast receiver, usually we will use it to observe something happen, for example, screen unlocked, boot completed, sms received.
It really depends on your needs in order to decide what kind of services you want. Please explain in details in order to get more information.
I've been trying to make a widget to my music app.
The app has a service that handles the mediaplayer so that i can "minimize" the app and yet continue listening to music.
I know that i need to update the widget from the service, but all the examples i've found, wants me to start a new service on each update, and that's not what i want.
Any help is appreciated!
Thanks in advance
You can do it without the service too. The service is recommended because if your updation takes too much time, android may close it and give Application Not Responding (ANR) error message. These are the cases like when you send a web-service request. Otherwise, you can use your custom AppWidgetProvider to update your widget (without making a service), using RemoteViews(the same way you do in the service). For more on how to do that refer this