Increase points even app is closed android - android

I am new in android and I don't know how to do this, I want to create a background service, There is two main component in my app one is Button (Start Earning) and another is TextView (Points). I want to create service like if User Open app and Click on Start Earning Button then the the points in TextView wants to Increase Continuously For 1 Hour (100 Points In 1 Hour). then after 1 hour I want to stop that service automatically even if app is closed
Example Is In This Screenshot.
Pls Help Me I Search Every Where (Google,Stackoverflow,Github,etc.) But I Don't Get Any Answer.

The service you are looking to use here is a broadcast service, which broadcast how much time has been passed after button has been pressed. So you can have a broadcast service, which has a CountDownTimer for counting down 1hr time. Then you can broadcast the message (containing the points) after a particular time (like after each minute or seconds or after an hour) based on your need. In your fragment/activity you will have an broadcast listener, which will listen to the broadcasted messages (points) and update the UI. You can refer to https://stackoverflow.com/a/22498307/5309486 answer for the implementation details.

Related

Stopwatch service for updating notification

I am working on time tracking app. I need to track time and display it in activity and notification in minutes and seconds live. In order to display live time in notification I need some kind of service that updates the notification each second eventually sends broadcast for updating the stopwatch in activity. The question is how to implement the service.
Should I use something like ACTION_SCREEN_OFF/ACTION_SCREEN_ON to start and stop the service? What kind of timer should I use for example JobScheduler?
Thank you all for any response :)
I actually discovered that I dont need a service to update the notification. I can use Chronometer widget that does all the magic with counting seconds.

Update a textview even while user is not using my app

I basically want to create a timer. I have a textview where I need to show a timer like "Updating your location in 2min 29 secs" and I want the timer to decreament For eg 2min 28secs followed by 2min 27 secs.
And I want to update it even when the user is not using my app (I dont want it to update in real sense what I mean is when user opens my app after say 1 min then the timer should show 1min 27 secs).
Can some one please help me out in pointing out the correct and efficient way of doing this ? Thanks :)
The most efficient way in my opinion is using scheduleAtFixedRate when your activity is in foreground and when it is going to background you do not need any services you can just save the current time and the remaining time in a sharedpreferances and when again your activity comes to foreground read those values plus current time and then update the textview in a proper way.
You could use a service and send intents that your activity receives (the one with the TextView) using LocalBroadcastManager. You should register a BroadcastReceiver inside Activity.onResume(), and unregister it inside onPause().
Using a TimerTask should be the default approach but you must be aware that if your activity is sent to background it can be killed by the system. That's why I recommend using a service.
Hope it helps.

Remind event based on the date android

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.

Android Communicate with AlarmManager

I am not sure how to describe my Problem. Basically I want to write an app, which reminds the user after a specific time (e.g. 1hr) of something. Unless he does not confirm the reminder, it will nag him every 10 mins, after the initial hour has passed.
Until the reminder pops up for the first time there will be 4 stages (eg. every 15mins), which should be shown inside the activity (eg with colors green, yellow, orange, red).
I implemented the reminder with a Broadcast Reciever and the AlarmManager, so far so good. But how can I check how long its been since the Alarm was set to find out in which stage I am? If the Activity is running in the foreground while the stage changes, the change should be made visible instantly. If the activity is started while the alarm is still counting down, the current stage should also be shown.
Anyone can point me in the right direction on how to do this in a good way, since I am still fairly new to Android.
Thank you.
[So far I basically followed this tut http://www.vogella.de/articles/AndroidServices/article.html]
You may want to maintain an int that represents the state of your counter. You can make it a member variable in your Activity, and you may want to persist it in shared preferences as well. To pass status change notifications from the alarm service back to your Activity, send an Intent. Your activity need not check up on the alarm counter; it can simply wait for the intents to roll in.

How to initiate events based on stopwatch timer?

Basically I'm working on an application that alerts drivers how long they have been driving. It requires alerts at certain times.
At the moment I'm using a chronometer that displays time passed.
As one hour passes I need a message to appear on screen saying 'Hour Passed'.
As 2 hours passes I need another message saying 'Hour Passed' and a further message saying 'A 15 minute break is required'. Then a button needs to appear that allows the user to start a break which until starts a new chronometer.
When the break is complete at 15 minutes, a button needs to appear that says 'continue driving' and the original chronometer displaying driving time needs to continue from the time it was at before the break (2:00:01).
As you can see all the functionality is relatively similar, based on time events.
I'm guessing I need to use some sort of if statements that are initiated when the chronometer reaches a certain time but I'm basically stuck on how to make the application do the required functionality when the chronometer reaches a certain time.
Try taking a look at the AlarmManager class to run an intent a certain amount of time after the time the stopwatch is started.
Try using the alarm manager with a pending intent.Create a receiver that catches the pending intent action and do whatever you want to do.
You can either register a broadcastreceiver from your app when it is started(preferred way in your case) or create a broadcastrecievr and create a intent filter in the manifest file...In this case the event would be catched even if your app is not running.

Categories

Resources