I am having some design techniques about How shall I schedule a code to retrieve the weather info?
Should I use alarms to retrieve the weather each 10 minutes?
And do I need to run a service for this? Or just put the code in the Broadcastreceiver and start when the alarm fired?
This is a good question. Yes, you will need to put this code in either a service or broadcastreceiver because when and activity loses focus (meaning the user is using a different app or the phone is asleep) they pause and/or close. However, i have no experience with either Services or Broadcastrecievers, so that is as much as i can tell you.
Related
In my application, I would like to send user location to server continuously for every 5 minuets,.. I don't know much about services.. I would appreciate if someone can help me.. Please give me some references to create service and intentservice examples. Thanks
You should use a Service for that. Use START_STICKY to ensure it doesn't get killed permanently and/or set an Interval starting the Service using AlarmManager.
However you probably should reconsider doing this at all. No one wants an app which uses RAM and network bandwidth all the time and leaks personal data.
I have a service which reads data from a Socket connection and during peak hours. The frequency of the changes is every five seconds.
I have two options in mind.
Update the List, using a BroadCastReceiver.
Create a new Thread in the Activity where I created the List, read a static variable every five seconds, and when the change occurs, change the static variable from the Service.
Which option is more efficient and what are the pros and cons of both?
If you think there is a better option, please let me know.
Thank you.
I would use the BroadcastReceiver (with the LocalBroadcastManager) option. I would recommend you to make your model class implement Parcelable. I would definitely avoid the solution 2
BroadCastReceiver will be efficient for your case.
It will help you to update your ListView when it receives new data. So you don't need to check for update in every 5 seconds.
It will increase the performance of your App. On the other hand in case of very frequent updates it will not wait for the given interval.
So go with broadcastreceicer.
Here is the pros that you will get from BroadcastReceiver :
A Broadcast receiver wakes your application up, the inline code works only when your application is running.
For example if you want your application to be notified of an incoming call, even if your app is not running, you use a broadcast receiver.
If your application is playing audio, and you want to stop the music on an incoming call, you use the inline code.
With broadcast receiver you can't uptade the UI, you can see WHY here, if you talk about other thing, explain us please... In that post you can see a other method to update UI from intent service, and I think is the best way to update UI from intent service because you only update UI when you need it and you don't overcharge the app process .
Finally if you want to update your UI every 5 seconds do it but with a little difference... I would do update every 5 seconds my UI if my app is in first plane if isn't it then update my app when app comes to first plane in OnResume() method because you don't use extra memory from device when my app is in background...
Tell me if I helped you and good programming!
I want to fetch the current Location of the user in background. I know how it can be done using Service in Android. But I would like to know is there any possibilities to get user Location without running service. Like using BroadcastReceiver or anything? I'm just trying to avoid running Service to fetch Location.
For Example I referred this link. But I couldn't follow how to do like this.
Any help will be appreciated. Correct me in case I'm asking anything wrong.
EDIT: Oh, I think in the above link he is using Service to get the Location. So I think it's not possible to get location in background without running a Service. Still suggestions are welcomed.
You Should Implement a BroadcastReceiver that receives alarm every 2 hours. That alarm is Set by Your Activity.
When the alarm is triggered by your System, Broadcast Receiver receives it, Executes Your Process in BroadCastReceiver.
This is what i can think of.
I am creating a notification app, which will alert user after they set a reminder notification. My current implementation logic is:
Create a Service, which starts running in background when user opens their app.
In onCreate() method of service, I am implementing a Timer task which will repeat after 5000ms interval and will call a method, which will check all reminders set by user and notify user if any reminder is set for current time.
I use broadcast receiver to run the service on Boot_Completed event, if in case user turns off their phone.
This implementation is working good, I have faced no issues with it, but my concern is that is this a good practice? Keeping in mind that service running and checking every 5 secs will consume battery. Also if user turns on Stamina Mode, Power saving mode or any such mode, will OS kill my service. Is there anything I can do to give priority to my Service not to be killed.
If there is any other more efficient way to implement this sort of task, I want to implement that in my project.
Looking forward for suggestions.
Much Appreciated.
best approach is wakeful intent service with alarm receiver as mentioned here
https://github.com/commonsguy/cwac-wakeful
all good but use AlarmManager.setRepeating() as timer. the intent come even if your app killed.
I want to write reminder. What i need to use? Make service app or just standart app runing in background or another way?
Thanks for replys!
What I really liked about this question is you asked about the idea for the app that you want to implement. You didn't ask for code.
I would suggest that you should make an app which should have a broadcast receiver, but still it should have service that runs in background.
The service will check the current time with your reminder time. A broadcast receiver is required to listen to startup broadcast, because you need to start your app as soon as your handset starts.
Have a look at this.
Have a look at the AlarmManager. It allows you to schedule your application to be run at some point in the future.