(Android) How to run multiple instances of a service - android

anyone knows how i can make multiple instances of same service class work seperately, am working on a taxi meter app where i have a "Counter" service which will calculate for me the distance and add locations to database for each client, in my mainactivity i made three instance of the same service and started everyone seperately but it doesn't seem to work as expected(only i made a toast in every service start action but it seem to appear for just one client).
i know i have read in several treads that it's no possible to make multiple instance of services work at the same time but this is the only way i could do to make a taxi meter that supports multi clients at the same thing.

As rightly pointed out, we cannot have multiple instances of service. A service can only have one instance. Each subsequent call to startService, the method onStartCommand is called again.
But,suppose the onStartCommand instantiates a Timer object to repeat a task every 30 secs. Each subsequent call to startService, a new instance of object Timer is created...!
Now concider following use case:
timer is set up to log "Hello World" after every 30 sec
1st startService command is issued at 16:00:00
for next 2 mins, you should see 5 "Hello World" printed at
-16:00:00
-16:00:30
-16:01:00
-16:01:30
-16:02:00
Now 2nd startService command is issued at 16:02:10, for next 2 mins, you should see "Hello World" printed as follows:
-16:02:10 (2nd call)
-16:02:30 (1st call)
-16:02:40 (2nd call)
-16:03:00 (1st call)
-16:03:10 (...)
-16:03:30
-16:03:40
-16:04:00
So after 2nd call, two instances of the Timer class exist. But only one Service instance.

Related

Flutter-foreground-task: how to change the callback interval

I'm using flutter_foreground_task, and I try to change the callback interval.
I tried several things, but no one works:
In _receivePort's listening function that handles messages from onEvent::_sendPort?.send(message):
I tried to _closeReceivePort(), then _stopForegroundTask(), then _initForegroundTask with a different interval, and finally _startForegroundTask
The same without _closeReceivePort
I tried to only _initForegroundTask again with another interval and _startForegroundTask (so that it restarts)
I also tried 1, 2 and 3 in a Timer callbcak function called after the _receivePort listening function has finished (mutex protection)
For 3: the foreground task still work, but at the same interval.
For 1: I don't really understand, it seems the foreground task still runs, but my _receivePort listening function is not called anymore.
What would you advise to do change the OnEvent interval ?
Thanks for help.

Take action for every two minutes of inactivity (no user interaction)

I am trying to write to a file whenever the user has not interacted with the application for 2 minutes. Currently am having base activity in which I have overriden the onUserInteraction method. In this method the float time variable is reset and onResume I subtract the time with the current time to check if two minutes have passed. This works fine but sometimes acts crazy. Second approach was using the postDelayed method of the Handler and start a thread. This works perfectly but does not include the case when the app goes to background or the device goes to sleep.Is there a way to cover all these cases. ahve researched a lot. Also came across Wakeful Intent service but read that it is expensive.
Is there a way to cover all these cases.
Yes. ... and it is expensive. Waking the phone up, every 2 minutes is going to drain the battery like crazy.
That said, the answer to your question is that you need to use the AlarmManager, probably in concert with either the WakefulIntentService or the WakefulBroadcastReceiver. Create a PendingIntent and schedule it for delivery every 2 minutes.
I don't know a way of tracking inactivity but there is a way to track user activity. You can catch a callback called onUserInteraction() in your activities that is called every time the user does any interaction with the application. I'd suggest doing something like this:
#Override
public void onUserInteraction(){
MyTimerClass.getInstance().resetTimer();
}
If your app contains several activities, why not put this method in an abstract super class (extending Activity) and then have all you activities extending it.

Android - refresh activity

My android application has 2 activities:
In the first one (MainActivity), the user chooses some parameters and these parameters are sent to the second activity (Display).
The second activity calls a web service, and according to the chosen parameters, the web service returns a value. I use the returned value to draw a bar chart of the evolution of this value. That's why I created a timer in the second activity that I put in the onCreate() function:
Timer t = new Timer();
t.scheduleAtFixedRate(new TimerTask() {
public void run() {
finish();
startActivity(getIntent());
}
},10000, 10000);
So every 10 seconds, the second activity is called again and the bar chart is updated with the new returned value.
The problem is, after the 2nd or the 3rd execution of the timer, several identical values are returned at the same time, as if the activity was called several times. And then the application starts freezing (but doesn't close).
I'm using the charts provided by this library: http://android-graphview.org/
I've also tried using the functions provided on the above website (resetData and appendData), and also the invalidate() function, but nothing works.
Any ideas why this happens? Is my way of refreshing the activity wrong?
The way you described it:
you create a new Timer (see 2.) with every call of the second activity
The purpose of each Timer is to call the second activity (see 1.) every 10 seconds
As a result the amount of times the second activity is called as a function of time increases exponentially.
A possible solution would be to move the timer to the onCreate method of your Main Activity (and still call the second activity from it).
This way there should be exactly one Timer active at any time.
EDIT:
As commented by Marius, an Activity might not be the optimal choice. If there is no user-input and the only thing the activity does is call a webservice and return the result, a method called from your Main Activity would be sufficient.
Firstly, calling finish() and starting the activity, NOT A GOOD IDEA.
Secondly, As far as i have understood your scenario, calling an AsyncTask inside a Timer after every 10 seconds is a better way to accomplish this. Call your web service in doInBackground() and then update your UI from onPostExecute(), this way you can avoid calling finish() and relaunching your activity every 10 seconds.
Finally you are creating a new Timer instance eveytime your Activity is called, so creating a huge number of Timer instances hanging your application.

ANDROID: Is it possible to run a specific function every 10 seconds while the application is running?

I have three activities
1 is the main activity
2 is some other activity that does something
and 3 is some other activity that does some storage management with sqlite
in this 3 activity I have a function that updates some data on the database's table
I want every 10 seconds to call a function in order to update this table
ScheduledExecutorService can do what I want. However I'm not 100% sure. If I'm on the 1 activity will it be able to call the function from the 3 activity even though I'm on the 1?
I also found alarmmanager but I don't want the update of information to be happening even when I close my application, I want it to happen only when I have the application open!
thanks!
You can use TimerTask or Handler. Here is a tutorial:
http://developer.android.com/resources/articles/timed-ui-updates.html

How to notify Activity about the progress of a Service

I'm writing an Android app with 3 Activity()s: A, B and C; and one Service: S. The user can start the service using activity A. The service runs for a long time, in 7 steps. Each step takes a few seconds (or minutes). Once a step has completed, I want the service to notify Activity()s B and C about its progress if they are visible. That is, I want to notify an Activity different from which has started the Service. There is no need the show an Activity if it's not visible.
How do I do that? What is the cleanest and/or simplest solution?
Register a receiver at runtime and listen/publish the events. This answer provides an example

Categories

Resources