I have an app which has a queue in the background to do something. I also have a service which shows and updates a notification in the notification-center about the progress of the queue (because it is not depending on a certain activity).
Notification be like: "there are 4 items left".
All works great except the app is destroyed (by the user and/or system).
I want to update the notification bevore the app or the service is closed (to something like "come back, you missed some items"). I know there is no "close"-event for an application and the "destroyed" event from the service is not called in this case.
I'm working with Xamarin.
What can I do to achive this? Any ideas? Thanks
You can initiate your background service using Alarm Manager even if it is killed. I have faced the same problem when I had to update the user's current location. I set an infinite alarm which starts android service if its not started in every five minutes. You can give it a try.
Related
I am trying to implement a timer with notification that cannot be swiped away by user. I want to continuously update the timer's time.
I know there are a lot of similar question online. But a lot of them discuss using handler, or a service. All of these won't run when my app is not being used by the user. AlarmManager doesn't work because i want to update every x millisecond.
Thanks.
Use a foreground service
A foreground service performs some operation that is noticeable to the
user. For example, an audio app would use a foreground service to play
an audio track. Foreground services must display a Notification.
Foreground services continue running even when the user isn't
interacting with the app.
There is a limit on how frequently you can update the notifications as well, every MS will cause problems.
I want app to start a timer at certain point and run for four hour and is displayed in UI.
Now problem with using service is that it closes if app is closed. (Same with Handler)
And if I use system time than if someone changes system time, 4 hours extends
So is there a better way to implement such task?
You can make services not close when the activity (visible) is stopped. One way to guarantee it stays alive is to make it a foreground service (will show a notification). But if you don't make it a foreground service it will still last for some time unless your device is running out of battery for example.
In your case, to make it last four hours, perhaps use the foreground service alternative (showing a notification)
Foreground service
Vogella foreground service
AlarmManager should help see page http://developer.android.com/reference/android/app/AlarmManager.html
I would like to notify my users of new content available in the latest version of my android game without using an external service like Push Notifications.
What I would like to do as a first step to achieve this is to just create a standard notification on application update.
This brings me to my question: is it possible to start a service the first time a user launches the game and then just keep it running indefinitely? I want to even keep it running after an update completes. Since it is possible that the code for the service may have changed between versions, would I need to stop the old version of the service manually and start the new version? Is it possible to even do something like this where the event that drives initiation of the service is the completion of an app upate?
My plan is to have this service check some persisted data about the last time the user was notified about new content and based on this I will be able to determine if a notification should be created for them after they update.
It's important that they are not required to go back into the game after update in order for the notification to be created. This is the problem that I am having now. Auto-updates occur and they don't know and hence also don't know that there are new stages, etc, available so they never go back into the game if they were already done with the previously available content.
It seems that games like Family Guy have got this approach working, but I am not sure if they implemented it in this way. Even after I update it, I will still get notifications even if I have not actually executed the app since update.
Services are mostly killed when other process needs the resources. Therefore use
startForground(...)
for running Services indefintly on background.
See here: http://developer.android.com/reference/android/app/Service.html#startForeground(int, android.app.Notification)
What you can do is use the AlarmManager in your service that notifies you at certain time. Start the service, do the task and set again an alarm to notify you. In this way, you'll be able to run your service infinitely at periodic time.
I started redacting this answer on GameDev Stack Exchange before you
deleted the question. Unlike the guy who told you to delete, i thought it was an interesting question even for game dev stack exchange.
What you describe is a bad practice on android. I don't believe any game do that. What's more, android require "service" app to run as ForegroundService (This force you to display you app icon in the notification bar, so that users are always aware of running services) It can also be randomly be killed when the system lacks memory. However it can be implemented nicely using Alarms and AlarmManager.
Services and Notifications :
You can however schedule intents when the app is running, with AlarmManager, this is the good practice. Let's take a simple exemple : Candy Crush.
In Candy Crush, when you lose your last life, every 30 minutes you regain one, and have a limit of 5 lives. You want to notify the user when all his lives are back. How to do that ?
Exemple 1 :
When the user lose his last life or quit the game, schedule an intent with AlarmManager in (5-number_of_life)*30 minutes that will fire a local notification.
Exemple 2 :
Schedule an intent with AlarmManager every 30 minutes to check the number of lives, and fire a notification when the user have 5 lives again.
Using a Boot receiver may also allow you to schedule things as soon as the device boot.
However, users expects to be able to disable this kind of features.
Updates :
If you want your app to notify users when an update is available, you'll need to somehow check on the internet with a request on your server. You may schedule an intent again with alarm manager to check regularly if the update is available. If the user is ingame, you can also check it more regularly.
A lot of "online" games do that, and simply force the user to download the new update from the Play Store, before they can play again.
I have developed an android app that needs to run in the background and alert the user after a certain amount of time. The problem is I have is that if I set the time and do something else on the phone, the notification doesn't come through. (I tested this by setting the timer for 10 minutes). Is there a way to make sure the app is always open in the background?
Did you use a Service? If not, it is definitely not running in the background. As soon as an Activity is withdrawn from focus, its onPuase method is invoked and it stops running (see Activity lifecycle). To make your app working in the background you need to implement a Service.
I am building an android app for fetching internet data and rendering it as a list. The data is changing every minute, so I made a service and used a Timer to load the data with an interval.
My question is that:
I want to know when the app (not a particular activity) goes to the background, for example, user pressed the home button, in that case, I want to pause the service in order to save battery.
I want to know when the phone is sleeping (screen dimmed), in that case, I would like to pause the service too.
Any ideas?
Pause your service in onPause (and resume in onResume).
Unless you have a wake lock, your app will not run when the phone is sleeping.
If you're bound to a foreground app, why use a service in the first place?
To elaborate on 1: I understand you're saying "app", not "a particular activity", but your best bet is to use Activity.onPause and Activity.onResume. If anything, you can add a short delay so that nothing happens if onPause is followed immediately by onResume.