I am building a game and now I have to send user scores on every first of the month to the server .
I have multiple options like
Timer
ScheduledThreadPoolExecutor
Service
BroadcastReciever with AlarmManager.
but not sure which one to use.
for the following reasons:
I don't think Alarm or Timer will be the best as it doesn't make sense to have an alarm after every month as user may can install the app at any date.
besides that I can't even check the date from user device as what if the user has wrong time set on device?
And what if the user doesn't have the internet connection available at first of the month or what if he didn't launch the app on the first.
I know we can use service for this problem but again is it feasible to always run the service in background which only requires once a month?
I checked other SO post too like this Scheduling recurring task in Android
but its not same as my case as I only have to make the calls once in a month.
Thanks.
use
AlarmManager is your best bet.
Your Issue:-
while scheduling an alarm check the current date and schedule the alarm accodingly EX:- if today is 17th schedule alarm after 14 day.
Regarding getting the current date put the current date on server and get it from there
in my opinion you should write a service and execute timer in your service which will be executed after every 24 hours and than if you don't want to get date and time from user device you can use google API or any other API for getting time and than decide if app needs to update score or not.
Related
We are designing an app that users use on a subscription basis. When the user pays for a particular period, a receipt is issued from our server valid for the duration. The app is designed to be used "offline", so it will only be connecting with the internet to get the receipt from the server.
When in "offline" mode, the app waits for the paid duration #-of days until it requires the user to pay again. We are having problems implementing this.
We first tried to use an always running background service. But after reading https://stackoverflow.com/a/2682130/5753416, http://www.androidguys.com/2009/09/09/diamonds-are-forever-services-are-not/, we decided to use AlarmManger to schedule periodic alarms to check for the payment.
We are now facing problems with the alarm when the user changes the time/date. We are listening to Intent.ACTION_TIME_CHANGED and Intent.ACTION_DATE_CHANGED broadcast, but that isn't being broadcast when setting the date to the past.
My question is what is the right way of implementing a similar functionality. Any advice is appreciated.
edit
issues with the broadcast
ASOP bug issue
There is one solution what you need to do is check phone up time so you can schedule alarm for some time like may be an hour or depending on your freq. So what you need to do is check phone uptime + duration for which the user is paid if that passes then cancel his subs. The only extra thing u need to check is if phone restarts once net should be connected to access your service.
I am a newbie to android , and I am designing an app that reminds event , I am not aware of how to get started to achieve this .
I stored the Date Month Year of the event in the database , the thing I need is
Run a background service that looks for the event to occur
push notification based on time set by the user , also days set that is 1 days before , 15 days before
Please help me how to achieve this
Use the AlarmManager.
You can register a pending intent to be triggered at any time, or periodically. You can then make the pending intent trigger a broadcast or a service or just a notification, if I remember correctly. Just check this or google for Notification / AlarmManager.
This way is more power efficient than just run a background service that checks periodically if it needs to notify the user.
I want my app to start automatically every month on a particular day(Specified by user).
I searched a lot on Google, but I din get any solution. I got alarm for seconds like the app starts after a particular seconds. I am new to android. So an example will be highly appreciated
You are looking for the AlarmManager class.
Quoting from the documentation,
This class provides access to the system alarm services. These allow
you to schedule your application to be run at some point in the
future.
create a service with alarm manager ...........
and start the app when particular date comes
I am using alarm manager along with broadcastReceiver. I am able to set an alarm and all that. But I am stuck with how to approach my problem. I need to be able to set a repeating alarm and the trick here is to have it repeat every 14th of a month at 4:00 pm (so monthly alarm).
How do I go about it? I know how to make it repeating every day or every week as it is easy to calculate the how many milli seconds in a week, but when we are talking monthly, every month has different number of days so I can't set it with a fixed interval.
Any help here?
Thank you
Based on the req. you provided above, I would only schedule one alarm at a time, just calculate the new time values when the previous one is triggered. One thing you will want to do as well is setup a service to listen for boot complete event. Alarms do not persist through restarts. you can find information on how to do that here How to start an Application on startup?
Is there a way to send a notification to the user that app has been downloaded but a certain task from the app is not yet complete even after certain period - say a month. One way is a background service which should come alive every month in this case, check the app state (in sharedprefs) and then send a notification. Is there some other easier way in Android without writing custom service.
Here's how I would do it. Schedule an alarm using the AlarmManager to go off a month from today. That alarm can trigger some code inside of a Receiver or otherwise to check whether the said event has occured. If it hasn't, you can then show a Dialog or whatever.
In order to wake up your app after some amount of time (in your example a month) you're going to have to set an alarm. You can use AlarmManager for that. If all you're going to do is check SharedPreferences, you can do that in a broadcast receiver. You can send your notification there.